Maven Commands Cheat Sheet



General syntax of a maven command is:

  1. Maven Cheat Sheet Knowing Maven is a must-have skill for any respected Java developer. Unfortunately, memorizing the command line options and phases can be tough.
  2. Maven cheat sheet Author: Bonune Cigifo Subject: Maven cheat sheet. I've put together a few Maven commands, properties, and command line options. Creation of the Projec Created Date: 3/4/2020 4:04:06 AM.
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

Introduction to Maven Commands. Maven is a software project management and comprehension tool which was developed by Apache.It was initially released in July 2004. It is basically used to build projects written in C#, Ruby, Scala and other languages.

  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> mvnhelp:describe-Dcmd= compile
[INFO]
[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:
org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile
It is a part of the lifecycle for the POM packaging 'jar'. This lifecycle includes the following phases:
* initialize: Not defined
* process-sources: Not defined
* process-resources: org.apache.maven.plugins:maven-resources-plugin:2.5:resources
Maven Commands Cheat Sheet
* compile: org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile
* generate-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
* test: org.apache.maven.plugins:maven-surefire-plugin:2.10:test
* package: org.apache.maven.plugins:maven-jar-plugin:2.3.2:jar
* 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] Finished at: Thu Oct 29 22:58:33 CDT 2015
[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
Maven Commands Cheat Sheet

About Plugin Naming convention

Maven commands cheat sheet roblox

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 mvnhelp:describe-Dplugin=compiler

Output:
[INFO]
[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
Version: 3.3
Description: Compiles application sources
compiler:help
Description: Display help information on maven-compiler-plugin.
Call mvn compiler:help -Ddetail=true -Dgoal=<goal-name> to display
Description: Compiles application test sources.
For more information, run 'mvn help:describe [...] -Ddetail'
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Finished at: Sat Oct 31 14:34:28 CDT 2015
Maven Commands Cheat Sheet
[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.

CommandDescriptionTask details
mvncleanInvoking clean phase of Clean LifeCycleRemoves the files from a project's working directory, that were generated at build-time. Alternatively we can execute the plugin: mvnclean:clean That will do the same thing.
mvncompileInvoking compile phase of Default LifecycleCompiles 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: mvncompiler:compile
mvnclean packageInvoking 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
mvnclean installInvoking 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.
mvntestInvoking 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 mvninstall-Dmaven.test.skip=true
mvndependency:listInvoking list goal of dependency plugin (a tool, not bound to any phase by default) Lists all Maven dependencies from our project. Please use
mvnhelp: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:
mvnhelp:describe-Dcmd=dependency:get
mvnhelp:effective-pomInvoking 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 mvnhelp:describe-Dplugin=help to see all goals available with help plugin.

About Maven build Errors

Cheat

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-Xcompile
If all phases are finished successfully then we should see BUILD SUCCESS message.

Gradle is a build automation tool for java and android projects. Developer working on java projects know about gradle command use. This tutorial covers list of commands used by developer for dailu usage in projects

Please have a look of my previous article maven installation. For the maven installation, the JDK is required.

Gradle commands list

You can use either gradlew or gradle.bat file for command execution

Compile gradle project

It will compile the project java files

list tasks of a gradle build

all tasks can be listed for build file using below command

It will display all tasks related the build file in a project

Maven Commands Cheat Sheet

Running spring boot project with gradle

In spring boot project, once gradle plugin is configured, Application is build using below command

How do you make jar file for spring boot application

Here is the command for creating war file for the same

Maven Commands Cheat Sheet Download

Spring boot application can be started using bootRun command

Maven Commands Cheat Sheet 2020

This will compile, copy and run the spring boot server, It is not required to build and compile the application.