Programming

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 \
    -DartifactId=application

Creating a project packed as war:

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

Setting the source code encoding:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

Setting the Java version:

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

Default lifecycle #

The default lifecycle consist of the following phases:

PhaseDescription
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 #

PropertyDescription
${project.groupId}Project’s group id
${project.artifactId}Project’s artifact id
${project.version}Project’s version
${project.name}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.build.sourceDirectory}Project source directory
${project.build.testSourceDirectory}Project test source directory
${project.build.outputDirectory}Project output directory
${project.build.testOutputDirectory}Project test output directory
${project.build.directory}Directory which contains all of these output directories (target)
${project.build.finalName}Final name of the file created when the built project is packaged

Build time properties #

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

Java system properties #

PropertyDescription
${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
${os.name}Operating system name
${os.arch}Operating system architecture
${os.version}Operating system version
${user.name}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 #