Skip to content

Instantly share code, notes, and snippets.

@jahe
Last active January 29, 2021 18:50
Show Gist options
  • Save jahe/d4f1f2a42b5f28432a30364028e1ecf8 to your computer and use it in GitHub Desktop.
Save jahe/d4f1f2a42b5f28432a30364028e1ecf8 to your computer and use it in GitHub Desktop.
Maven Cheatsheet

Maven Cheatsheet

Show unused dependencies

mvn dependency:analyze

Show infos to one specific plugin (available goals with phases, etc.)

mvn help:describe -Dplugin=org.sonarsource.scanner.maven:sonar-maven-plugin -Ddetail

Show root of transitive dependency

mvn dependency:tree -Dincludes=org.apache.activemq:activemq-broker:jar:5.13.4 -Dverbose

Default Properties that can be used in your pom.xml

  • ${project.basedir} - This references to the root folder of the module/project (the location where the current pom.xml file is located)
  • ${project.build.directory} - This represents by default the target folder.
  • ${project.build.outputDirectory} - This represents by default the target/classes folder.
  • ${project.build.testOutputDirectory} - This represents by default the target/test-classes folder.
  • ${project.build.sourceDirectory} - This represents by default the src/main/java folder.
  • ${project.build.testSourceDirectory} - This represents by default the src/test/java folder.
  • ${project.build.finalName} - This is by default defined as ${project.artifactId}-${project.version}.
  • ${project.version} - This can be used at locations where you have to write a literal version otherwise, in particular if you are in a multi-modules build for inter modules dependencies.
  • ${settings.localRepository} - which references the location of the local repository. This is by default ${home}/.m2/repository.

<dependencies> vs. <dependencyManagement>

Those two elements are very much similar to object-oriented programming paradigm:

The <dependencyManagement> section only declares the dependencies and their details in the current project - the purpose is management of the details and re-use in other projects, either via inheritance (parent) or import (scope). This is like declaring a data type in program and make it available for use.

The <dependency> section defines the actual use of the dependencies in the project, optionally inherit the details (i.e., version, etc.) of the dependencies declared under the <dependencyManagment>. That's why you will have missing dependencies if you only put them in <dependencyManagement>. This is analogous to instantiating an variable instance of a data type in a program where it is needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment