Maven Basics

Coordinates

Maven coordinates are use to unqiuely identify maven artifacts, the below is used to create the corrdinate. The coordinate is used for share your projects and in the dependencies/dependency in the POM file.

The typical version breakdown could be below but this depends on what your company implements

The snapshot versions are important and we will look in depth later and you will see why, I will provide a link

Coordinates example
<groupId>uk.co.datadisk</groupId>
<artifactId>hello-world</artifactId>
<version>0.0.1-SNAPSHOT</version>

Repositories

Repositories are a location where your maven artifacts are stored, there are a number of different primary types


When you execute maven it will first look in the local repository (cached copy), then using the dependency it will then look either at Maven entral or a 3rd party repository. All older versions of any artifacts will also be stored unless you clear them down.

Local Repository Maven Central

Wagon

Maven Wagon is an API for transporting artifacts (to or from a repo), this allows for different providers to communicate with a Maven repository, HTTP/HTTPS (File, FTP,SSH/SCP, WebDAV and SCM) are generally used and you also have a proxy setup if your company uses proxies. The below shows you that one API is used for all communication (HTTP, HTTPS, SSH, etc)


Project Object Model (POM)

The POM file is pom.xml, its a XML file that complies with a schema which defines the rules, what elements can be used, etc. The POM also has inheritance capabilities (parent POM) and thus you only need to define properties once. You can show the complete POM file (including all the inheritance) using the command effective-pom.

Effective POM
mvn help:effective-pom

Dependencies

Dependencies are artifacts that your project requires to compile/run, there are three terms that you need to understand

Dependencies have scope with in a Maven project, also there are three classpaths (compile, run and test) the scopes are

Dependencies are handled by the Maven dependency plugin and there are a few commands you can use

dependency - tree
mvn dependency:tree
dependency - resolve all
mvn dependency:go-offline
dependency - clear artifacts from local repo
mvn dependency:purge-local-repository
dependency - get sources
mvn dependency:sources

Directory Layout

Maven uses a standard directory outlay, below is a Maven project created by Intellij, the src directory contains both the source code (main/java directory) for the application and test files (test/java). Java packages will be created in the src/main/java directory.

Intellij Maven standard directory layout

Build Lifecycles

Maven uses plugins to build an application this consists of phases and goals, lifecycles and phases are the framework which call plugin goals in a sequence.


Maven has a number of pre-defined lifecycles

To see the phases for a particular lifestyle you can use the help:describe command, you can see that in the below screenshot the clean lifecycle uses 3 phases, the pre-clean and post-clean are not binded to anything but the clean phase uses a plugin goal called clean.


Display phases of a lifecycle
mvn help:describe -Dcmd=clean

The default lifecycle consists of the following top-level goals, the commonly used

Intellij has a very handy tool that allow you to look and run lifecycles within your project, you can also see the plugins


There are many phases in the default lifecycle, the complete list is below


I will talk more about lifecycles in another section.

Wrapper

The Maven wrapper is a wrapper around Maven that allows you to distribute your Maven version with the project, to install use the below command, you use the mvnw command instead of the normal mvn command

Install Maven Wrapper
mvn -N io.takari:maven:wrapper
mvn -N io.takari:maven:wrapper -Dmaven=3.5.2                  ## specific version

Archetypes

A archetype is a predefined Maven project template, when you select a specific archetype it will setup the structure and create the neccessary files, there are many archetypes


Looking at the simple archetype you can see the structure and the files that will be created


If you want to create a specific archetype its best to either use an your IDE (see intellij below) or go to the Maven website documentation to retrieve the command, there are many archetypes available including very old ones.