Maven cheat sheet

Nicol Leung
2 min readMay 28, 2018

Maven is a build automation tool used primarily for Java projects. Maven addresses two aspects of building software: first, it describes how software is built, and second, it describes its dependencies.

Maven life cycle

validate — ???
compile — just compile source code to class file
test — run unit tests
package — package classes into jar and copy dependencies
verify — ???
install — install package to local repository
deploy — deploy package to remote repository

To run a life cycle, go to project root and run

mvn deploy

When you run any of the life cycle listed above, all the previous life cycle will also be run in sequence.

Special life cycle

clean — Clean build folder

This is run separately. To clean and package run

mvn clean package

View dependency tree

mvn dependency:tree

Copy dependencies to target/lib

Notice that testing related jars are excluded by <includeScope>compile</includeScope> .

  <properties>
<mainClass>com.example.package1.Main</mainClass>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeScope>compile</includeScope>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>${mainClass}</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

Package all dependencies into a single jar

  <properties>
<mainClass>com.example.package1.Main</mainClass>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>${mainClass}</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- this is used for inheritance merges -->
<phase>package</phase>
<!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Specify Java version

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>1.8</java.version>
</properties>

Skip testing

mvn package -Dmaven.test.skip=true

Version conflict of a dependency

The first appearance of the dependency in dependency tree will define the version.

Install or deploy existing jar without source code

Install a jar to local repository

mvn install:install-file -DgroupId=oracle -DartifactId=xdb6 -Dversion=11.2.0.4 -Dpackaging=jar -Dfile=Downloads\xdb6.jar -DgeneratePom=true

Deploy a jar to Nexus

mvn deploy:deploy-file -DgroupId=oracle -DartifactId=xdb6 -Dversion=11.2.0.4 -Dpackaging=jar -Dfile=Downloads\xdb6.jar -DgeneratePom=true -Durl=http://nexus.office.zensis.com:8081

Multiple modules

A parent project can be created to contain multiple sub-modules.

Multiple modules may be sharing a lot of dependencies. To ensure all modules using the same version of a dependency, dependencyManagement can be used.

Set the default version of a dependency in parent pom.xml

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.4.2</version>
</dependency>
</dependencies>
</dependencyManagement>

In child pom.xml, you can omit the dependency version without getting errors

<parent>
<groupId>com.example</groupId>
<artifactId>project-parent</artifactId>
<version>0.0.10</version>
</parent>
...
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
</dependency>
</dependencies>

To build all modules at once when building parent project

In parent pom.xml

<modules>
<module>example-module</module>
</modules>

In child pom.xml you can omit the version of the module and inherit it from the parent.

To compile child you should install or deploy the parent project first without building child projects.

mvn -N deploy

--

--