Create a Java Project using Maven
Environment
The versions in this tutorial shouldnt matter too much, but Im using:
- Eclipse Java EE IDE for Web Developers.
Version: Luna Service Release 1 (4.4.1)
Build id: 20140925-1800 - Apache Maven 3.2.3
- Oracle JDK 1.7.0_71
Outline
- Creating the Project
- Cleaning the Project
- Importing the Project
Create the Project
mvn archetype:generate
-DgroupId=<my-group-id>
-DartifactId=<my-artifact-id>
-DarchetypeArtifactId=maven-archetype-quickstart
-DinteractiveMode=false
The groupId is unique among all Java projects. This generally conforms to the Java packaging naming standards. The artifactId is used in artifact generation and For example, a project might have a dependency on lucene-core (artifactId) and the source code in this project has the package structure "org.apache.lucene.core".
Operational Output
$ mvn archetype:generate -DgroupId=org.swtk.common.qa -DartifactId=commons-qa -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.2: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: basedir, Value: C:projectsswtk-common
[INFO] Parameter: package, Value: org.swtk.common.qa
[INFO] Parameter: groupId, Value: org.swtk.common.qa
[INFO] Parameter: artifactId, Value: commons-qa
[INFO] Parameter: packageName, Value: org.swtk.common.qa
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: C:projectsswtk-commoncommons-qa
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.802 s
[INFO] Finished at: 2014-12-12T15:58:12-08:00
[INFO] Final Memory: 15M/491M
[INFO] ------------------------------------------------------------------------
Using WinDirStat, lets look at what Maven has visualized for us:
The generated directories conform to Mavens standard directory layout. There is a already a simple Java class and even simpler test harness in place as a placeholder to run the Maven build lifecycle.
Clean the Project
Run the package command, as part of the maven build lifecycle to validate the new project:
mvn clean package
Import into Eclipse
As it stands right now, we will not be able to import this project into Eclipse, until we instruct Maven to add an Eclipse nature to the project.
Run this command:
mvn eclipse:eclipse
Operational Output
$ mvn eclipse:eclipse
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building commons-qa 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-eclipse-plugin:2.9:eclipse (default-cli) > generate-resources @ commons-qa >>>
[INFO]
[INFO] <<< maven-eclipse-plugin:2.9:eclipse (default-cli) < generate-resources @ commons-qa <<<
[INFO]
[INFO] --- maven-eclipse-plugin:2.9:eclipse (default-cli) @ commons-qa ---
[INFO] Using Eclipse Workspace: C:BackupJavaworkspacesswtkcommon
[WARNING] Workspace defines a VM that does not contain a valid jre/lib/rt.jar: C:Program FilesJavajre7
[INFO] Adding default classpath container: org.eclipse.jdt.launching.JRE_CONTAINER
[INFO] Not writing settings - defaults suffice
[INFO] Wrote Eclipse project for "commons-qa" to C:BackupJavaworkspacesswtkcommonprojectsswtk-commoncommons-qa.
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.797 s
[INFO] Finished at: 2014-12-12T16:07:38-08:00
[INFO] Final Memory: 11M/491M
[INFO] ------------------------------------------------------------------------
Then import the project into your Eclipse workspace:
File > Import > Existing Projects into Workspace
I find thats its necessary with this current version of Eclipse and Maven to perform an additional step at this point:
Right-Click on the Project > Configure > Convert to Maven Project
Eclipse will likely update the project with some default values, including JDK 1.5. It will be necessary to explicitly set the JDK build level in the POM file like this:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
By using these values in the POM file, Eclipse and Maven will stay in sync with Java compiler levels.
Automation
Under Windows, I like to add this batch script to my bin directory of Maven (or anywhere on the path):
@echo OFF
REM http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true
mvn archetype:generate -DgroupId=%~1 -DartifactId=%~2 -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
cd %~1
mvn eclipse:eclipse
mvn build
cd ..
On either OS X or Linux/Ubuntu, this shell script works great:
mvn archetype:generate
-DgroupId=$1
-DartifactId=$2
-DarchetypeArtifactId=maven-archetype-quickstart
-DinteractiveMode=false
pushd $1
mvn eclipse:eclipse
mvn build
popd
Then, when creating a new Java project, I can simply invoke:
mvn-gen com.mycompany.project my-project
References
- Introduction to Archetypes
- Archetype is a Maven project templating toolkit.
- An archetype is defined as an original pattern or model from which all other things of the same kind are made. The names fits as we are trying to provide a system that provides a consistent means of generating Maven projects. Archetype will help authors create Maven project templates for users, and provides users with the means to generate parameterized versions of those project templates.
- Standard Directory Layout
- Having a common directory layout allows users familiar with one Maven project to immediately feel at home in another Maven project. The advantages are analogous to adopting a site-wide look-and-feel.
- Creating a Web Project with Maven (and without Eclipse)