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.
This article aims to give you key information and useful resources about maven for the most frequent scenarios.
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
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T16:41:47+00:00)
Maven home: ~devbinapache-maven-3.x.xbin..
Java version: 1.8.0xx, vendor: Oracle Corporation
Java home: C:Program FilesJavajdk1.8.xxxjre
Default locale: en_GB, platform encoding: Cp1252
OS name: “windows 10”, version: “10.0”, arch: “amd64”, family: “dos”
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:
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.
With a valid POM and maven directory structure you will now be able to package the project.
After running this command, you will have a folder “target” which will include your classes packaged in a jar file.
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.
We will revisit the use of properties later in this guide.
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:
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
and use that throughout our POM like so:
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
You can now run the jar with the following command:
java -jar target/my-app-1.0-SNAPSHOT-jar-with-dependencies.jar
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.
Want to be at the top of the search ranks? How about a website that’ll give your audience a great experience? Or maybe you’re looking for a campaign that’ll drive more leads? Get in touch to find out how we can help.