Skip to content
Daniel Blazevski edited this page Sep 21, 2016 · 5 revisions

Introduction

According to Maven's documentation, "Maven is an attempt to apply patterns to a project's build infrastructure in order to promote comprehension and productivity by providing a clear path in the use of best practices."

We will focus on how Maven can be used to initialize, build a java application and manage its dependencies.

Creating a maven project

One creates a Maven project using one of the many (currently more than 1500!) templates Maven allows access to, which are called archetypes.

To create an application from a Maven archetype, one uses the command

mvn archetype:generate

Note that this command does not generate an archetype, rather it is used to create a project from an existing archetype. Running this command will prompt you to chose several project configurations. One can pre-specify the necessary configuration as follows

mvn archetype:generate 								\ 
-DarchetypeGroupId=<archetypeArtifactId>	 		\
-DarchetypeArtifactId=<archetypeGroupID>		 	\
-DgroupId=<GroupId> 								\
-DartifactId=<artifactId>							\

As mentioned, there are more than 1500 archetypes currently available, a standard one to chose is the maven quickstart archetype, which has archetypeGroupID=org.apache.maven.archetypes and archetypeArtifactId=maven-archetype-quickstart.

The groupId and artifactId in the above command are specific to your project and defined by you, whereas the archetypeGroupId and archetypeArtifactId are for the template you want to use.

What do the fields GroupId and ArtifactId mean?

The GroupId is a unique identifier for your project. For example, Apache Flink is a Maven project and its unique identifier is org.apache.flink.

Each project will generate a primary artifact -- an artifact in software engineering is loosely defined as a something produced by a code-base. The artifactId is a unique identifier that you specify. Typically, this will be a JAR file and have the form .jar.

Hello world example

From the previous discussion, to create a new Maven project, one uses archetype:generate to create a project from one of the 1500+ templates Maven makes available. To wrap your head around what goes into creating a Maven project, here is a summary

  • Each Maven project has a unique identifier, namely the GroupId.

  • The primary "thing" or artifact resulting from the project is call the ArtifactId

  • Creating a new Maven project is done by specifying a template, or archetype, for the project

  • The archetype is a Maven project, and as such it has a GroupId and associated ArtifactId, and to specify which archetype you use, one specifies the archetypes GroupId and ArtifactId.

Thus, the following command

mvn archetype:generate 								\ 
-DarchetypeGroupId=org.apache.maven.archetypes 		\
-DarchetypeArtifactId=maven-archetype-quickstart 	\
-DgroupId=hello.world								\
-DartifactId=hello-app								\
-Dversion=1.0.0

does nothing more than create new project based off of the Maven Quickstart archetype, and we call the project "hello.world" and the output of a build of the code "hello-app". Note that the GroupId may not contain dashes, if the GroupId was hello-world, you would get errors later when trying to build your project. The ArtifactId may contain dashes

Result of mvn archetype:generate

After running the mvn archetype:generate command, a lot has taken place. The directory of your project now has the structure

├── pom.xml
├── src
│   ├── main
│   │   └── java
│   │       └── hello
│   │           └── world
│   │               └── App.java
│   └── test
│       └── java
│           └── hello
│               └── world
│                   └── AppTest.java

This directory structure is mainly determined by the archetype that we specified, namely the Maven quickstart archetype. Notice that the groupId hello.world appears in the directory structure and that . creates a directory between hello and world . The files App.java and AppTest.java are from a simple "Hello World" example app.

The pom.xml file

This contains all the meta data related to your project in XML format. This will be modified later when adding in external dependencies to your project.

Building and running the project

Note that the artifactId does not seem to appear anywhere (unless you peak in the pom.xml file). Recall that the artifactId is the identifier for the output of building the project.

How do we build the project? Running mvn compile compiles any java classes, though will not create a .jar file that can be run.

Running mvn package will compile any needed Java classes and create a .jar file. Note that running after running

mvn package

a new directory target is created. This creates a fairly complex directory structure, most of you can can ignore. The most important file is the primary artifact, namely

target/hello-app-1.0.0.jar

One can run the app using java and specifying the jar and the class path (hence the -cp flag)

java -cp target/hello-app-1.0.0.jar hello.world.App

The class path is specified by the GroupId that we specified earlier. and App is the name of the class that contains the main method.

Clone this wiki locally