Maven cheat sheet

I’ve put together some Maven commands, properties and command line options.

Creating a project packed as jar:

mvn archetype:generate \
    -DgroupId=com.example.project \

Creating a project packed as war:

mvn archetype:generate \
    -DgroupId=com.example.project \
    -DartifactId=application \

Setting the source code encoding:


Setting the Java version:


Default lifecycle #

The default lifecycle consist of the following phases:

validateValidate the project, checking if everything is correct.
compileCompile source code of the project and store classes in target/classes.
testRun the tests.
packageTake the compiled code and package it in its distributable format, sucg as JAR or WAR.
verifyRun any checks to verify the package is valid and meets quality criteria.
installInstall the package into the local repository, or use as a dependency in other projects locally.
deployCopies the final package to the remote repository.

Use mvn <phase name> to execute a phase.

Useful command line options #

Command line optionDescription
-DskipTestsCompile the tests but don’t run them
-Dmaven.test.skip=trueDon’t compile the tests and don’t run them
-X or --debugEnable debug output
-U or --update-snapshotsForces a check for updated releases and snapshots on remote repositories
-o or --offlineWork offline (run as if no network connection is available)
-l <file path> or --log-file <file path>Writes the build output to a file
-v or --versionDisplay Maven version

Useful properties #

Project properties #

${project.groupId}Project’s group id
${project.artifactId}Project’s artifact id
${project.version}Project’s version
${}Project’s name
${project.description}Project’s description
${project.basedir}Directory containing the pom.xml file
${project.baseUri}Directory containing the pom.xml file as URI
${}Project source directory
${}Project test source directory
${}Project output directory
${}Project test output directory
${}Directory which contains all of these output directories (target)
${}Final name of the file created when the built project is packaged

Build time properties #

${}The UTC timestamp of build start, in yyyy-MM-dd'T'HH:mm:ss'Z' default format
${build.timestamp}Same as ${}
${}Can be used to override the default format for the build timestamp

Java system properties #

${java.class.path}Java class path
${java.home}Java installation directory
${java.vendor}Java Runtime Environment vendor
${java.version}Java Runtime Environment version
${line.separator}Line separator
${file.separator}File separator
${}Operating system name
${os.arch}Operating system architecture
${os.version}Operating system version
${}User’s account name
${user.dir}User’s current working directory
${user.home}User’s home directory

Useful plugins #

  • Dependency plugin: Provides the capability to manipulate artifacts.

    • mvn dependency:tree: Show the dependency tree.
    • mvn dependency:resolve: Resolve dependencies.
    • mvn dependency:resolve-plugins: Resolve plugins.
  • Versions plugin: Manage the versions of artifacts in a project’s POM.

    • mvn versions:set -DgenerateBackupPoms=false -DnewVersion=[new version]: Set a new project version.
  • Assembly plugin: Aggregate the project output along with its dependencies, modules and other files into a single distributable archive.

  • Release plugin: Plugin used to release a project.

  • Shade plugin: Package the artifact in an uber-jar, including its dependencies.

  • Javadoc plugin: Generate javadocs for a project.

    • mvn javadoc:javadoc: Output javadocs HTML files in target/site/apidocs.

Resources #