How to create a Java application with Maven

Here I explain how to create and run Java Application using Maven and Eclipse.
Tools used

Eclipse (Helios)
Maven Integration for Eclipse (Help->Eclipse Marketplace  and install it.)
Exec Maven Plugin

Create new Maven project.









































































Now generated project structure look like this.


















Generated pom.xml

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>codesstore</groupId>
  <artifactId>codesstore-core</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>codesstore-core</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

I created a new class called MyClass.java in com.codesstore.util package.

MyClass.java

package com.codesstore.util;

public class MyClass {

 public void printHello() {
  System.out.println("Hello Maven...");
 }
}
And called that class in main method.

App.java


package com.codesstore.main;

import com.codesstore.util.MyClass;


public class App {
 
 public static void main(String[] args) {
  new MyClass().printHello();
 }
}
Now I'm going to add Exec plugin to pom.xml. See more information on Exec plugin http://mojo.codehaus.org/exec-maven-plugin/

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>

 <groupId>codesstore</groupId>
 <artifactId>codesstore-core</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>

 <name>codesstore-core</name>
 <url>http://maven.apache.org</url>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>

 <dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>3.8.1</version>
   <scope>test</scope>
  </dependency>
 </dependencies>

 <build>
  <plugins>
   <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.1-beta-1</version>
    <executions>
     <execution>
      <goals>
       <goal>java</goal>
      </goals>
     </execution>
    </executions>
    <configuration>
     <mainClass>com.codesstore.main.App</mainClass>
    </configuration>
   </plugin>
  </plugins>
 </build>

</project>
Right click on the pom file -> Run as ->Maven build..























Then you can set goals to run. I use exec:java here.



































Out put













Project structure in Eclipse


No comments:

Post a Comment