In this tutorial, we will show you how to create a Java project with Maven3, imports it into the Eclipse IDE, and package the Java project into a jar faile.
Tools used:
- Maven 3.3.1
- Eclipse 4.4
- JDK 7
<strong>Note
</strong>Please make sure Maven is installed and configured properly before you proceed with this tutorial, to avoid mvn command not found error.
1. Create a project from maven template
In a terminal(Unix or Linux) or command prompt(Windows), navigate to the folder you want to create the Java project. Type this command:
mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart
<span style="white-space:pre"> </span>-DgroupId={project-packging}
<span style="white-space:pre"> </span>-DartifactId={project-name}
<span style="white-space:pre"> </span>-DinteractiveMode=false
This tells Maven to create a Java project from the Maven maven-archetype-quickstart template. You can ignore the archetypeArtfactId option, maven will use the default template: maven-archetype-quickstart.
For example,
D:\workspaces\MavenDemo>mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=org.whl.demo
DartifactId=demo -DinteractiveMode=false
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.3:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.3:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.3:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Batch mode
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: org.whl.demo
[INFO] Parameter: packageName, Value: org.whl.demo
[INFO] Parameter: package, Value: org.whl.demo
[INFO] Parameter: artifactId, Value: demo
[INFO] Parameter: basedir, Value: D:\workspaces\MavenDemo
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: D:\workspaces\MavenDemo\demo
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.672 s
[INFO] Finished at: 2015-05-17T20:55:24+08:00
[INFO] Final Memory: 15M/152M
[INFO] ------------------------------------------------------------------------
In above case, a new Java project named "demo", and the entire project directory structure is created automatically.
<strong>Note</strong>
You can debug the Maven with parameter -X if you have any issues.In additional, this step is not mandatory, you can create the folders manually, see step 2 project structure.
with mvn archetype:generate+maven-archetype-quickstart template, the follwing project directory structrue is created.
demo
|-src
|---main
|------java
|--------com
|----------whl
|------------demo
|---------------App.java
|---test
</pre><pre name="code" class="plain"> |--------com
|----------whl
|------------demo
|---------------AppTest.java
|-pom.xml
In simple, all source code puts in folders /src/main/java/, all unit test code puts in /srtc/test/java/.
In additional, a standard pom.xml is generated. This POM file is like the Ant build.xml file, it describes the entire project information, everything from directory sturctrue, project plugins, project dependencies, how to build this project and etc,read this official POM guide.
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>org.whl.demo</groupId>
<artifactId>demo</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>demo</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>
To make this as an Eclipse project, in prompt, navigate to "demo" project, type this command:
mvn eclipse:eclipse
It will generate all project files that are required by Eclipse IDE.
To import the project into Eclipse IDE, select "File -> Import ... -> General -> Existing Projected into Workspace"
4.Update POM
The default pom.xml is too simple, often times you need to add the compiler plugin to tell Maven which JDK version is used to compile you project.(The default JDK1.4 is too old)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
pom.xml --updated version
<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>org.whl.demo</groupId>
<artifactId>demo</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>demo</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</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.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
In prompt, issue the same command mvn eclipse:eclipse again, Maven will download the plugins and project dependencies (junit) from Maven center repository and store it into your local Maven repository automaticall.