How To Create A Java Project With Maven
In this tutorial, we will show you an easy way to create a Java project, using Apache Maven. Also, we will make it support Eclipse IDE. Finally, we will complete the tutorial, by showing how we can package our Java project into an executable “.jar” file.
Tools used :
- Maven 3.0.5
- Eclipse Indigo
- JDK 6
Create a java project using maven’s archetype
Archetype is a Maven project templating toolkit that enables the creation of Maven project templates for users.
In this tutorial, we will show you an easy way to create a Java project, using Apache Maven. Also, we will make it support Eclipse IDE. Finally, we will complete the tutorial, by showing how we can package our Java project into an executable “.jar” file.
Tools used :
- Maven 3.0.5
- Eclipse Indigo
- JDK 6
Create a java project using maven’s archetype
Archetype is a Maven project templating toolkit that enables the creation of Maven project templates for users.
First of all, using the terminal (Linux or Mac) or the command prompt (Windows), navigate to the folder where the new project shall be created Using the command
mvn archetype:generate -DgroupId={project-packaging} -DartifactId={project-name} -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
This command tell Maven to create a Java project from “maven-archetype-quickstart” template. If you ignore thearchetypeArtifactId argument, a list of the templates will be listed for you to choose.
Open command prompt and navigate to the folder where you want to create project(In my case I have created MavenProject folder inside D drive).Issue the following command.
mvn archetype:generate -DgroupId=com.javawebtutor -DartifactId=SampleJavaApplication -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
This command creates a new Java project under the name SampleJavaApplication, along with its entire directory structure.
Layout of the project’s directory
By default, the source code of the project is located under the folder “/src/main/java/project-package”, while all junit tests are located under the folder “/src/test/java/project-package”.
Apart from this, a file called pom.xml is generated by Apache Maven. This filecontains all necessary information about the project . Specifically, this file contains the directory structure, all necessary plugins and all projects’ dependencies.
pom.xml file is shown below:
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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javawebtutor</groupId> <artifactId>SampleJavaApplication</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>SampleJavaApplication</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
Eclipse IDE integration
To convert Maven project to support Eclipse IDE, in terminal, navigate to “SampleJavaApplication” project, and issue this command :
mvn eclipse:eclipse
This command generates those files required by the Eclipse IDE, in order for the project to be recognized as a valid Apache Maven project.
Add the M2_REPO classpath variable in Eclipse IDE
The “M2_REPO” classpath variable is required by the Eclipse IDE, in order to locate all declared depencencies for a Maven project. This variable must point to Maven’s Local Repository.
- Open Eclipse IDE, and click on Windows -> Preferences.
- In the left panel, click on Java -> Build path -> Classpath Variables
- In the right panel, click on “New:” button and fill these values: Name: “M2_REPO” Path: “C:\Users\Username\.m2\repository\”
- Click on the “OK” button.
Now Import the project into Eclipse IDE.Project directory is given below.
Update the “pom.xml” file
It is recommended that you add the compiler plugin, in order to guide Maven for which JDK version to use for compiling your project. Add the following lines to your pom.xml file:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build>
Update the jUnit from 3.8.1 to latest 4.11.
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency>
The above XML snippet is called a “Maven Coordinate”. In order to declare the jUnit “.jar” file as a dependency to your project, you must find its corresponding Maven Coordinate. The same procedure must be executed for every external “.jar” file that poses as a dependency to your project, such as Apache Common, Spring, etc. For your convenience, visit the Maven Central Repository and choose the most appropriate Maven Coordinate for every dependency of your project.
The final form of our pom.xml file is given bellow
<projectxmlns="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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javawebtutor</groupId> <artifactId>SampleJavaApplication</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>SampleJavaApplication</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> </project>
In command prompt, issue the same command mvneclipse:eclipse again,in order for Maven to download all declared plugins and every project dependency. All downloaded files are stored in Maven’s Local Repository.
Develop Sample Application
In this step, we will create a simple Java application that has sum method which takes two int parameters to add them and return it. Inside App.java Copy and paste the following code:
package com.javawebtutor; public class App { public int sum(int num1, int num2) { return num1 + num2; } public static void main(String[] args) { App app = new App(); System.out.println("The sum of number is \"" + app.sum(10, 20) + "\""); } }
Next, we will create a simple jUnit test, in order to confirm that our application functions properly. Insid the AppTest.java. Copy and paste the following code:
package com.javawebtutor; import org.junit.Assert; import org.junit.Test; /** * Unit test for simple App. */ public class AppTest { @Test public void testApp() { App app = new App(); Assert.assertEquals(2, app.sum(1, 1)); } }
Package our application into a .jar file
We can now use Apache Maven to package our application into an executable “.jar” file. The packaging element inside our pom.xml file defines the packaging format or output.
<groupId>com.javawebtutor</groupId> <artifactId>SampleJavaApplication</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>SampleJavaApplication</name>
Open command prompt as in Step 1, navigate to the SampleJavaApplication project folder and execute the command:
mvn package
This command compiles our project, executes all unit tests and finally, generates the executable “.jar” file inside the “project/target”folder.
Project Execution
The generated executable file can be executed using the following command:
java -cp target/SampleJavaApplication-1.0-SNAPSHOT.jar com.javawebtutor.App
You can download the source code of the example by clicking on the Download link below.
Source : Download |
Source + Lib : Download |
Related Articles