Maven Useful Commands

Cheatsheet Maven

Maven commands to quickly get you going.

What is this? Official Docs

Build automation and dependency management tool for Java projects with powerful lifecycle management.

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).

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.

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

mvn dependency:tree

Filter Specific Dependency

mvn dependency:tree -Dincludes=com.fasterxml.jackson.core

Verbose Mode

mvn dependency:tree -Dverbose

Show Conflicts

mvn dependency:tree -Dverbose | grep "omitted for conflict"

πŸ”„ Force Update Dependencies

Force Maven to download latest SNAPSHOT and release versions.

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.

mvn -pl module-name -am clean install

Flags

FlagDescription
-plProject list - specify module(s)
-amAlso make - build required dependencies
-amdAlso make dependents - build dependent modules

Examples

# 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

mvn clean install -DskipTests

Skip Compilation & Execution

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

mvn -Dtest=LoginTest test

Pattern Matching

mvn -Dtest=User*Test test

Specific Test Method

mvn -Dtest=LoginTest#shouldLoginWorks test

Multiple Tests

mvn -Dtest=LoginTest,UserTest test

🧹 Fix Corrupted Dependencies

Delete Specific Library

rm -rf ~/.m2/repository/com/oracle

Purge Entire Local Repo

mvn dependency:purge-local-repository

Re-download All

mvn dependency:purge-local-repository -DreResolve=true

πŸ“œ Effective POM

View the final POM after all inheritance and interpolation.

mvn help:effective-pom

Save to File

mvn help:effective-pom > effective-pom.xml

Why Use It

  • Debug property resolution
  • See inherited configurations
  • Verify profile activation

πŸ”§ List Active Plugins

mvn help:effective-pom | grep "<plugin>"

All Available Plugins

mvn help:describe -Dplugin=compiler

🐞 Debug Mode

Enable debug logging for CI/CD troubleshooting.

mvn clean install -X

Show Stack Traces

mvn clean install -e

πŸ” Fix SSL/Certificate Issues

Import Certificate

keytool -import -alias nexus \
  -file nexus.crt \
  -keystore $JAVA_HOME/jre/lib/security/cacerts \
  -storepass changeit

Disable SSL Verification (Development Only)

mvn clean install -Dmaven.wagon.http.ssl.insecure=true \
  -Dmaven.wagon.http.ssl.allowall=true

🧡 Work with Profiles

List All Profiles

mvn help:all-profiles

Show Active Profiles

mvn help:active-profiles

Build with Profile

mvn clean install -Pdev

Multiple Profiles

mvn clean install -Pdev,fast-tests

πŸ“¦ Offline Mode

Download all dependencies for offline use.

mvn dependency:go-offline

Build Offline

mvn clean install -o

Use Cases

  • CI/CD with dependency caching
  • Development without internet
  • Air-gapped environments

🎯 Clean Specific Targets

Clean Generated Sources

mvn clean:clean@clean-generated

Clean Without Deleting Logs

mvn clean -Dclean.exclude=logs/**

Deep Clean

mvn clean install -U -DskipTests