20+ Maven 命令和选项(小抄)

介绍

Maven 是 Java 应用程序中最流行的项目和依赖管理工具之一. Maven 提供了许多命令和选项,可帮助您完成日常任务。

此 cheat sheet 使用样本 Maven 项目来演示一些有用的 Maven 命令. 它最初是为 OpenJDK 13.0.1 和 Maven 3.6.3 编写的。

Maven 命令 Cheat 表

Maven Commands Cheat Sheet

《MVN清洁》

此命令通过删除目标目录来清除 Maven 项目:

1mvn clean

出口的例子:

1[secondary_label Output]
2[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-example-jar ---
3[INFO] Deleting /Users/sammy/Desktop/maven-examples/maven-example-jar/target
4[INFO] ------------------------------------------------------------------------
5[INFO] BUILD SUCCESS
6[INFO] ------------------------------------------------------------------------

《mvn compiler:compile》

此命令编译了 Maven 项目的 Java 源类:

1mvn compiler:compile

出口的例子:

1[secondary_label Output]
2[INFO] --- maven-compiler-plugin:3.8.1:compile (default-cli) @ maven-example-jar ---
3[INFO] Changes detected - recompiling the module!
4[INFO] Compiling 1 source file to /Users/sammy/Desktop/maven-examples/maven-example-jar/target/classes
5[INFO] ------------------------------------------------------------------------
6[INFO] BUILD SUCCESS
7[INFO] ------------------------------------------------------------------------

《mvn compiler:testCompile》

此命令汇编了 Maven 项目的测试类:

1mvn compiler:testCompile

出口的例子:

1[secondary_label Output]
2[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-cli) @ maven-example-jar ---
3[INFO] Changes detected - recompiling the module!
4[INFO] Compiling 1 source file to /Users/sammy/Desktop/maven-examples/maven-example-jar/target/test-classes
5[INFO] ------------------------------------------------------------------------
6[INFO] BUILD SUCCESS
7[INFO] ------------------------------------------------------------------------

《mvn包》

此命令构建了 Maven 项目,并将其包装成一个JAR,WAR,等等:

1mvn package

出口的例子:

 1[secondary_label Output]
 2[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ maven-example-jar ---
 3[INFO] Changes detected - recompiling the module!
 4[INFO] Compiling 1 source file to /Users/sammy/Desktop/maven-examples/maven-example-jar/target/classes
 5...
 6[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ maven-example-jar ---
 7[INFO] Changes detected - recompiling the module!
 8[INFO] Compiling 1 source file to /Users/sammy/Desktop/maven-examples/maven-example-jar/target/test-classes
 9...
10[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-example-jar ---
11[INFO] Surefire report directory: /Users/sammy/Desktop/maven-examples/maven-example-jar/target/surefire-reports
12
13-------------------------------------------------------
14 T E S T S
15-------------------------------------------------------
16Running com.example.maven.classes.AppTest
17Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 sec
18
19Results :
20
21Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
22
23[INFO]
24[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-example-jar ---
25[INFO] Building jar: /Users/sammy/Desktop/maven-examples/maven-example-jar/target/maven-example-jar-0.0.1-SNAPSHOT.jar
26[INFO] ------------------------------------------------------------------------
27[INFO] BUILD SUCCESS

输出显示JAR文件的位置,就在BUILD SUCCESS消息之前。 注意目标在包装 build 之前执行compile,testCompiletest目标。

mvn 安装

此命令构建了 Maven 项目,并将项目文件(‘JAR’、‘WAR’、‘pom.xml’ 等)安装到本地存储库:

1mvn install

出口的例子:

 1[secondary_label Output]
 2[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-example-jar ---
 3...
 4[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ maven-example-jar ---
 5...
 6[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-example-jar ---
 7...
 8[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ maven-example-jar ---
 9...
10[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-example-jar ---
11...
12[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-example-jar ---
13...
14[INFO] --- maven-install-plugin:2.4:install (default-install) @ maven-example-jar ---
15[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
16[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
17[INFO] ------------------------------------------------------------------------
18[INFO] BUILD SUCCESS

《MVN部署》

此命令将文物部署到远程存储库:

1mvn deploy

远程存储库应在项目文件 pom.xml 标签 distributionManagement 中正确配置,Maven settings.xml 文件中的服务器条目用于提供身份验证细节。

《MVN 验证》

此命令验证了 Maven 项目,以确保一切都是正确的,并提供所有必要的信息:

1mvn validate

《mvn dependence:tree》

此命令会生成 Maven 项目的依赖树:

1mvn dependency:tree

出口的例子:

 1[secondary_label Output]
 2[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ Mockito-Examples ---
 3[INFO] com.example.mockito:Mockito-Examples:jar:1.0-SNAPSHOT
 4[INFO] +- org.junit.platform:junit-platform-runner:jar:1.2.0:test
 5[INFO] |  +- org.apiguardian:apiguardian-api:jar:1.0.0:test
 6[INFO] |  +- org.junit.platform:junit-platform-launcher:jar:1.2.0:test
 7[INFO] |  \- org.junit.platform:junit-platform-suite-api:jar:1.2.0:test
 8[INFO] |     \- org.junit.platform:junit-platform-commons:jar:1.2.0:test
 9[INFO] +- org.junit.jupiter:junit-jupiter-engine:jar:5.2.0:test
10[INFO] |  +- org.junit.platform:junit-platform-engine:jar:1.2.0:test
11[INFO] |  |  \- org.opentest4j:opentest4j:jar:1.1.0:test
12[INFO] |  \- org.junit.jupiter:junit-jupiter-api:jar:5.2.0:test
13[INFO] +- org.mockito:mockito-junit-jupiter:jar:2.19.0:test
14[INFO] |  \- org.mockito:mockito-core:jar:2.19.0:test
15[INFO] |     +- net.bytebuddy:byte-buddy:jar:1.8.10:test
16[INFO] |     +- net.bytebuddy:byte-buddy-agent:jar:1.8.10:test
17[INFO] |     \- org.objenesis:objenesis:jar:2.6:test
18[INFO] \- org.testng:testng:jar:6.14.3:test
19[INFO]    +- com.beust:jcommander:jar:1.72:test
20[INFO]    \- org.apache-extras.beanshell:bsh:jar:2.0b6:test

《mvn dependence:analyze》

此命令分析了maven项目,以识别未使用的声明和未使用的声明依赖:

1mvn dependency:analyze

出口的例子:

1[secondary_label Output]
2[INFO] --- maven-dependency-plugin:2.8:analyze (default-cli) @ Mockito-Examples ---
3[WARNING] Used undeclared dependencies found:
4[WARNING]    org.junit.jupiter:junit-jupiter-api:jar:5.2.0:test
5[WARNING]    org.mockito:mockito-core:jar:2.19.0:test
6[WARNING] Unused declared dependencies found:
7[WARNING]    org.junit.platform:junit-platform-runner:jar:1.2.0:test
8[WARNING]    org.junit.jupiter:junit-jupiter-engine:jar:5.2.0:test
9[WARNING]    org.mockito:mockito-junit-jupiter:jar:2.19.0:test

通过识别未使用的依赖性并从pom.xml文件中删除它们来减少构建大小是有用的。

《mvn archetype:generate》

此命令会生成不同类型的骨骼 Maven 项目,如JAR,Web 应用程序,Maven 网站等:

1mvn archetype:generate

出口的例子:

1[secondary_label Output]
2[INFO] ----------------------------------------------------------------------------
3[INFO] Using following parameters for creating project from Archetype: maven-archetype-quickstart:1.4
4[INFO] ----------------------------------------------------------------------------
5...
6[INFO] Project created from Archetype in dir: /Users/sammy/Desktop/maven-examples/maven-example-jar
7[INFO] ------------------------------------------------------------------------
8[INFO] BUILD SUCCESS

推荐阅读: 使用 Maven Archetypes 创建 Java 项目

mvn网站:网站

此命令为该项目生成一个网站:

1mvn site:site

在执行此命令后,您将在目标目录中看到一个网站目录。

1/Users/sammy/Desktop/maven-examples/maven-example-jar/target/site/index.html

网站目录中将有多个HTML文件,提供与项目相关的信息。

《MVN测试》

此命令运行该项目的测试案例:

1mvn test

出口的例子:

 1[secondary_label Output]
 2[INFO] -------------------------------------------------------
 3[INFO]  T E S T S
 4[INFO] -------------------------------------------------------
 5[INFO] Running TestSuite
 6first-element
 7second-element
 8Employee setName Argument = Sammy
 9...
10[INFO] Results:
11[INFO]
12[INFO] Tests run: 5, Failures: 0, Errors: 0, Skipped: 0
13[INFO]
14[INFO] ------------------------------------------------------------------------
15[INFO] BUILD SUCCESS

《mvn compile》

此命令编译了项目的源Java类:

1mvn compile

出口的例子:

1[secondary_label Output]
2[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ Mockito-Examples ---
3[INFO] Changes detected - recompiling the module!
4[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
5[INFO] Compiling 10 source files to /Users/sammy/Desktop/maven-examples/Mockito-Examples/target/classes

它与以前的mvn compiler:compile命令类似,但运行整个Maven生命周期,直到compile

mvn 验证

此命令构建了项目,运行所有测试案例,并对集成测试结果进行任何检查,以确保质量标准得到满足:

1mvn verify

Maven 选项

Maven 提供了许多命令行选项来更改 Maven 构建过程:

mvn - 帮助

此命令行选项打印了 Maven 使用情况和所有可用的选项:

1mvn -help

出口的例子:

 1[secondary_label Output]
 2usage: mvn [options] [<goal(s)>] [<phase(s)>]
 3
 4Options:
 5  -am,--also-make If project list is specified, also
 6                                   build projects required by the
 7                                   list
 8  -amd,--also-make-dependents If project list is specified, also
 9                                   build projects that depend on
10                                   projects on the list
11  -B,--batch-mode Run in non-interactive (batch)
12                                   mode (disables output color)
13  -b,--builder <arg>               The id of the build strategy to
14                                   use
15  -C,--strict-checksums Fail the build if checksums don't
16                                   match
17  -c,--lax-checksums Warn if checksums don't match
18  -cpu,--check-plugin-updates Ineffective, only kept for
19                                   backward compatibility

「mvn -f dir/pom.xml 包」

此命令行选项将从不同的位置构建一个项目:

1mvn -f dir/pom.xml package

提供pom.xml文件的位置来构建项目,当您需要从脚本中运行 Maven 构建时,这是有用的。

「mvn -o 包」

此命令行选项在 _offline 模式下运行 Maven 构建:

1mvn -o package

当您在本地存储库中下载所有所需的JAR时,它是有用的,并且您不希望Maven在远程存储库中搜索任何JAR时。

《mvn -q 包》

此命令行选项在 quiet mode 中运行 Maven 构建,以便只显示测试案例结果和错误:

1mvn -q package

《mvn - X 包》

此命令行选项打印了 Maven 版本,并在 _debug 模式中运行构建,以便显示所有消息:

1mvn -X package

出口的例子:

 1[secondary_label Output]
 2Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
 3Maven home: /Users/sammy/Downloads/apache-maven-3.6.3
 4Java version: 13.0.1, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-13.0.1.jdk/Contents/Home
 5Default locale: en_IN, platform encoding: UTF-8
 6OS name: "mac os x", version: "10.15.1", arch: "x86_64", family: "mac"
 7[DEBUG] Created new class realm maven.api
 8[DEBUG] Importing foreign pacakges into class realm maven.api
 9[DEBUG]   Imported: javax.annotation.* < plexus.core
10[DEBUG]   Imported: javax.annotation.security.* < plexus.core
11[DEBUG]   Imported: javax.enterprise.inject.* < plexus.core
12[DEBUG]   Imported: javax.enterprise.util.* < plexus.core
13[DEBUG]   Imported: javax.inject.* < plexus.core

《MV》

此命令行选项显示了 Maven 版本信息:

1mvn -v

出口的例子:

1[secondary_label Output]
2Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
3Maven home: /Users/sammy/Downloads/apache-maven-3.6.3
4Java version: 13.0.1, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-13.0.1.jdk/Contents/Home
5Default locale: en_IN, platform encoding: UTF-8
6OS name: "mac os x", version: "10.15.1", arch: "x86_64", family: "mac"

《MV包》

此命令行选项打印了 Maven 版本,然后继续构建:

1mvn -V package

它与命令相当:

1mvn -v;mvn package

「mvn -DskipTests包」

此命令行选项适用于skipTests系统属性,以跳过构建周期中的单元测试案例:

1mvn -DskipTests package

您还可以跳过测试案例执行:

1mvn -Dmaven.test.skip=true package

mvn -T 4清洁安装

此命令行选项告诉 Maven 使用指定的线程数运行并行构建:

1mvn -T 4 clean install

它在多个模块项目中有用,其中模块可以并行构建,可以缩短项目的构建时间。

参考

Published At
Categories with 技术
Tagged with
comments powered by Disqus