Ask Sawal

Discussion Forum
Notification Icon1
Write Answer Icon
Add Question Icon

Kashinath Basit




Posted Questions



Wait...

Posted Answers



Answer


Maven is one of the most popular project and dependency management tools for Java applications. Maven provides a lot of commands and options to help you in your day to day tasks.

This cheat sheet uses a sample Maven project to demonstrate some useful Maven commands. It was originally written for OpenJDK 13.0.1 and Maven 3.6.3. These commands have been verified with OpenJDK 19.0.1 and Maven 3.8.7.

This command cleans the Maven project by deleting the target directory:

Example of the output:

This command compiles the Java source classes of the Maven project:

Example of the output:

This command compiles the test classes of the Maven project:

Example of the output:

This command builds the Maven project and packages it into a JAR, WAR, etc.:

Example of the output:

The output shows the location of the JAR file just before the "BUILD SUCCESS" message. Notice the package goal executes compile, testCompile, and test goals before packaging the build.

This command builds the Maven project and installs the project files (JAR, WAR, pom.xml, etc.) to the local repository:

Example of the output:

This command deploys the artifact to the remote repository:

The remote repository should be configured properly in the project pom.xml file distributionManagement tag. The server entries in the Maven settings.xml file are used to provide authentication details.

This command validates the Maven project to ensure that everything is correct and all the necessary information is available:

This command generates the dependency tree of the Maven project:

Example of the output:

This command analyzes the maven project to identify the unused declared and used undeclared dependencies:

Example of the output:

It’s useful in reducing the build size by identifying the unused dependencies and removing them from the pom.xml file.

This command generates skeleton Maven projects of different types, such as JAR, web application, Maven site, etc:

Example of the output:

Recommended Reading: Creating a Java Project using Maven Archetypes

This command generates a site for the project:

You will notice a site directory in the target directory after executing this command.

There will be multiple HTML files inside the site directory that provide information related to the project.

This command runs the test cases of the project:

Example of the output:

This command compiles the source Java classes of the project:

Example of the output:

It is similar to the previous mvn compiler:compile command, but runs the entire Maven lifecycle up to compile.

This command builds the project, runs all the test cases and run any checks on the results of the integration tests to ensure quality criteria are met:

Maven provides a lot of command-line options to alter the Maven build process:

This command-line option prints the Maven usage and all the available options:

Example of the output:

This command-line option builds a project from a different location:

Provide the pom.xml file location to build the project. It’s useful when you have to run a Maven build from a script.

This command-line option runs the Maven build in offline mode:

It’s useful when you have all the required JARs downloaded in the local repository and you don’t want Maven to look for any JARs in the remote repository.

This command-line option runs the Maven build in quiet mode, so that only the test case results and errors are displayed:

This command-line option prints the Maven version and runs the build in debug mode, so that all messages are displayed:

Example of the output:

This command-line option displays the Maven version information:

Example of the output:

This command-line option prints the Maven version and then continues with the build:

It’s equivalent to the commands:

This command-line option applies the skipTests system property to skip the unit test cases from the build cycle:

You can also skip the test cases execution:

This command-line option tells Maven to run parallel builds using the specified thread count:

It’s useful in multiple module projects where modules can be built in parallel. It can reduce the build time of the project.


Answer is posted for the following question.

Mvn run command?

Answer


Best&Less

Address: Howtree Pl, Floreat WA 6014, Australia


Answer is posted for the following question.

Where is the best and less in Perth, Australia?


Wait...