Getting Started With Maven

Maven is a popular dependency and build tool within the Java ecosystem, however it can be daunting to start with. This guide is targeted at developers that have previously used tools such as npn, gulp and grunt. In a previous blog article, we provided a guide to using AWS Code Deploy and this article aims to give you key information and useful resources about maven for the most frequent scenarios.

Install Maven

The installation of maven is relatively simple; download, extract and place the appropriate bin folder onto your PATH variable. The instructions on the project’s own installation page should be enough to get a point where the terminal command mvn works. You can check everything is expected by getting maven to print version

> mvn -version
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T16:41:47+00:00)
Maven home: ~\dev\bin\apache-maven-3.x.x\bin\..
Java version: 1.8.0_xx, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.8.x_xx\jre
Default locale: en_GB, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "dos"

Generate the pom.xml

The Project Object Model (POM) file is the key file inside a maven project. It provides information about the project itself, how it is configured and its dependencies. To get started you can allow maven to use the quick start template. Additional details about this quick start template can be found here:

 mvn archetype:generate \
  -DgroupId=com.mycompany.app \
  -DartifactId=my-app \
  -DarchetypeArtifactId=maven-archetype-quickstart \
  -DinteractiveMode=false

This command doesn’t need to be run within a project folder as it will generate a folder named “my-app” which will contain all the other generated files.

At this point you will have a pom.xml file with the following content.

The groupId and artifactId identify this project and its output. The packaging tag tells Maven how the final output of the project will be packaged. You will typically see jar and war as values. These two types of packaging are very popular in the Java ecosystem. You may also see the type pom. This is used to create a package of submodules and allow cross artefact dependencies between artefacts of the same group.

Using Maven for the First Time

With a valid POM and maven directory structure you will now be able to package the project.

> mvn package

After running this command, you will have a folder “target” which will include your classes packaged in a jar file.

Fix Your Warnings

If you look carefully at the output of Maven you will notice a warning about the build being platform dependent.

[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!

Simply setting the source encoding removes this warning. This can be done by adding the following to the node.

<properties> 
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

We will revisit the use of properties later in this guide.

Dependencies

A dependency is identified by its GroupId and ArtifactId. Many projects will give you an example XML node to add to your pom.xml document. For example junit gives this dependency node in its wiki:

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
  <scope>test</scope>
</dependency>

This is added inside the node.

Simple dependencies often only have one artefact but more complex or well modularised dependencies could have many artefacts to select. It becomes tedious (and error prone) to specify the version number using a literal value.

We can add a node to properties

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <log4j.version>2.4</log4j.version>
</properties>

and use that throughout our POM like so:

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>${log4j.version}</version>
  <scope>test</scope>
</dependency>

Finding Dependencies

The Maven central repository contains a large number of dependencies to select from. If it does not include the dependency you are looking for then you should consider installing the jar locally or hosting your own private Maven repository.

Packaging Everything in a Single Executable Jar

Often it is desirable to package your project into a single jar with all the required dependencies included. This can be done by using the assembly plugin. The first step is to update your POM to include your main class in the assembly configuration by adding the following node to

<build>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef> 
        </descriptorRefs>
        <archive>
          <manifest>
            <mainClass>com.mycompany.app.App</mainClass>
          </manifest>
        </archive>
      </configuration>
    </plugin>
  </plugins>
</build>
 

> mvn assembly:single

You can now run the jar with the following command:

> java -jar target/my-app-1.0-SNAPSHOT-jar-with-dependencies.jar
Hello World!

Conclusion

In this guide you have learnt the basics of Maven, details about the POM file, how to use properties in the pom file to keep it as DRY as possible and learnt how to find and specify dependencies.

Tweet us @FastWebMedia if you have any further thoughts about Maven and check out our data analysis services that could help you build a strong competitive advantage.

We hope you enjoyed reading

Related Articles