Maven Commands

[Last Updated: Nov 3, 2018]

General syntax of a maven command is:

mvn [options] <One or more Phases OR Goal Plugins[along with plugin options], in any order>

'mvn' invokes mvn.bat which is located in maven installed bin directory. The is the only part which is mandatory to start maven tasks etc. Another necessary thing is, you have to be in the project directory with a valid pom.xml

  1. The syntax for executing a phase: mvn [phase-name]
  2. The syntax for executing a plugin goal: mvn [plugin-name]:[goal-name]

Maven help:describe command

The help:describe plugin tool is very helpful to list information of our current Maven project. Go to a maven project root directory where we have pom.xml (like the one we created in the previous tutorials) and execute this:

D:\maven-first-example> mvn help:describe -Dcmd= compile
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-first-example 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] --- maven-help-plugin:2.2:describe (default-cli) @ maven-first-example ---
[INFO] 'compile' is a phase corresponding to this plugin:
It is a part of the lifecycle for the POM packaging 'jar'. This lifecycle includes the following phases:
* validate: Not defined
* initialize: Not defined
* generate-sources: Not defined
* process-sources: Not defined
* generate-resources: Not defined
* process-resources: org.apache.maven.plugins:maven-resources-plugin:2.5:resources
* compile: org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile
* process-classes: Not defined
* generate-test-sources: Not defined
* process-test-sources: Not defined
* generate-test-resources: Not defined
* process-test-resources: org.apache.maven.plugins:maven-resources-plugin:2.5:testResources
* test-compile: org.apache.maven.plugins:maven-compiler-plugin:2.3.2:testCompile
* process-test-classes: Not defined
* test: org.apache.maven.plugins:maven-surefire-plugin:2.10:test
* prepare-package: Not defined
* package: org.apache.maven.plugins:maven-jar-plugin:2.3.2:jar
* pre-integration-test: Not defined
* integration-test: Not defined
* post-integration-test: Not defined
* verify: Not defined
* install: org.apache.maven.plugins:maven-install-plugin:2.3.1:install
* deploy: org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.542s
[INFO] Finished at: Thu Oct 29 22:58:33 CDT 2015
[INFO] Final Memory: 8M/85M
[INFO] ------------------------------------------------------------------------

Here we have to understand following points

  • validate: Not defined means, phase validate is the first phase in execution sequence with no goals bound to it. Generally speaking the format here is phase:pluginName-goal (e.g. compile: org. apache. maven. plugins:maven-compiler-plugin:2. 3. 2:compile). To figure out a valid pluginName please see the next section.
  • What life cycle the above tool just described? Answer is default because the option part -Dcmd=compile belongs to default lifecycle.
  • We can also specify a goal instead of phase, e.g. -Dcmd=compiler:compile

About Plugin Naming convention

The naming convention for a plugin name is maven-<plugin_name>-plugin if group id is org.apache.maven.plugins otherwise it is <plugin_name>-maven-plugin. The first one is the reserved naming pattern for official apache maven plugins maintained by the apache maven team with groupId org.apache.maven.plugins.
The syntax for using a plugin goal is [plugin_name]:[goal_name]. For example from above output, look at the following line closely
*test: org.apache.maven.plugins:maven-surefire-plugin:2.10:test
We can see this is exactly per naming convention of phase_name: plugin_group-id:plugin_qualified_name:version:plugin_goal_name. From this kind of output, we can easily figure out the plugin goal syntax. In this case, that would be: surefire:test

How to list all Goals in a plugin?

By using -Dplugin=[plugin_name] option of describe mojo. For example mvn help:describe -Dplugin=compiler

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-first-example 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] --- maven-help-plugin:2.2:describe (default-cli) @ maven-first-example ---
[INFO] org.apache.maven.plugins:maven-compiler-plugin:3.3
Name: Apache Maven Compiler Plugin
Description: The Compiler Plugin is used to compile the sources of your
Group Id: org.apache.maven.plugins
Artifact Id: maven-compiler-plugin
Version: 3.3
Goal Prefix: compiler
This plugin has 3 goals:
  Description: Compiles application sources
  Description: Display help information on maven-compiler-plugin.
    Call mvn compiler:help -Ddetail=true -Dgoal=<goal-name> to display
    parameter details.
  Description: Compiles application test sources.
For more information, run 'mvn help:describe [...] -Ddetail'
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.909s
[INFO] Finished at: Sat Oct 31 14:34:28 CDT 2015
[INFO] Final Memory: 10M/85M
[INFO] ------------------------------------------------------------------------

Frequently used Commands

Here are some frequently used commands. If you are new to maven, you should try each command to get familiar with them.

Command Description Task details
mvn clean Invoking clean phase of Clean LifeCycle Removes the files from a project's working directory, that were generated at build-time. Alternatively we can execute the plugin: mvn clean:clean That will do the same thing.
mvn compile Invoking compile phase of Default Lifecycle Compiles the source code of the projects. This command will do all pre compile phases which are validate, initialize, generate-sources, process-sources, generate-resources, process-resources, and finally compile. Remember execution falls from start to the phase we are invoking. That makes sense because for a phase to work it's prerequisite state must be achieved.
One important point here: by default executing this phase will download external dependencies from remote repository. In fact any first plugin in execution sequence annotated with @requiresDependencyResolution will cause that (we will see that annotation in action when we learn how to write our own plugins)
Again we can do the same thing as: mvn compiler:compile
mvn clean package Invoking clean phase of Clean Lifecycle followed by package phase of Default Lifecycle. Cleans the target directory first then compiles, runs unit tests and packages the compiled code and other files into a single file. The type of final file being created depends on what we have defined in <packaging> in our pom.xml. the valid packaging values are jar, war, ear and pom
mvn clean install Invoking clean phase of Clean Lifecycle followed by install phase of Default Lifecycle. Cleans the previous build, then packages and then install the package into the local repository. Which by default is at User Home Directory/.m2/repository/. The local repository is a cache of the remote downloads(dependencies), and locally installed artifacts) which can be used as a dependency in other local projects. Our locally installed artifacts are actually temporary ones until we release that and put into some remote repository.
mvn test Invoking test phase of Default Lifecycle. Runs the unit test from compiled source and test source files. This is one step above package phase. If we are running some post test phase and want to skip tests then use skip optionObject.
For example mvn install -Dmaven.test.skip=true
mvn dependency:list Invoking list goal of dependency plugin (a tool, not bound to any phase by default) Lists all Maven dependencies from our project. Please use
mvn help:describe -Dplugin=dependency
to see other goals available with this plugin.
From help:describe you can discover more goals of a plugin, then to know more about a particular goal (let's say 'get' goal of 'dependency' plugin), you can use:
mvn help:describe -Dcmd= dependency:get
mvn help:effective-pom Invoking effective-pom goal of help plugin(tool) Displays the effective POM as an XML for this build. This is also helpful to know what currently plugins (either bound to a phase or tool plugins) are available/installed. Please invoke mvn help:describe -Dplugin= help to see all goals available with help plugin.

About Maven build Errors

If running a mvn command shows a BUILD FAILURE message, just about the end of output then some goal must have quit with an Error based on some invalid condition. That will terminate the phase sequence execution at that point. In that case you should look for [ERROR] and try to resolve the errors. One of the commonly occurring errors is compilation error, happens during compile phase. In case if error doesn't provide enough information, you can try -X flag (which provides some extra DEBUG information). For example mvn -X compile
If all phases are finished successfully then we should see BUILD SUCCESS message.

See Also