Efficient Working with Maven: 24 Commands and Options Overview

Introduction

Maven is one of the most popular tools for project and dependency management in Java applications. It provides a variety of commands and options that can assist you in your daily tasks. In this cheat sheet, we use a sample project to demonstrate some useful Maven commands. 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.

Maven Commands

1. `mvn clean`

With this command, you can clean the Maven project by deleting the `target` directory:


2. `mvn compiler:compile`

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


3. `mvn compiler:testCompile`

This command compiles the test classes of the Maven project:


4. `mvn package`

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


5. `mvn install`

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


6. `mvn deploy`

This command deploys the artifact to the remote repository. This requires proper configuration of the remote repository in the project’s `code>pom.xml` file and providing authentication details in Maven’s `settings.xml` file.


7. `mvn validate`

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


8. `mvn dependency:tree`

Generates the dependency tree of the Maven project:


9. `mvn dependency:analyze`

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


10. `mvn archetype:generate`

Generates skeleton Maven projects of different types, such as JAR, web application, Maven site, etc.:

<


11. `mvn site:site`

Generates a site for the project:


12. `mvn test`

This command runs the test cases of the project:


13. `mvn compile<`

Compiles the source Java classes of the project:


14. `mvn verify`

Builds the project, runs all the test cases, and checks the results of integration tests to ensure quality criteria are met:


15. `mvn -help`

Displays the Maven command line and all available options:


16. `mvn -f dir/pom.xml package`

Builds a project from a different location:


17. `mvn -o package`

Runs the Maven build in offline mode:


18. `mvn -q package`


19. `mvn -X package`

Displays the Maven version and runs the build in debug mode, showing all messages:


20. `mvn -v`

Displays the Maven version information:


21. `mvn -V package`

Displays the Maven version and continues with the build:


22. `mvn -DskipTests package`

Skips the execution of unit tests during the build:


23. `mvn -Dmaven.test.skip=true package`

Also skips the execution of test cases:


mvn -Dmaven.test.skip=true package


24. `mvn -T 4 clean install`

Executes the build with parallel threads to reduce build time:


Maven Options

Maven also provides a variety of command-line options to customize the build process:

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: