Maven Common Commands
Maven commands follow the following pattern:
mvn [选项] [生命周期阶段] [目标]
Check Maven version
Example
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:
- clean: clean the project
- default (build): build the project
- site: generate project documentation
Clean the project
Example
This command will delete thetargetdirectory, i.e., delete all files generated by the build.
Compile the project
Example
Compile the source code. The compiled class files will be placed in thetarget/classesdirectory.
Run tests
Example
Run all test cases in the project.
Package the project
Example
Package the project according to the packaging type (such as jar, war) specified in the POM file.
Install to the local repository
Example
Build and install the project into the local Maven repository for use as a dependency by other projects.
Deploy to the remote repository
Example
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
Display the project's dependency tree, which helps analyze dependency relationships and resolve dependency conflicts.
Download source code
Example
Download the source code of project dependencies.
Analyze dependencies
Example
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
Create a simple Java project using the Maven archetype system.
Create a Web project
Example
Create a simple Web application project.
Other useful commands
Skip tests
Example
Build the project and install it to the local repository, but skip the test phase.
Run specific tests
Example
Run only the specified test class.
Example
Run only a specific test method in the specified test class.
Update dependencies
Example
Check which dependencies in the project have newer versions available.
Generate site documentation
Example
Generate the project's site documentation, including various reports.
Run a specific phase
Example
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
First clean the project, then compile, test, and install to the local repository.
Example
Clean the project, package but do not run tests.
Troubleshooting common issues
Force update snapshot dependencies
Example
-UThe parameter forces Maven to check for updated versions of dependencies in remote repositories.
Offline mode
Example
-oThe parameter makes Maven run in offline mode, using only dependencies in the local repository.
Debug mode
Example
-XThe parameter enables debug output, showing detailed execution information.
Basic build commands
| Command | Purpose | Lifecycle phase executed |
|---|---|---|
mvn clean | Deletetarget/directory | clean |
mvn validate | Verify the 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 the project | package |
mvn verify | Run integration tests | verify |
mvn install | Install to the local repository | install |
mvn deploy | Deploy to the remote repository | deploy |
Typical development workflow:
mvn clean compile test package
Dependency management commands
| Command | Purpose |
|---|---|
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 dependency updates |
# 查看依赖树 mvn dependency:tree # 检查可用更新 mvn versions:display-dependency-updates
Plugin related commands
| Command | Purpose |
|---|---|
mvn help:describe -Dplugin=插件名 | View plugin information |
mvn help:effective-pom | View the effective POM |
mvn 插件前缀:目标 | Directly run plugin goal |
# 查看compiler插件信息 mvn help:describe -Dplugin=compiler # 直接运行compiler插件的compile目标 mvn compiler:compile
Test related commands
| Command | Purpose |
|---|---|
mvn test | Run all tests |
mvn -Dtest=测试类名 test | Run specified test class |
mvn -Dtest=测试类名#方法名 test | Run specified test method |
mvn -DskipTests=true package | Skip tests |
# 只运行UserServiceTest类 mvn -Dtest=UserServiceTest test # 只运行UserServiceTest中的testCreate方法 mvn -Dtest=UserServiceTest#testCreate test
Multi-module project commands
| Command | Purpose |
|---|---|
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
| Command | Purpose |
|---|---|
mvn -T 4 install | Build in parallel with 4 threads |
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 | Perform project release |
# 并行构建(4线程) mvn -T 4 clean install # 强制更新snapshot依赖 mvn -U clean install
Common command combinations
| Combined command | Purpose |
|---|---|
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"Other extensions