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:
mvn clean
2. `mvn compiler:compile`
This command compiles the Java source classes of the Maven project:
mvn compiler:compile
3. `mvn compiler:testCompile`
This command compiles the test classes of the Maven project:
mvn compiler:testCompile
4. `mvn package`
This command builds the Maven project and packages it into a JAR, WAR, etc.:
mvn package
5. `mvn install`
This command builds the Maven project and installs the project files (JAR, WAR, pom.xml, etc.) to the local repository:
mvn install
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.
mvn deploy
7. `mvn validate`
This command validates the Maven project to ensure that everything is correct and all the necessary information is available:
mvn validate
8. `mvn dependency:tree`
Generates the dependency tree of the Maven project:
mvn dependency:tree
9. `mvn dependency:analyze`
This command analyzes the Maven project to identify unused declared and used undeclared dependencies:
mvn dependency:analyze
10. `mvn archetype:generate`
Generates skeleton Maven projects of different types, such as JAR, web application, Maven site, etc.:
<
mvn archetype:generate
11. `mvn site:site`
Generates a site for the project:
mvn site:site
12. `mvn test`
This command runs the test cases of the project:
mvn test
13. `mvn compile<`
Compiles the source Java classes of the project:
mvn compile
14. `mvn verify`
Builds the project, runs all the test cases, and checks the results of integration tests to ensure quality criteria are met:
mvn verify
15. `mvn -help`
Displays the Maven command line and all available options:
mvn -help
16. `mvn -f dir/pom.xml package`
Builds a project from a different location:
mvn -f dir/pom.xml package
17. `mvn -o package`
Runs the Maven build in offline mode:
mvn -o package
18. `mvn -q package`
mvn -q package
19. `mvn -X package`
Displays the Maven version and runs the build in debug mode, showing all messages:
mvn -X package
20. `mvn -v`
Displays the Maven version information:
mvn -v
21. `mvn -V package`
Displays the Maven version and continues with the build:
mvn -V package
22. `mvn -DskipTests package`
Skips the execution of unit tests during the build:
mvn -DskipTests package
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:
mvn -T 4 clean install