1. Maven build command overview
Maven is one of the most popular build tools in the Java ecosystem. It provides a standardized set of build commands that enable developers to easily manage tasks such as compilation, testing, packaging, installation and deployment of projects.
This article will provide an in-depth analysis of the most commonly used build commands in Maven, including:
mvn clean
mvn compile
mvn test
mvn package
mvn install
mvn deploy
- Other common commands, such as
mvn dependency:tree
、mvn help:effective-pom
wait.
2. Detailed explanation of Maven build command
2.1 mvn clean
effect: Cleartarget/
Directory, delete the files you built before, and ensure a clean environment for the next build.
mvn clean
Execution process:
- delete
target/
Table of contents - Remove compiled class files, packaged JAR files, etc.
Applicable scenarios:
- Before rebuilding the project, avoid old compiled files affecting the new version.
Example:
mvn clean package
Effect:
- Clean up first
target/
Catalog, then package.
2.2 mvn compile
effect:Compiledsrc/main/java
Source code in the directory and generate.class
File totarget/classes/
Table of contents.
mvn compile
Execution process:
- Analysis
- Download project dependencies (if the dependencies do not exist)
- Compilation
src/main/java
Java source code in the directory - generate
.class
File totarget/classes/
Applicable scenarios:
- When only the code needs to be compiled without the tests being executed.
Example:
mvn clean compile
Effect:
- Clean up first
target/
Directory, and then compile the code.
2.3 mvn test
effect:Compiledsrc/test/java
Test code in the directory and run unit tests (JUnit, TestNG).
mvn test
Execution process:
- Execute first
mvn compile
- Compilation
src/test/java
Test code in the directory - Run tests (JUnit or TestNG)
Applicable scenarios:
- Run unit tests in the project to ensure the code logic is correct.
Example:
mvn clean test
Effect:
- Clean up first
target/
Directory, and then run all the tests.
2.4 mvn package
effect: Package the compiled code (such as JAR, WAR) totarget/
Table of contents.
mvn package
Execution process:
- Execute first
mvn test
- Pack
target/classes/
The file under is JAR or WAR
Applicable scenarios:
- When it is necessary to generate deployable JAR/WAR files.
Example:
mvn clean package
Effect:
- Clean up old ones
target/
Directory, compile code, run tests, and finally package JAR/WAR.
2.5 mvn install
effect: Install the packaged JAR/WAR file toLocal Maven repository(~/.m2/repository/
) for use in other projects.
mvn install
Execution process:
- Execute first
mvn package
- Will
target/
Copy the JAR/WAR files in the directory to the local Maven repository
Applicable scenarios:
- When developing multiple modules (Modules), one module can be installed to the local repository for reference by another module.
Example:
mvn clean install
Effect:
- Clean up old builds, compile code, run tests, package, and install to local repository.
2.6 mvn deploy
effect: Deploy the built JAR/WAR toRemote Maven Repository, such as Nexus, Artifactory.
mvn deploy
Execution process:
- Execute first
mvn install
- Upload
target/
JAR/WAR files in the directory to the remote Maven repository
Applicable scenarios:
- When the developed libraries need to be shared within the team, they can be deployed to a private repository (such as Nexus).
Example:
mvn clean deploy
Effect:
- Clean, compile, test, package, and upload to remote repository.
3. Other commonly used Maven commands
3.1 View the dependency tree
effect: List all project dependencies, including transitive dependencies, to help analyze dependency conflicts.
mvn dependency:tree
Example:
mvn dependency:tree
Output:
[INFO] :my-app:jar:1.0-SNAPSHOT
[INFO] ├─ :spring-core:jar:5.3.9:compile
[INFO] ├─ commons-logging:commons-logging:jar:1.2:compile
3.2 Forced update dependencies
effect: Force updates all dependencies to ensure that the latest version is pulled.
mvn clean install -U
3.3 Show Maven's valid POM configuration
effect: View the merged, help debug dependency management.
mvn help:effective-pom
3.4 Skip the test
effect: Skip unit tests during build to improve build speed.
mvn clean package -DskipTests
3.5 Run a single test
effect: Run only the specified test class, not all tests.
mvn test -Dtest=MyTestClass
3.6 Running a Web Project
effect: If usedtomcat7-maven-plugin
, can run web projects directly.
mvn tomcat7:run
4. Summary of commonly used commands of Maven
Order | effect |
---|---|
mvn clean | Delete the target/ directory and clean the build file |
mvn compile | Compile the code in the src/main/java directory |
mvn test | Run the test code in the src/test/java directory |
mvn package | Generate JAR/WAR file |
mvn install | Install JAR/WAR to the local Maven repository |
mvn deploy | Deploy JAR/WAR to a remote repository |
mvn dependency:tree | View the project's dependency tree |
mvn clean install -U | Forced update dependencies |
mvn help:effective-pom | View the merged POM configuration |
mvn clean package -DskipTests | Skip the test package |
mvn test -Dtest=MyTestClass | Run only the specified test class |
5. Summary
- Maven provides a standard set of build commands coveringClean, compile, test, package, install, deployand other functions.
- pass
mvn clean install
A complete build can be completed and installed to the local repository. - pass
mvn deploy
The built JAR/WAR can be deployed to a remote repository for sharing by the team. - pass
mvn dependency:tree
Dependencies can be analyzed to help resolve dependency conflicts.
The above is personal experience. I hope you can give you a reference and I hope you can support me more.