Maven Common Commands
Maven commands follow the following patterns:
mvn [选项] [生命周期阶段] [目标]
Check Maven version
Example
mvn
-v
This command displays the installed Maven version, Java version, and other information.
Maven Lifecycle Commands
Maven is based on the concept of a build lifecycle and contains three main lifecycles:
- clean: clean the project
- default (build): project build
- site: generate project documentation
Clean the project
Example
mvn clean
This command deletes the target directory, i.e., deletes all files generated by the build.
Compile the project
Example
mvn compile
Compile source code; the compiled class files are placed in target/classes directory.
Running 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 specified in the POM file (such as jar, war).
Install to the local repository
Example
mvn
install
Build and install the project to the local Maven repository for use as a dependency by other projects.
Deploy to a remote repository
Example
mvn deploy
Copy the final project package to a 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, helping to analyze dependencies 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 used but not declared in the project, and those 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
Use the Maven archetype system to create a simple Java project.
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 a test
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 and package without running tests.
Common Problem Solutions
Force update of snapshot dependencies
Example
mvn clean
install
-U
-U The parameter forces Maven to check for updated versions of dependencies in the remote repository.
Offline mode
Example
mvn
-o
package
-o The parameter makes Maven run in offline mode, using only dependencies in the local repository.
Debug Mode
Example
mvn
-X
clean
install
-X The 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 the 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 a remote repository | deploy |
Typical development workflow:
mvn clean compile test package
Dependency Management Commands
| Command | Purpose |
|---|---|
mvn dependency:tree |
Display the dependency tree |
mvn dependency:analyze |
Analyze dependency issues |
mvn dependency:copy-dependencies |
Copy dependencies to a directory |
mvn dependency:purge-local-repository |
Clear the local dependency cache |
mvn versions:display-dependency-updates |
Check for dependency updates |
# 查看依赖树
mvn dependency:tree
# Check for available updates
mvn versions:display-dependency-updates
Plugin-Related Commands
| Command | Purpose |
|---|---|
mvn help:describe -Dplugin=插件名 |
View plugin information |
mvn help:effective-pom |
View the finally effective POM |
mvn 插件前缀:目标 |
Run a plugin goal directly |
# 查看compiler插件信息
mvn help:describe -Dplugin=compiler
# Directly run the compile target of the compiler plugin
mvn compiler:compile
Test-Related Commands
| Command | Purpose |
|---|---|
mvn test |
Run all tests |
mvn -Dtest=测试类名 test |
Run a specified test class |
mvn -Dtest=测试类名#方法名 test |
Run a specified test method |
mvn -DskipTests=true package |
Skip a test |
# 只运行UserServiceTest类
mvn -Dtest=UserServiceTest test
# Run only the testCreate method in UserServiceTest
mvn -Dtest=UserServiceTest#testCreate test
Multi-Module Project Commands
| Command | Purpose |
|---|---|
mvn -pl 模块名 命令 |
Execute a command on a specified module |
mvn -am -pl 模块名 命令 |
Execute a command and process dependent modules |
mvn -rf 模块名 命令 |
Continue execution from a specified module |
# 只编译service模块
mvn -pl service compile
# Compile the web module and its dependent modules
mvn -am -pl web compile
Advanced usage
| Command | Purpose |
|---|---|
mvn -T 4 install |
Build in parallel using 4 threads |
mvn -o package |
Run in offline mode |
mvn -X clean install |
Enable debug output |
mvn -U clean install |
Force update of snapshot dependencies |
mvn release:prepare |
Prepare project release |
mvn release:perform |
Perform project release |
# 并行构建(4线程)
mvn -T 4 clean install
# Force update snapshot dependency
mvn -U clean install
Common Command Combinations
| Combined commands | 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 problems:
mvn -X clean install
Skip checks:
mvn install -Dcheckstyle.skip=true -Dpmd.skip=true
Specify a profile:
mvn install -P production
View build time:
mvn install --batch-mode | grep "Total time"