YouTip LogoYouTip

Maven Commands

Maven Common Commands

Maven Common Commands

Maven commands follow the following pattern:

mvn   

Check Maven Version

Example

mvn -v

This command will display the installed Maven version, Java version, and other information.


Maven Lifecycle Commands

Maven is based on the concept of build lifecycle, which includes three main lifecycles:

  1. clean: Clean the project
  2. default (build): Build the project
  3. site: Generate project documentation

Clean Project

Example

mvn clean

This command will delete the target directory, which means deleting all build-generated files.

Compile Project

Example

mvn compile

Compile the source code, the compiled class files will be placed in the target/classes directory.

Run Tests

Example

mvn test

Run all test cases in the project.

Package Project

Example

mvn package

Package the project according to the package type specified in the POM file (such as jar, war).

Install to Local Repository

Example

mvn install

Build the project and install it to the local Maven repository for other projects to depend on.

Deploy to Remote Repository

Example

mvn deploy

Copy the final project package to the remote repository for other developers and projects to use.


Dependency Management Commands

View Project Dependency Tree

Example

mvn dependency:tree

Display the project's dependency tree, which helps analyze dependency relationships and resolve dependency conflicts.

Download Source Code

Example

mvn dependency:sources

Download the source code of project dependencies.

Analyze Dependencies

Example

mvn dependency:analyze

Analyze dependencies that are used but not declared in the project, as well as dependencies that are declared but not used.


Project Creation Commands

Create Simple Project

Example

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Create a simple Java project using the Maven archetype system.

Create Web Project

Example

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-web-app -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

Create a simple web application project.


Other Useful Commands

Skip Tests

Example

mvn install -DskipTests

Build the project and install it to the local repository, but skip the test phase.

Run Specific Tests

Example

mvn test -Dtest=TestClassName

Run only the specified test class.

Example

mvn test -Dtest=TestClassName#testMethodName

Run only the specific test method in the specified test class.

Update Dependencies

Example

mvn versions:display-dependency-updates

Check which dependencies in the project have newer versions available.

Generate Site Documentation

Example

mvn site

Generate site documentation for the project, including various reports.

Run Specific Phase

Example

mvn clean package

First execute the clean lifecycle, then execute the default lifecycle to the package phase.


Maven Command Combinations

Maven commands can be combined for improved efficiency:

Example

mvn clean install

First clean the project, then compile, test, and install to the local repository.

Example

mvn clean package -DskipTests

Clean the project, package but do not run tests.


Common Problem Solutions

Force Update Snapshot Dependencies

Example

mvn clean install -U

The -U parameter forces Maven to check for updated versions of dependencies in the remote repository.

Offline Mode

Example

mvn -o package

The -o parameter makes Maven run in offline mode, using only dependencies from the local repository.

Debug Mode

Example

mvn -X clean install

The -X parameter enables debug output, showing detailed execution information.


Basic Build Commands

Command Description Lifecycle Phase
mvn clean Delete target/ directory clean
mvn validate Validate project is correct validate
mvn compile Compile main source code compile
mvn test-compile Compile test code test-compile
mvn test Run unit tests test
mvn package Package project package
mvn verify Run integration tests verify
mvn install Install to local repository install
mvn deploy Deploy to remote repository deploy

Typical development workflow:

mvn clean compile test package

Dependency Management Commands

Command Description
mvn dependency:tree Display dependency tree
mvn dependency:analyze Analyze dependency issues
mvn dependency:copy-dependencies Copy dependencies to directory
mvn dependency:purge-local-repository Clear local dependency cache
mvn versions:display-dependency-updates Check for dependency updates
# View dependency tree
mvn dependency:tree

# Check for available updates
mvn versions:display-dependency-updates

Plugin-Related Commands

Command Description
mvn help:describe -Dplugin=plugin-name View plugin information
mvn help:effective-pom View the final effective POM
mvn plugin-prefix:goal Run plugin goal directly
# View compiler plugin information
mvn help:describe -Dplugin=compiler

# Run compiler plugin's compile goal directly
mvn compiler:compile

Testing Commands

Command Description
mvn test Run all tests
mvn -Dtest=test-class-name test Run specified test class
mvn -Dtest=test-class-name#method-name test Run specified test method
mvn -DskipTests=true package Skip tests
# Run only UserServiceTest class
mvn -Dtest=UserServiceTest test

# Run only testCreate method in UserServiceTest
mvn -Dtest=UserServiceTest#testCreate test

Multi-Module Project Commands

Command Description
mvn -pl module-name command Execute command on specified module
mvn -am -pl module-name command Execute command and process dependent modules
mvn -rf module-name command Continue execution from specified module
# Compile only service module
mvn -pl service compile

# Compile web module and its dependent modules
mvn -am -pl web compile

Advanced Usage

Command Description
mvn -T 4 install Build with 4 threads in parallel
mvn -o package Run in offline mode
mvn -X clean install Enable debug output
mvn -U clean install Force update snapshot dependencies
mvn release:prepare Prepare project release
mvn release:perform Execute project release
# Parallel build (4 threads)
mvn -T 4 clean install

# Force update snapshot dependencies
mvn -U clean install

Common Command Combinations

Combined Command Description
mvn clean install Clean and reinstall
mvn clean package -DskipTests Clean, package and skip tests
mvn clean test Clean and run tests
mvn clean deploy Clean and deploy

Practical Tips

Quickly view project information:

mvn help:effective-pom
mvn help:describe -Dcmd=compile

Debug build issues:

mvn -X clean install

Skip checks:

mvn install -Dcheckstyle.skip=true -Dpmd.skip=true

Specify profile:

mvn install -P production

View build time:

mvn install --batch-mode | grep "Total time"
← Maven Multi ModuleMaven Intro β†’