Introduction to Maven – Part 2

Login | Register

Introduction to Maven – Part 2

Maven

In the first post about Maven, I showed how to download, install and test a Maven installation. In this post, we’ll show how to create a basic Java application project using Maven and how to compile, test and run the application.

Create A New Project

To create a new project using Maven, we use the mvn archetype:generate goal. This goal takes several parameters that specify how the project is to be built.

mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes
-DarchetypeArtifactId=maven-archetype-quickstart
-DgroupId=com.acme.mavenapp
-DartifactId=MavenApp

At first glance this looks rather complicated, but the idea behind Maven is to use different archetypes to define projects. An archetype is basically a template project layout that can be used to generate a project based upon some questions asked to the users. A Maven template or archetype is defined by the archetypeGroupId and the archetypeArtifactId parameters.

In this example above, we are using the built in Maven Quick Start archetype.

The groupId parameter specifies the unique name for the project whereas the artifactId parameter specifies the name of the main artifact of the project for example the projects .Jar or .War file etc.

When we run this command, Maven will download the necessary files required to build the project and will ask what version of the project we wish to build – the default is 1.0-SNAPSHOT.

We can accept this default or change to any value required. After selecting a version, Maven will provide confirmation of the project definition. If we accept this definition, the project will be created.

~/temp/maven $> mvn archetype:generate
-DarchetypeGroupId=org.apache.maven.archetypes
-DarchetypeArtifactId=maven-archetype-quickstart
-DgroupId=com.acme.mavenapp
-DartifactId=MavenApp
… Loads of Maven output…
Define value for version: 1.0-SNAPSHOT: :
Confirm properties configuration:
groupId: com.acme.mavenapp
artifactId: MavenApp
version: 1.0-SNAPSHOT
package: com.acme.mavenapp
Y: : y
… Loads of Maven output…
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 minute 5 seconds
[INFO] Finished at: Fri Oct 24 00:57:16 BST 2008
[INFO] Final Memory: 7M/14M
[INFO] ------------------------------------------------------------------------

Maven has now created a new project inside the MavenApp directory based upon the Quick Start Maven artifact that we selected earlier.

Compiling, Testing and Running a Project

Now that we have created a project with Maven, we can compile, test and run it. To perform these actions, Maven defines three additional goals:

  • mvn compile – This will compile our application
  • mvn test – This will test our application
  • mvn package – This will package our application into a .Jar file

To compile the project, change directory into the MavenApp directory and execute the mvn compile goal.

~/temp/maven/MavenApp $> mvn compile
… Lots of output …
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3 seconds
[INFO] Finished at: Fri Oct 24 01:14:25 BST 2008
[INFO] Final Memory: 3M/9M
[INFO] ------------------------------------------------------------------------

The Maven Quick Start project also includes a basic JUnit test class. We can run this test by executing the mvn test goal.

~/temp/maven/MavenApp $> mvn test
… Lots of output …
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.acme.mavenapp.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.088 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

Finally, we can package the application into a .Jar file by executing the mvn package command.

~/temp/maven/MavenApp $> mvn package
… Lots of output …
[INFO] Building jar: ~/temp/maven/MavenApp/target/MavenApp-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4 seconds
[INFO] Finished at: Fri Oct 01 15:18:16 BST 2008
[INFO] Final Memory: 6M/13M
[INFO] ------------------------------------------------------------------------

We can then test out the packaged application by executing the Jar file:

~/temp/maven/MavenApp $> java -cp target/MavenApp-1.0-SNAPSHOT.jar com.acme.mavenapp.App Hello World!

In this post, we’ve seen how to create a simple Maven project and then how to build, test and package it up. In the next article we’ll take a closer look at the project we created, including the pom.xml file.

About

At Develop In Java, we've been a part of the Java community for over 10 years. If there's anything you'd like us to cover, just ask !

Current versions

  • Java - 1.8
  • Java EE - 7
  • Spring - 4.x