Maven 是 Java 应用程序最流行的项目和依赖关系管理工具之一。 Maven 提供了许多命令和选项来帮助您完成日常任务。
本备忘单使用示例 Maven 项目来演示一些有用的 Maven 命令。它最初是为 OpenJDK 13.0.1 和 Maven 3.6.3 编写的。这些命令已通过 OpenJDK 19.0.1 和 Maven 3.8.7 进行验证。
Maven Commands Cheat Sheet
此命令通过删除目标目录来清理 Maven 项目:
输出示例:
Output
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-example-jar ---
[INFO] Deleting /Users/sammy/Desktop/maven-examples/maven-example-jar/target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
此命令编译 Maven 项目的 Java 源类:
输出示例:
Output
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-cli) @ maven-example-jar ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/sammy/Desktop/maven-examples/maven-example-jar/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
该命令编译Maven项目的测试类:
输出示例:
Output
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-cli) @ maven-example-jar ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/sammy/Desktop/maven-examples/maven-example-jar/target/test-classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
该命令构建 Maven 项目并将其打包成JAR
, WAR
, etc.:
输出示例:
Output
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ maven-example-jar ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/sammy/Desktop/maven-examples/maven-example-jar/target/classes
...
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ maven-example-jar ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/sammy/Desktop/maven-examples/maven-example-jar/target/test-classes
...
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-example-jar ---
[INFO] Surefire report directory: /Users/sammy/Desktop/maven-examples/maven-example-jar/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.example.maven.classes.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-example-jar ---
[INFO] Building jar: /Users/sammy/Desktop/maven-examples/maven-example-jar/target/maven-example-jar-0.0.1-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
输出显示了该位置JAR
文件就在之前"BUILD SUCCESS"
信息。注意package
目标执行compile
, testCompile
, and test
打包构建之前的目标。
此命令构建 Maven 项目并安装项目文件 (JAR
, WAR
, pom.xml
等)到本地存储库:
输出示例:
Output
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-example-jar ---
...
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ maven-example-jar ---
...
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-example-jar ---
...
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ maven-example-jar ---
...
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-example-jar ---
...
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-example-jar ---
...
[INFO] --- maven-install-plugin:2.4:install (default-install) @ maven-example-jar ---
[INFO] Installing /Users/sammy/Desktop/maven-examples/maven-example-jar/target/maven-example-jar-0.0.1-SNAPSHOT.jar to /Users/sammy/.m2/repository/com/example/maven/maven-example-jar/0.0.1-SNAPSHOT/maven-example-jar-0.0.1-SNAPSHOT.jar
[INFO] Installing /Users/sammy/Desktop/maven-examples/maven-example-jar/pom.xml to /Users/sammy/.m2/repository/com/example/maven/maven-example-jar/0.0.1-SNAPSHOT/maven-example-jar-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
此命令将工件部署到远程存储库:
项目中应正确配置远程存储库pom.xml
file distributionManagement
标签。 Maven 中的服务器条目settings.xml
文件用于提供身份验证详细信息。
此命令验证 Maven 项目,以确保一切正确并且所有必要的信息均可用:
该命令生成Maven项目的依赖树:
输出示例:
Output
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ Mockito-Examples ---
[INFO] com.example.mockito:Mockito-Examples:jar:1.0-SNAPSHOT
[INFO] +- org.junit.platform:junit-platform-runner:jar:1.2.0:test
[INFO] | +- org.apiguardian:apiguardian-api:jar:1.0.0:test
[INFO] | +- org.junit.platform:junit-platform-launcher:jar:1.2.0:test
[INFO] | \- org.junit.platform:junit-platform-suite-api:jar:1.2.0:test
[INFO] | \- org.junit.platform:junit-platform-commons:jar:1.2.0:test
[INFO] +- org.junit.jupiter:junit-jupiter-engine:jar:5.2.0:test
[INFO] | +- org.junit.platform:junit-platform-engine:jar:1.2.0:test
[INFO] | | \- org.opentest4j:opentest4j:jar:1.1.0:test
[INFO] | \- org.junit.jupiter:junit-jupiter-api:jar:5.2.0:test
[INFO] +- org.mockito:mockito-junit-jupiter:jar:2.19.0:test
[INFO] | \- org.mockito:mockito-core:jar:2.19.0:test
[INFO] | +- net.bytebuddy:byte-buddy:jar:1.8.10:test
[INFO] | +- net.bytebuddy:byte-buddy-agent:jar:1.8.10:test
[INFO] | \- org.objenesis:objenesis:jar:2.6:test
[INFO] \- org.testng:testng:jar:6.14.3:test
[INFO] +- com.beust:jcommander:jar:1.72:test
[INFO] \- org.apache-extras.beanshell:bsh:jar:2.0b6:test
此命令分析 Maven 项目以识别未使用的已声明和已使用的未声明依赖项:
输出示例:
Output
[INFO] --- maven-dependency-plugin:2.8:analyze (default-cli) @ Mockito-Examples ---
[WARNING] Used undeclared dependencies found:
[WARNING] org.junit.jupiter:junit-jupiter-api:jar:5.2.0:test
[WARNING] org.mockito:mockito-core:jar:2.19.0:test
[WARNING] Unused declared dependencies found:
[WARNING] org.junit.platform:junit-platform-runner:jar:1.2.0:test
[WARNING] org.junit.jupiter:junit-jupiter-engine:jar:5.2.0:test
[WARNING] org.mockito:mockito-junit-jupiter:jar:2.19.0:test
它通过识别未使用的依赖项并将其从构建中删除来减少构建大小。pom.xml
file.
该命令生成不同类型的骨架Maven项目,例如JAR
、Web 应用程序、Maven 站点等:
输出示例:
Output
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: maven-archetype-quickstart:1.4
[INFO] ----------------------------------------------------------------------------
...
[INFO] Project created from Archetype in dir: /Users/sammy/Desktop/maven-examples/maven-example-jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
推荐阅读:使用 Maven 原型创建 Java 项目
此命令为项目生成一个站点:
你会注意到一个site
目录中的target
执行此命令后的目录。
/Users/sammy/Desktop/maven-examples/maven-example-jar/ target/site/index.html
里面会有多个HTML文件site
提供与项目相关的信息的目录。
此命令运行项目的测试用例:
输出示例:
Output
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running TestSuite
first-element
second-element
Employee setName Argument = Sammy
...
[INFO] Results:
[INFO]
[INFO] Tests run: 5, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
此命令编译项目的源 Java 类:
输出示例:
Output
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ Mockito-Examples ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 10 source files to /Users/sammy/Desktop/maven-examples/Mockito-Examples/target/classes
它与之前的类似mvn compiler:compile
命令,但运行整个 Maven 生命周期compile
.
此命令构建项目,运行所有测试用例并对集成测试的结果运行任何检查以确保满足质量标准:
Maven 提供了许多命令行选项来改变 Maven 构建过程:
此命令行选项打印 Maven 用法和所有可用选项:
输出示例:
Output
usage: mvn [options] [<goal(s)>] [<phase(s)>]
Options:
-am,--also-make If project list is specified, also
build projects required by the
list
-amd,--also-make-dependents If project list is specified, also
build projects that depend on
projects on the list
-B,--batch-mode Run in non-interactive (batch)
mode (disables output color)
-b,--builder <arg> The id of the build strategy to
use
-C,--strict-checksums Fail the build if checksums don't
match
-c,--lax-checksums Warn if checksums don't match
-cpu,--check-plugin-updates Ineffective, only kept for
backward compatibility
此命令行选项从不同位置构建项目:
mvn -f dir/ pom.xml package
提供pom.xml
构建项目的文件位置。当您必须从脚本运行 Maven 构建时,它非常有用。
此命令行选项运行 Maven 构建离线模式 :
当您拥有所有必需的内容时,它很有用JAR
下载到本地存储库中并且您不希望 Maven 查找任何JAR
s 在远程存储库中。
此命令行选项运行 Maven 构建静音模式 ,以便仅显示测试用例结果和错误:
此命令行选项打印 Maven 版本并运行构建调试模式 ,以便显示所有消息:
输出示例:
Output
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /Users/sammy/Downloads/apache-maven-3.6.3
Java version: 13.0.1, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-13.0.1.jdk/Contents/Home
Default locale: en_IN, platform encoding: UTF-8
OS name: "mac os x", version: "10.15.1", arch: "x86_64", family: "mac"
[DEBUG] Created new class realm maven.api
[DEBUG] Importing foreign pacakges into class realm maven.api
[DEBUG] Imported: javax.annotation.* < plexus.core
[DEBUG] Imported: javax.annotation.security.* < plexus.core
[DEBUG] Imported: javax.enterprise.inject.* < plexus.core
[DEBUG] Imported: javax.enterprise.util.* < plexus.core
[DEBUG] Imported: javax.inject.* < plexus.core
此命令行选项显示 Maven 版本信息:
输出示例:
Output
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /Users/sammy/Downloads/apache-maven-3.6.3
Java version: 13.0.1, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-13.0.1.jdk/Contents/Home
Default locale: en_IN, platform encoding: UTF-8
OS name: "mac os x", version: "10.15.1", arch: "x86_64", family: "mac"
此命令行选项打印 Maven 版本,然后继续构建:
它相当于以下命令:
此命令行选项应用skipTests
系统属性从构建周期中跳过单元测试用例:
您还可以跳过测试用例执行:
mvn -Dmaven.test.skip = true package
此命令行选项告诉 Maven 使用指定的线程数运行并行构建:
它在可以并行构建模块的多个模块项目中非常有用。它可以减少项目的构建时间。