not logged in | [Login]

Maven Cheat Sheet and Tips

Clean, Build

This will build, run tests, and install to the local repository

mvn clean install

Build but skip tests

This will skip compiling and running the tests

mvn clean install -Dmaven.test.skip=true

This will still compile, but not run the tests

mvn clean install -DskipTests

Run a single integration test

mvn -Dit.test=YourIntegrationTest verify

Purge local repository

Use the Purge Local Repository Plugin.

mvn dependency:purge-local-repository

You've might of tried to use the -U option. Beware, there are issues with -U https://issues.apache.org/jira/browse/MNG-4142

Dependency tree

maven-dependency-plugin

mvn dependency:tree

# The verbose flag instructs the dependency tree to display conflicting
# dependencies that were omitted from the resolved dependency tree.
mvn dependency:tree -Dverbose -Dincludes=org.apache.*

# Debug logging
mvn dependency:tree -Dverbose -X

Also see this nice web UI: https://app.updateimpact.com/treeof/io.dropwizard/dropwizard-core/1.0.0

Find upgradable dependencies

Use the Maven Versions Plugin.

mvn versions:display-dependency-updates

Also see this article.

Find unused dependencies

Use the Maven Dependency Plugin

mvn dependency:analyze -DignoreNonCompile=true

Sort/clean up pom.xml

mvn com.github.ekryd.sortpom:sortpom-maven-plugin:2.8.0:sort -Dsort.sortDependencies=scope,groupId,artifactId -Dsort.nrOfIndentSpace=2 -Dsort.keepBlankLines=true -Dsort.expandEmptyElements=false

or the official maven style:

mvn com.github.ekryd.sortpom:sortpom-maven-plugin:2.8.0:sort -Dsort.expandEmptyElements=false -Dsort.predefinedSortOrder=recommended_2008_06

Download Eclipse sources

mvn eclipse:eclipse -DdownloadSources=true

Download an artifact from the command line

mvn org.apache.maven.plugins:maven-dependency-plugin:2.1:get -Dartifact=groupId:artifactId:version -DrepoUrl=url

A list of predefined Maven properties and built-ins

See https://github.com/cko/predefined_maven_properties/blob/master/README.md

What are SNAPSHOTS anyways?

Source: http://stackoverflow.com/questions/5901378/what-exactly-is-a-maven-snapshot-and-why-do-we-need-it

When you build an application, Maven will search for dependencies in the local repository. If a stable version is not found there, it will search the remote repositories (defined in settings.xml or pom.xml) to retrieve this dependency. Then, it will copy it into the local repository, to make it available for the next builds.

For example, a foo-1.0.jar library is considered as a stable version, and if Maven finds it in the local repository, it will use this one for the current build.

Now, if you need a foo-1.0-SNAPSHOT.jar library, Maven will know that this version is not stable and is subject to changes. That's why Maven will try to find a newer version in the remote repositories, even if a version of this library is found on the local repository. However, this check is made only once per day. That means that if you have a foo-1.0-20110506.110000-1.jar (i.e. this library has been generated on 2011/05/06 at 11:00:00) in your local repository, and if you run the Maven build again the same day, Maven will not check the repositories for a newer version.

Maven provides you a way to can change this update policy in your repository definition:

<repository>
    <id>foo-repository</id>
    <url>...</url>
    <snapshots>
        <enabled>true</enabled>
        <updatePolicy>XXX</updatePolicy>
    </snapshots>
</repository>
where XXX can be:
  • always: Maven will check for a newer version on every build;
  • daily, the default value;
  • interval:XXX: an interval in minutes (XXX)
  • never: Maven will never try to retrieve another version. It will do that only if it doesn't exist locally. With the configuration, SNAPSHOT version will be handled as the stable libraries.

A model of the settings.xml can be found here.