General syntax of a maven command is:
- 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.
- 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.
- The syntax for executing a phase:
mvn[phase-name]
- 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:
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
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 |
---|---|---|
mvnclean | 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: mvnclean:clean That will do the same thing. |
mvncompile | 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: mvncompiler:compile |
mvnclean 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 |
mvnclean 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. |
mvntest | 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 mvninstall-Dmaven.test.skip=true |
mvndependency:list | Invoking list goal of dependency plugin (a tool, not bound to any phase by default) | Lists all Maven dependencies from our project. Please usemvn 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 |
mvnhelp: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 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-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.