Maven Commands Cheat Sheet
I’ve put together some Maven commands, properties and command line options.
This command cleans the maven project by deleting the target directory. The command output relevant messages are shown below. INFO - maven-clean-plugin:2.5:clean (default-clean) @ maven-example-jar - INFO Deleting /Users/pankaj/Desktop/maven-examples/maven-example-jar/target. Red Hat Developer cheat sheets 🌟 Browse through our collection of cheat sheets to help you develop with Red Hat products, which you can download for free as a Red Hat Developer member. You’ll find handy guides on a range of the latest developer tools and technologies, including Kubernetes, microservices, containers, and more. Here is an example to execute the dependency tree goal from the command line. It’s not part of any build phases. Mvn dependency:tree Role of POM in Build Lifecycle. The maven builds executed through the command line runs a set of phases and goals. The pom.xml configuration plays a major role in setting up the project build lifecycle. Cheat sheet Getting started with Maven s rg! Useful command line options-DskipTests=true compiles the tests, but skips running them-Dmaven.test.skip=true skips compiling the tests and does not run them-T - number of threads: -T 4 is a decent default-T 2C - 2 threads per CPU -rf, -resume-from resume build from the specified project. Red Hat Developer cheat sheets 🌟 Browse through our collection of cheat sheets to help you develop with Red Hat products, which you can download for free as a Red Hat Developer member. You’ll find handy guides on a range of the latest developer tools and technologies, including Kubernetes, microservices, containers, and more.
Creating a project (jar):
Creating a project (war):
Setting the source code encoding:
Setting the Java version:
Default lifecycle
The default
lifecycle consist of the following phases:
Phase | Description |
---|---|
validate | Validate the project, checking if everything is correct. |
compile | Compile source code of the project and store classes in target/classes . |
test | Run the tests. |
package | Take the compiled code and package it in its distributable format, sucg as JAR or WAR. |
verify | Run any checks to verify the package is valid and meets quality criteria. |
install | Install the package into the local repository, or use as a dependency in other projects locally. |
deploy | Copies the final package to the remote repository. |
Use mvn <phase name>
to execute a phase.
Useful command line options
Command line option | Description |
---|---|
-DskipTests | Compile the tests but don’t run them |
-Dmaven.test.skip=true | Don’t compile the tests and don’t run them |
-X or --debug | Enable debug output |
-U or --update-snapshots | Forces a check for updated releases and snapshots on remote repositories |
-o or --offline | Work 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 --version | Display Maven version |
Maven Commands Cheat Sheet Cheat
Useful properties
Project properties
Property | Description |
---|---|
${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
Property | Description |
---|---|
${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
Property | Description |
---|---|
${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 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 intarget/site/apidocs
.
Resources
A quick reference to Maven commands.
mvn -Dtest=<unqualified-classname> test
Tests only the specified class
mvn -Dmaven.surefire.debug test
Enables remote debugging of tests on port 5005. Surefire will block on the port until you connect with your debugger.
mvn -Dcargo.wait=true -P int integration-test
Runs CS in Tomcat via cargo
mvn help:effective-pom
Shows the logical contents of a pom.xml, including contents inherited from the parent pom.xml, up to and including the Maven super POM.
mvn dependency:tree
Shows all dependencies (including transitive dependencies) of your project. This is very helpful for debugging dependency version issues.
mvn -X <package-name>
Shows all explicit and transitive dependencies for a package, helping to identify conflicts
mvn dependency:sources
Downloads all project sources separate from IDE project creation. Execute from root of parent project, then have your IDE synch up sources.
Not Currently Available to Customers:
mvn -Dupgrade.version=<version you're upgrading to> com.jivesoftware.maven:maven-upgrade-plugin:upgrade
Will generate a list of diff files based on product changes that correspond to your overlays. Also generates a mirrored directory structure with product files that enables you to diff against your project's web directory
Note: requires SVN access; a version is in development to use Maven dependencies instead.
Maven Commands Cheat Sheet 5e
mvn deploy:deploy-file -DgroupId=<package> -DartifactId=<artifact-name> -Dversion=<version-no> -Dpackaging=jar -Dfile=/path/to/file.jar -Durl=https://maven-secure.jiveland.com/archiva/repository/jive.snapshots -DrepositoryId=secure.jive.snapshots
Maven Commands Cheat Sheet 2020
In this example, the command deploys a jar artifact to our repository. This command working depends on your having adequate permissions.