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 lifecycles, containing three main lifecycles:

  1. clean: clean the project
  2. default (build): build the project
  3. site: generate project documentation

Clean the project

Example

mvn clean

This command will delete thetargetdirectory, i.e., delete all files generated by the build.

Compile the project

Example

mvn compile

Compile the source code. The compiled class files will be placed in thetarget/classesdirectory.

Run tests

Example

mvn test

Run all test cases in the project.

Package the project

Example

mvn package

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

Install to the local repository

Example

mvn install

Build and install the project into the local Maven repository for use as a dependency by other projects.

Deploy to the remote repository

Example

mvn deploy

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


Dependency Management Commands

View the 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, as well as dependencies that are declared but not used.


Project Creation Commands

Create a 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 a 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 a 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 the project's site documentation, including various reports.

Run a specific phase

Example

mvn clean package

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


Combining Maven commands

Maven commands can be combined to improve 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.


Troubleshooting common issues

Force update snapshot dependencies

Example

mvn clean install -U

-UThe parameter forces Maven to check for updated versions of dependencies in remote repositories.

Offline mode

Example

mvn -o package

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

Debug mode

Example

mvn -X clean install

-XThe parameter enables debug output, showing detailed execution information.


Basic build commands

CommandPurposeLifecycle phase executed
mvn cleanDeletetarget/directoryclean
mvn validateVerify the project is correctvalidate
mvn compileCompile main source codecompile
mvn test-compileCompile test codetest-compile
mvn testRun unit teststest
mvn packagePackage the projectpackage
mvn verifyRun integration testsverify
mvn installInstall to the local repositoryinstall
mvn deployDeploy to the remote repositorydeploy

Typical development workflow:

mvn clean compile test package

Dependency management commands

CommandPurpose
mvn dependency:treeDisplay dependency tree
mvn dependency:analyzeAnalyze dependency issues
mvn dependency:copy-dependenciesCopy dependencies to directory
mvn dependency:purge-local-repositoryClear local dependency cache
mvn versions:display-dependency-updatesCheck dependency updates
# 查看依赖树
mvn dependency:tree

# 检查可用更新
mvn versions:display-dependency-updates

Plugin related commands

CommandPurpose
mvn help:describe -Dplugin=插件名View plugin information
mvn help:effective-pomView the effective POM
mvn 插件前缀:目标Directly run plugin goal
# 查看compiler插件信息
mvn help:describe -Dplugin=compiler

# 直接运行compiler插件的compile目标
mvn compiler:compile

Test related commands

CommandPurpose
mvn testRun all tests
mvn -Dtest=测试类名 testRun specified test class
mvn -Dtest=测试类名#方法名 testRun specified test method
mvn -DskipTests=true packageSkip tests
# 只运行UserServiceTest类
mvn -Dtest=UserServiceTest test

# 只运行UserServiceTest中的testCreate方法
mvn -Dtest=UserServiceTest#testCreate test

Multi-module project commands

CommandPurpose
mvn -pl 模块名 命令Execute command on specified module
mvn -am -pl 模块名 命令Execute command and process dependent modules
mvn -rf 模块名 命令Continue execution from specified module
# 只编译service模块
mvn -pl service compile

# 编译web模块及其依赖的模块
mvn -am -pl web compile

Advanced usage

CommandPurpose
mvn -T 4 installBuild in parallel with 4 threads
mvn -o packageRun in offline mode
mvn -X clean installEnable debug output
mvn -U clean installForce update snapshot dependencies
mvn release:preparePrepare project release
mvn release:performPerform project release
# 并行构建(4线程)
mvn -T 4 clean install

# 强制更新snapshot依赖
mvn -U clean install

Common command combinations

Combined commandPurpose
mvn clean installClean and reinstall
mvn clean package -DskipTestsClean, package, and skip tests
mvn clean testClean and run tests
mvn clean deployClean 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"
Other extensions