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

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.   <modelVersion>4.0.0</modelVersion>  
  4.   
  5.   <groupId>codesstore</groupId>  
  6.   <artifactId>codesstore-core</artifactId>  
  7.   <version>0.0.1-SNAPSHOT</version>  
  8.   <packaging>jar</packaging>  
  9.   
  10.   <name>codesstore-core</name>  
  11.   <url>http://maven.apache.org</url>  
  12.   
  13.   <properties>  
  14.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  15.   </properties>  
  16.   
  17.   <dependencies>  
  18.     <dependency>  
  19.       <groupId>junit</groupId>  
  20.       <artifactId>junit</artifactId>  
  21.       <version>3.8.1</version>  
  22.       <scope>test</scope>  
  23.     </dependency>  
  24.   </dependencies>  
  25. </project>  

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

MyClass.java

  1. package com.codesstore.util;  
  2.   
  3. public class MyClass {  
  4.   
  5.  public void printHello() {  
  6.   System.out.println("Hello Maven...");  
  7.  }  
  8. }  
And called that class in main method.

App.java


  1. package com.codesstore.main;  
  2.   
  3. import com.codesstore.util.MyClass;  
  4.   
  5.   
  6. public class App {  
  7.    
  8.  public static void main(String[] args) {  
  9.   new MyClass().printHello();  
  10.  }  
  11. }  
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

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.  <modelVersion>4.0.0</modelVersion>  
  4.   
  5.  <groupId>codesstore</groupId>  
  6.  <artifactId>codesstore-core</artifactId>  
  7.  <version>0.0.1-SNAPSHOT</version>  
  8.  <packaging>jar</packaging>  
  9.   
  10.  <name>codesstore-core</name>  
  11.  <url>http://maven.apache.org</url>  
  12.   
  13.  <properties>  
  14.   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  15.  </properties>  
  16.   
  17.  <dependencies>  
  18.   <dependency>  
  19.    <groupId>junit</groupId>  
  20.    <artifactId>junit</artifactId>  
  21.    <version>3.8.1</version>  
  22.    <scope>test</scope>  
  23.   </dependency>  
  24.  </dependencies>  
  25.   
  26.  <build>  
  27.   <plugins>  
  28.    <plugin>  
  29.     <groupId>org.codehaus.mojo</groupId>  
  30.     <artifactId>exec-maven-plugin</artifactId>  
  31.     <version>1.1-beta-1</version>  
  32.     <executions>  
  33.      <execution>  
  34.       <goals>  
  35.        <goal>java</goal>  
  36.       </goals>  
  37.      </execution>  
  38.     </executions>  
  39.     <configuration>  
  40.      <mainClass>com.codesstore.main.App</mainClass>  
  41.     </configuration>  
  42.    </plugin>  
  43.   </plugins>  
  44.  </build>  
  45.   
  46. </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


1 comment: