Build Automation with Maven


CodeBooks

Maven is a build automation tool, designed to make creating complex projects more about writing code and less about managing the project itself - updating dependencies, running tests, etc. Maven will take care of all of this for you, and allow you to focus more on the code you write.

Maven Basics

Getting Maven

Maven is provided for free by the Apache Foundation here. Installation instructions are here.

The POM File

POM stands for Project-Object Model, and it's how Maven stores all of the data about your project. When you create a project with Maven, there will be a file called pom.xml in the project root. This is your POM.

All of the features available in your POM are listed here. I'll cover the few most commonly-used parts in this book.

Maven Central

Central (aka Maven Central) is a massive repository of over 120,000 projects (at the time of this writing) all built with Maven. These projects are freely available as dependencies, downloadable through Maven.

Artifacts

Artifacts are your finished products. They are the output of a build, and represent all the user needs to get. The most common artifact for Java projects is the project's compiled .jar file, but there are other types as well. All projects found on Maven Central also have a compiled POM file, a source archive, and a javadoc archive in addition to the main jar.

GAV (Group, Artifact, Version)

A GAV represents the 'coordinates' of an artifact in Maven Central. GAV stands for groupId, artifactId, and version. With these three values, all of the artifacts for a particular version of a project - i.e. everything Maven has to download - can be located in Central.

GAVs are often represented as groupId:artifactId:version. The syntax highlighting is a custom feature, but note how the parts are separated by colons. This is the most concise and common way of representing an artifact. (There is also the uncommon GAVP, adding a packaging parameter, in the format groupId:artifactId:version:packaging. However, this is very rarely used.)

groupId is a string that represents a unique author, separated by periods, in least- to most-specific order. The recommended practice is to use a website you own, as that will have no multiplicity. However, TLDs are the least specific part of a URL, so you have to reverse it. (As an example, my groupId is io.github.endreman0.)

artifactId is a representation of your project, and must be unique among all of the projects you own. For example, io.github.endreman0:java-json and io.github.digi755:java-json would not conflict, but two of my projects with artifactId java-json would.

version is the version number of your artifact(s). This should be unique per version, but again, doesn't have to be unique across all of your or everyone's projects. io.github.endreman0:java-json:0.1.0 doesn't conflict with io.github.endreman0:calculator:0.1.0 or io.github.digi755:java-json:0.1.0.

packaging (rare) selects the type of artifact to download. Example values could be jar (for the compiled jar), source (the source code archive), javadoc (Javadoc), or pom (compiled POM file). The same rules as above apply: io.github.endreman0:java-json:0.1.0:jar and io.github.endreman0:java-json:0.1.0:jar conflict, but io.github.endreman0:java-json:0.1.0:source, io.github.endreman0:java-json:0.1.1:jar, io.github.endreman0:calculator:0.1.0:jar, and io.github.digi755:java-json:0.1.0:jar do not.

Dependency Management

Maven will automatically manage your dependencies for you - download all of the dependency's artifacts, alert you when new versions are available, and package the dependencies into your final artifact.

What are dependencies? Why would you use them?

Dependencies are artifacts that your code depends on, usually libraries like java-json.

Dependencies exist because when you need to do a calculation/process in your code, there's a good chance that someone else has had that need as well and has already created a solution in the form of a library. If you're lucky, they then would have uploaded the library to Maven Central, at which point you can use it anywhere. Now you don't have to solve that problem, and can move on. For example, why write your own JSON parser when you can use java-json? If all you need JSON for is to, say, parse a settings file, why spend days on a JSON parser when you can use a prebuilt one?

How do I get Maven to manage my dependencies?

You need to edit your POM, and add a <dependencies> element. Inside this element, you will need one <dependency> element for each dependency, each containing a <groupId>, <artifactId>, and <version>. For example, here's a section of my Pokedex POM:


        

After configuring your dependencies, the next time you run Maven, all of the dependencies you have listed will be downloaded for you.