SoFunction
Updated on 2025-04-10

Detailed explanation about the use of Maven build commands

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 asmvn dependency:treemvn help:effective-pomwait.

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

  • deletetarget/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 firsttarget/Catalog, then package.

2.2 mvn compile

effect:Compiledsrc/main/javaSource code in the directory and generate.classFile totarget/classes/Table of contents.

mvn compile

Execution process

  • Analysis
  • Download project dependencies (if the dependencies do not exist)
  • Compilationsrc/main/javaJava source code in the directory
  • generate.classFile totarget/classes/

Applicable scenarios

  • When only the code needs to be compiled without the tests being executed.

Example

mvn clean compile

Effect

  • Clean up firsttarget/Directory, and then compile the code.

2.3 mvn test

effect:Compiledsrc/test/javaTest code in the directory and run unit tests (JUnit, TestNG).

mvn test

Execution process

  • Execute firstmvn compile
  • Compilationsrc/test/javaTest 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 firsttarget/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 firstmvn test
  • Packtarget/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 onestarget/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 firstmvn package
  • Willtarget/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 firstmvn install
  • Uploadtarget/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.
  • passmvn clean installA complete build can be completed and installed to the local repository.
  • passmvn deployThe built JAR/WAR can be deployed to a remote repository for sharing by the team.
  • passmvn dependency:treeDependencies 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.