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.
Matching infrastructure at centron
Node applications need somewhere to run: ccloud³ VMs with full root access from €3.12 per month – or as a managed server if you would rather not run it yourself. Rent a cloud server →
Maven Commands
1. `mvn clean`
With this command, you can clean the Maven project by deleting the `target` directory:
mvn clean2. `mvn compiler:compile`
This command compiles the Java source classes of the Maven project:
mvn compiler:compile3. `mvn compiler:testCompile`
This command compiles the test classes of the Maven project:
mvn compiler:testCompile4. `mvn package`
This command builds the Maven project and packages it into a JAR, WAR, etc.:
mvn package5. `mvn install`
This command builds the Maven project and installs the project files (JAR, WAR, pom.xml, etc.) to the local repository:
mvn install6. `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 deploy7. `mvn validate`
This command validates the Maven project to ensure that everything is correct and all the necessary information is available:
mvn validate8. `mvn dependency:tree`
Generates the dependency tree of the Maven project:
mvn dependency:tree9. `mvn dependency:analyze`
This command analyzes the Maven project to identify unused declared and used undeclared dependencies:
mvn dependency:analyze10. `mvn archetype:generate`
Generates skeleton Maven projects of different types, such as JAR, web application, Maven site, etc.:
<
mvn archetype:generate11. `mvn site:site`
Generates a site for the project:
mvn site:site12. `mvn test`
This command runs the test cases of the project:
mvn test13. `mvn compile<`
Compiles the source Java classes of the project:
mvn compile14. `mvn verify`
Builds the project, runs all the test cases, and checks the results of integration tests to ensure quality criteria are met:
mvn verify15. `mvn -help`
Displays the Maven command line and all available options:
mvn -help16. `mvn -f dir/pom.xml package`
Builds a project from a different location:
mvn -f dir/pom.xml package17. `mvn -o package`
Runs the Maven build in offline mode:
mvn -o package18. `mvn -q package`
mvn -q package19. `mvn -X package`
Displays the Maven version and runs the build in debug mode, showing all messages:
mvn -X package20. `mvn -v`
Displays the Maven version information:
mvn -v21. `mvn -V package`
Displays the Maven version and continues with the build:
mvn -V package22. `mvn -DskipTests package`
Skips the execution of unit tests during the build:
mvn -DskipTests package23. `mvn -Dmaven.test.skip=true package`
Also skips the execution of test cases:
mvn -Dmaven.test.skip=true package24. `mvn -T 4 clean install`
Executes the build with parallel threads to reduce build time:
mvn -T 4 clean installMaven Options
Maven also provides a variety of command-line options to customize the build process:
Testen Sie Ihr Setup auf ccloud³
Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.