# curl https://devtools.krishanchawla.com/raw/maven-useful-commands.txt MAVEN USEFUL COMMANDS Maven commands to quickly get you going. 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. This guide covers frequently used Maven commands for dependency management, debugging, and troubleshooting. **⚡ Key Features** - Local JAR installation - Dependency debugging - Multi-module builds - Profile management - Offline mode support **🎯 Use Cases** - Installing proprietary JARs - Debugging dependency conflicts - Managing multi-module projects - Optimizing build performance - Troubleshooting build issues --- ## 🧩 Install Local JAR Manually install a dependency when it **cannot be downloaded from Maven Central** (Oracle, IBM, internal JARs). ```bash mvn install:install-file \ -Dfile=ojdbc8.jar \ -DgroupId=com.oracle.database.jdbc \ -DartifactId=ojdbc8 \ -Dversion=19.3.0.0 \ -Dpackaging=jar ``` ### Common Use Cases - Oracle JDBC drivers - Telecom / vendor SDKs - Internal utilities not published yet ## 🏢 Deploy to Nexus/Artifactory Publish any standalone JAR to your **internal Maven repository**. ```bash mvn deploy:deploy-file \ -Durl=http://nexus.myorg.com/repository/maven-releases/ \ -DrepositoryId=nexus \ -Dfile=my-util.jar \ -DgroupId=com.myorg.util \ -DartifactId=my-util \ -Dversion=1.0.0 \ -Dpackaging=jar ``` ### Requirements - Configure `repositoryId` in `settings.xml` - Ensure credentials are set up - Use releases repo for stable versions ## 🔍 Debug Dependencies ### View Full Tree ```bash mvn dependency:tree ``` ### Filter Specific Dependency ```bash mvn dependency:tree -Dincludes=com.fasterxml.jackson.core ``` ### Verbose Mode ```bash mvn dependency:tree -Dverbose ``` ### Show Conflicts ```bash mvn dependency:tree -Dverbose | grep "omitted for conflict" ``` ## 🔄 Force Update Dependencies Force Maven to download latest SNAPSHOT and release versions. ```bash mvn clean install -U ``` ### When to Use - SNAPSHOT dependencies not updating - Corrupted local repository - After changing remote repository URLs ## 🧱 Build Specific Module Build only a specific module in a multi-module project. ```bash mvn -pl module-name -am clean install ``` ### Flags | Flag | Description | |------|-------------| | `-pl` | Project list - specify module(s) | | `-am` | Also make - build required dependencies | | `-amd` | Also make dependents - build dependent modules | ### Examples ```bash # Build single module mvn -pl service-api clean install # Build multiple modules mvn -pl service-api,service-impl clean install # Build module and dependents mvn -pl shared-utils -amd clean install ``` ## 🚫 Skip Tests ### Skip Test Execution ```bash mvn clean install -DskipTests ``` ### Skip Compilation & Execution ```bash mvn clean install -Dmaven.test.skip=true ``` ### Difference - `skipTests`: Compiles but doesn't run - `maven.test.skip`: Skips everything ## 🧪 Run Specific Tests ### Single Test Class ```bash mvn -Dtest=LoginTest test ``` ### Pattern Matching ```bash mvn -Dtest=User*Test test ``` ### Specific Test Method ```bash mvn -Dtest=LoginTest#shouldLoginWorks test ``` ### Multiple Tests ```bash mvn -Dtest=LoginTest,UserTest test ``` ## 🧹 Fix Corrupted Dependencies ### Delete Specific Library ```bash rm -rf ~/.m2/repository/com/oracle ``` ### Purge Entire Local Repo ```bash mvn dependency:purge-local-repository ``` ### Re-download All ```bash mvn dependency:purge-local-repository -DreResolve=true ``` ## 📜 Effective POM View the final POM after all inheritance and interpolation. ```bash mvn help:effective-pom ``` ### Save to File ```bash mvn help:effective-pom > effective-pom.xml ``` ### Why Use It - Debug property resolution - See inherited configurations - Verify profile activation ## 🔧 List Active Plugins ```bash mvn help:effective-pom | grep "" ``` ### All Available Plugins ```bash mvn help:describe -Dplugin=compiler ``` ## 🐞 Debug Mode Enable debug logging for CI/CD troubleshooting. ```bash mvn clean install -X ``` ### Show Stack Traces ```bash mvn clean install -e ``` ## 🔐 Fix SSL/Certificate Issues ### Import Certificate ```bash keytool -import -alias nexus \ -file nexus.crt \ -keystore $JAVA_HOME/jre/lib/security/cacerts \ -storepass changeit ``` ### Disable SSL Verification (Development Only) ```bash mvn clean install -Dmaven.wagon.http.ssl.insecure=true \ -Dmaven.wagon.http.ssl.allowall=true ``` ## 🧵 Work with Profiles ### List All Profiles ```bash mvn help:all-profiles ``` ### Show Active Profiles ```bash mvn help:active-profiles ``` ### Build with Profile ```bash mvn clean install -Pdev ``` ### Multiple Profiles ```bash mvn clean install -Pdev,fast-tests ``` ## 📦 Offline Mode Download all dependencies for offline use. ```bash mvn dependency:go-offline ``` ### Build Offline ```bash mvn clean install -o ``` ### Use Cases - CI/CD with dependency caching - Development without internet - Air-gapped environments ## 🎯 Clean Specific Targets ### Clean Generated Sources ```bash mvn clean:clean@clean-generated ``` ### Clean Without Deleting Logs ```bash mvn clean -Dclean.exclude=logs/** ``` ### Deep Clean ```bash mvn clean install -U -DskipTests ``` ──────────────────────────────────────────────────────────── Full page: https://devtools.krishanchawla.com/devtools/maven-useful-commands More tools: https://devtools.krishanchawla.com