# curl https://devtools.krishanchawla.com/raw/maven-commands.txt MAVEN - COMMANDS Essential Maven commands for building, testing, and managing Java projects Category: Maven · Type: cheatsheet Official docs: https://maven.apache.org/ ──────────────────────────────────────────────────────────── ## What is Maven? **Maven** is a powerful build automation and project management tool primarily used for Java projects. It manages dependencies, builds projects, and follows a standard directory structure with convention over configuration. **⚡ Key Features** - Dependency management - Standard project structure - Build lifecycle phases - Plugin-based architecture - Central repository (Maven Central) **🎯 Use Cases** - Java project builds - Dependency resolution - Multi-module projects - Automated testing - Artifact deployment --- ## 🏗️ Build - Clean Install Clean and build project with dependencies installed to local repository. ```bash mvn clean install ``` Most commonly used Maven command. Removes target directory, compiles, tests, and installs to `~/.m2/repository`. ## 📦 Build - Clean Package Clean and package project without installing to local repository. ```bash mvn clean package ``` Faster than `install` when you don't need artifacts in local repo. Creates JAR/WAR in `target/`. ## ⚙️ Build - Compile Only Compile source code without running tests. ```bash mvn compile ``` Compiles `src/main/java` to `target/classes`. Useful for quick syntax checks. ## ⏭️ Build - Skip Tests Build project while skipping test execution. ```bash mvn clean install -DskipTests ``` Compiles tests but doesn't run them. Faster builds when tests are not needed. **Skip test compilation entirely:** ```bash mvn clean install -Dmaven.test.skip=true ``` ## 🧪 Test - Run All Tests Execute all unit tests in the project. ```bash mvn test ``` Runs tests in `src/test/java`. Generates reports in `target/surefire-reports/`. ## 🎯 Test - Run Specific Test Run a single test class. ```bash mvn test -Dtest=UserServiceTest ``` **Run specific test method:** ```bash mvn test -Dtest=UserServiceTest#testLogin ``` ## 🔍 Test - Run Pattern Run tests matching a pattern. ```bash mvn test -Dtest=*ServiceTest ``` Matches all test classes ending with `ServiceTest`. ## 🌳 Dependency - Show Tree Display project dependency tree. ```bash mvn dependency:tree ``` Shows all direct and transitive dependencies in tree format. **Filter by groupId:** ```bash mvn dependency:tree -Dincludes=org.springframework ``` ## 🔬 Dependency - Analyze Find unused and undeclared dependencies. ```bash mvn dependency:analyze ``` Helps identify: - Unused declared dependencies - Used undeclared dependencies (should be in POM) ## 📋 Dependency - Copy Dependencies Copy all dependencies to target directory. ```bash mvn dependency:copy-dependencies ``` Copies JARs to `target/dependency/`. Useful for creating lib folders. **Specify output directory:** ```bash mvn dependency:copy-dependencies -DoutputDirectory=lib ``` ## 🔗 Dependency - Resolve Download all dependencies to local repository. ```bash mvn dependency:resolve ``` Ensures all dependencies are in `~/.m2/repository` without building. ## 🧹 Clean - Remove Target Delete the target directory. ```bash mvn clean ``` Removes all compiled classes, JARs, and build artifacts from `target/`. ## 📦 Package - Create JAR Package compiled code into JAR file. ```bash mvn package ``` Creates JAR in `target/-.jar`. ## 🚀 Package - Create Executable JAR Build JAR with dependencies included (fat JAR). ```bash mvn clean package assembly:single ``` Requires maven-assembly-plugin. Creates executable JAR with all dependencies. ## 💾 Install - Local Repository Install artifact to local Maven repository. ```bash mvn install ``` Installs to `~/.m2/repository` for use by other local projects. ## 📥 Install - Custom JAR Install external JAR to local repository. ```bash mvn install:install-file \ -Dfile=mylib.jar \ -DgroupId=com.example \ -DartifactId=mylib \ -Dversion=1.0 \ -Dpackaging=jar ``` ## 🚢 Deploy - Remote Repository Deploy artifact to remote repository. ```bash mvn deploy ``` Uploads to repository configured in ``. Requires credentials in `settings.xml`. ## ℹ️ Version - Display Version Show Maven version and Java version. ```bash mvn -version ``` Displays Maven version, Java version, and OS information. ## ⬆️ Version - Update Parent Update parent POM version. ```bash mvn versions:update-parent ``` Updates `` version to latest release. ## 🔢 Version - Set Version Set new project version. ```bash mvn versions:set -DnewVersion=2.0.0 ``` Updates version in POM and all modules. **Commit the change:** ```bash mvn versions:commit ``` **Revert the change:** ```bash mvn versions:revert ``` ## 🔌 Plugin - Execute Goal Run specific plugin goal. ```bash mvn groupId:artifactId:version:goal ``` **Example - Spring Boot run:** ```bash mvn spring-boot:run ``` ## 📜 Plugin - List Plugins Display all plugins in the project. ```bash mvn help:effective-pom | grep plugin ``` ## ✅ Lifecycle - Validate Validate project structure and POM. ```bash mvn validate ``` Checks if project is correct and all necessary information is available. ## ✔️ Lifecycle - Verify Run integration tests. ```bash mvn verify ``` Runs unit tests and integration tests. Higher lifecycle phase than `test`. ## 📄 POM - Effective POM Display effective POM with all inherited settings. ```bash mvn help:effective-pom ``` Shows final POM after inheritance and interpolation. **Save to file:** ```bash mvn help:effective-pom > effective-pom.xml ``` ## ⚙️ POM - Effective Settings Display effective Maven settings. ```bash mvn help:effective-settings ``` Shows merged settings from global and user `settings.xml`. ## 🗑️ Repository - Purge Local Remove artifacts from local repository. ```bash mvn dependency:purge-local-repository ``` Deletes project dependencies from `~/.m2/repository` and re-downloads them. ## 🔄 Repository - Update Snapshots Force update of snapshot dependencies. ```bash mvn clean install -U ``` The `-U` flag forces update of snapshots and releases. ## 🐛 Debug - Verbose Output Run Maven with debug information. ```bash mvn clean install -X ``` Shows detailed debug logs. Useful for troubleshooting build issues. ## ❌ Debug - Show Errors Display full error stack traces. ```bash mvn clean install -e ``` Shows exception stack traces when build fails. ## 📡 Offline - Build Without Network Build project in offline mode. ```bash mvn clean install -o ``` Uses only local repository. Fails if dependencies are missing. ## 🎭 Profile - Activate Profile Build with specific Maven profile. ```bash mvn clean install -P production ``` **Multiple profiles:** ```bash mvn clean install -P profile1,profile2 ``` ## 🧱 Multi-Module - Build Single Module Build specific module in multi-module project. ```bash mvn clean install -pl module-name ``` **Build module with dependencies:** ```bash mvn clean install -pl module-name -am ``` **Build from specific module:** ```bash mvn clean install -rf :module-name ``` ## ⚡ Performance - Parallel Build Build project using multiple threads. ```bash mvn clean install -T 4 ``` Uses 4 threads. Faster for multi-module projects. **Use cores:** ```bash mvn clean install -T 1C ``` Uses 1 thread per CPU core. ## 📚 Site - Generate Documentation Generate project documentation site. ```bash mvn site ``` Creates HTML documentation in `target/site/`. ## 🏛️ Archetype - Create Project Create new Maven project from archetype. ```bash mvn archetype:generate \ -DgroupId=com.example \ -DartifactId=my-app \ -DarchetypeArtifactId=maven-archetype-quickstart \ -DinteractiveMode=false ``` ## 📥 Source - Download Sources Download source JARs for dependencies. ```bash mvn dependency:sources ``` **Download Javadocs:** ```bash mvn dependency:resolve -Dclassifier=javadoc ``` ## 🎁 Wrapper - Install Maven Wrapper Install Maven wrapper for project. ```bash mvn wrapper:wrapper ``` Creates `mvnw` and `mvnw.cmd` scripts. Allows building without installing Maven globally. **Specify Maven version:** ```bash mvn wrapper:wrapper -Dmaven=3.9.6 ``` ──────────────────────────────────────────────────────────── Full page: https://devtools.krishanchawla.com/devtools/maven-commands More tools: https://devtools.krishanchawla.com