If you are working on Java, you must know Maven. Maven is the most popular project and dependency management tool for Java applications. Maven provides a lot of commands and options to help us in our day to day tasks.

如果您使用Java,则必须了解Maven。 Maven是最流行的Java应用程序项目和依赖管理工具。 Maven提供了许多命令和选项来帮助我们完成日常任务。

Maven命令 (Maven Commands)

Let’s look into some popular and must know maven commands. We will use a sample Maven project to showcase the command output.

让我们研究一些流行的必须知道的Maven命令。 我们将使用一个示例Maven项目来展示命令输出。

1.清洁 (1. mvn clean)

This command cleans the maven project by deleting the target directory. The command output relevant messages are shown below.

此命令通过删除目标目录来清理Maven项目。 命令输出相关消息如下所示。

$ mvn clean
...
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-example-jar ---
[INFO] Deleting /Users/pankaj/Desktop/maven-examples/maven-example-jar/target
...
$

2. mvn编译器:compile (2. mvn compiler:compile)

This command compiles the java source classes of the maven project.

该命令编译maven项目的java源类。

$ mvn compiler:compile
...
[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/pankaj/Desktop/maven-examples/maven-example-jar/target/classes
...
$

3. mvn编译器:testCompile (3. mvn compiler:testCompile)

This command compiles the test classes of the maven project.

此命令编译maven项目的测试类。

$ mvn compiler:testCompile
...
[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/pankaj/Desktop/maven-examples/maven-example-jar/target/test-classes
...
$

4. mvn包 (4. mvn package)

This command builds the maven project and packages them into a JAR, WAR, etc.

此命令将构建Maven项目并将其打包到JAR,WAR等中。

$ mvn package
...
[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/pankaj/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/pankaj/Desktop/maven-examples/maven-example-jar/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-example-jar ---
[INFO] Surefire report directory: /Users/pankaj/Desktop/maven-examples/maven-example-jar/target/surefire-reports-------------------------------------------------------T E S T S
-------------------------------------------------------
Running com.journaldev.maven.classes.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 secResults :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/pankaj/Desktop/maven-examples/maven-example-jar/target/maven-example-jar-0.0.1-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
...

The output shows the location of the JAR file just before the “BUILD SUCCESS” message. Notice the package goal executes compile, testCompile, and test goals before packaging the build.

输出显示JAR文件的位置,紧接在“ BUILD SUCCESS”消息之前。 请注意,打包目标在打包构建版本之前先执行compile,testCompile和test目标。

5. mvn安装 (5. mvn install)

This command builds the maven project and installs the project files (JAR, WAR, pom.xml, etc) to the local repository.

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

$ mvn install
...
[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/pankaj/Desktop/maven-examples/maven-example-jar/target/maven-example-jar-0.0.1-SNAPSHOT.jar to /Users/pankaj/.m2/repository/com/journaldev/maven/maven-example-jar/0.0.1-SNAPSHOT/maven-example-jar-0.0.1-SNAPSHOT.jar
[INFO] Installing /Users/pankaj/Desktop/maven-examples/maven-example-jar/pom.xml to /Users/pankaj/.m2/repository/com/journaldev/maven/maven-example-jar/0.0.1-SNAPSHOT/maven-example-jar-0.0.1-SNAPSHOT.pom
...

6. mvn部署 (6. mvn deploy)

This command is used to deploy the artifact to the remote repository. The remote repository should be configured properly in the project pom.xml file distributionManagement tag. The server entries in the maven settings.xml file is used to provide authentication details.

此命令用于将工件部署到远程存储库。 应该在项目pom.xml文件distributionManagement标记中正确配置远程存储库。 maven settings.xml文件中的服务器条目用于提供身份验证详细信息。

7. mvn验证 (7. mvn validate)

This command validates the maven project that everything is correct and all the necessary information is available.

此命令验证Maven项目是否一切正确,并且所有必要的信息均可用。

8. mvn依赖项:tree (8. mvn dependency:tree)

This command generates the dependency tree of the maven project.

此命令生成Maven项目的依赖关系树。

$ mvn dependency:tree
...
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ Mockito-Examples ---
[INFO] com.journaldev.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

9. mvn依赖关系:分析 (9. mvn dependency:analyze)

This command analyzes the maven project to identify the unused declared and used undeclared dependencies. It’s useful in reducing the build size by identifying the unused dependencies and then remove it from the pom.xml file.

此命令分析maven项目以标识未使用的已声明和已使用的未声明依赖项。 通过标识未使用的依赖项并将其从pom.xml文件中删除,可以减小构建大小。

$ mvn dependency:analyze
...
[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
...
$

10. mvn原型:generate (10. mvn archetype:generate)

Maven archetypes is a maven project templating toolkit. We can use this command to generate a skeleton maven project of different types, such as JAR, web application, maven site, etc.

Maven原型是一个Maven项目模板工具包。 我们可以使用此命令生成不同类型的框架Maven项目,例如JAR,Web应用程序,Maven站点等。

Recommended Reading: Creating a Java Project using Maven Archetypes

推荐读物: 使用Maven原型创建Java项目

11. mvn网站:site (11. mvn site:site)

This command generates a site for the project. You will notice a “site” directory in the target after executing this command. There will be multiple HTML files inside the site directory that provides information related to the project.

此命令为项目生成一个站点。 执行此命令后,您将在目标中注意到一个“站点”目录。 网站目录中将有多个HTML文件,这些文件提供与项目有关的信息。

Maven site Command

Maven站点命令

12.MVN测试 (12. mvn test)

This command is used to run the test cases of the project using the maven-surefire-plugin.

此命令用于使用maven-surefire-plugin运行项目的测试用例。

$ mvn test
...
[INFO] --- maven-surefire-plugin:2.22.0:test (default-test) @ Mockito-Examples ---
[INFO]
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running TestSuite
first-element
second-element
Employee setName Argument = Pankaj
...
[INFO] Results:
[INFO]
[INFO] Tests run: 5, Failures: 0, Errors: 0, Skipped: 0
...
$

13. mvn编译 (13. mvn compile)

It’s used to compile the source Java classes of the project.

它用于编译项目的源Java类。

$ mvn compile
...
[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/pankaj/Desktop/maven-examples/Mockito-Examples/target/classes
...
$

14.MVN验证 (14. mvn verify)

This command build the project, runs all the test cases and run any checks on the results of the integration tests to ensure quality criteria are met.

此命令将构建项目,运行所有测试用例,并对集成测试的结果进行任何检查,以确保符合质量标准。

Maven选项 (Maven Options)

Maven provides a lot of command-line options to alter the maven build process. Let’s look at some of the important maven options.

Maven提供了许多命令行选项来更改Maven构建过程。 让我们看一些重要的Maven选项。

15. mvn-帮助 (15. mvn -help)

This command prints the maven usage and all the available options for us to use.

此命令将打印Maven用法以及所有可用选项供我们使用。

Maven Help

Maven帮助

16. mvn -f maven-example-jar / pom.xml软件包 (16. mvn -f maven-example-jar/pom.xml package)

This command is used to build a project from a different location. We are providing the pom.xml file location to build the project. It’s useful when you have to run a maven build from a script.

此命令用于从其他位置构建项目。 我们提供pom.xml文件位置来构建项目。 当您必须通过脚本运行Maven构建时,这很有用。

17. mvn -o软件包 (17. mvn -o package)

This command is used to run the maven build in the offline mode. It’s useful when we have all the required JARs download in the local repository and we don’t want Maven to look for any JARs in the remote repository.

此命令用于在脱机模式下运行Maven构建。 当我们在本地存储库中下载了所有必需的JAR且不希望Maven在远程存储库中查找任何JAR时,这很有用。

18. mvn -q程序包 (18. mvn -q package)

Runs the maven build in the quiet mode, only the test cases results and errors are displayed.

在安静模式下运行Maven构建,仅显示测试用例结果和错误。

$ mvn -q package         -------------------------------------------------------T E S T S
-------------------------------------------------------
Running com.journaldev.maven.classes.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.006 secResults :Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
$

19. mvn -X程序包 (19. mvn -X package)

Prints the maven version and runs the build in the debug mode. It’s opposite of the quiet mode and you will see a lot of debug messages in the console.

打印Maven版本并在调试模式下运行构建。 它与安静模式相反,您将在控制台中看到很多调试消息。

mvn -X Debug Mode

mvn -X调试模式

20. mvn -v (20. mvn -v)

Used to display the maven version information.

用于显示Maven版本信息。

$ mvn -v
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /Users/pankaj/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"
$

21. mvn -V程序包 (21. mvn -V package)

This command prints the maven version and then continue with the build. It’s equivalent to the commands mvn -v;mvn package.

此命令将打印Maven版本,然后继续进行构建。 等效于命令mvn -v;mvn package

22. mvn -DskipTests包 (22. mvn -DskipTests package)

The skipTests system property is used to skip the unit test cases from the build cycle. We can also use -Dmaven.test.skip=true to skip the test cases execution.

skipTests系统属性用于从构建周期中跳过单元测试用例。 我们还可以使用-Dmaven.test.skip=true跳过测试用例的执行。

23. mvn -T 4程序包 (23. mvn -T 4 package)

This command tells maven to run parallel builds using the specified thread count. It’s useful in multiple module projects where modules can be built in parallel. It can reduce the build time of the project.

该命令告诉maven使用指定的线程数运行并行构建。 它在可以并行构建模块的多个模块项目中很有用。 它可以减少项目的构建时间。

Maven命令备忘单 (Maven Commands Cheat Sheet)

Maven Commands Cheat Sheet

Maven命令备忘单

参考资料 (References)

  • Maven PluginsMaven插件
  • Maven CLI Options ReferenceMaven CLI选项参考

翻译自: https://www.journaldev.com/33645/maven-commands-options-cheat-sheet

20多个Maven命令和选项(备忘单)相关推荐

  1. Cheat—— 给Linux初学者和管理员一个终极命令行备忘单

    当你不确定你所运行的命令,尤其是那些使用了许多选项的复杂命令时,你会怎么做?在这种情况下,我们使用man pages来获取帮助.还有一些其它的选择可能包括像'help','whereis'和'what ...

  2. linux mt命令,Cheat—— 给Linux初学者和管理员一个终极命令行备忘单

    当你不确定你所运行的命令,尤其是那些使用了许多选项的复杂命令时,你会怎么做?在这种情况下,我们使用man pages来获取帮助.还有一些其它的选择可能包括像'help','whereis'和'what ...

  3. freedos 常用命令_庆祝FreeDOS 24周年:有用的命令备忘单

    freedos 常用命令 FreeDOS在6月29日满24岁. FreeDOS是一个完整的,免费的,与DOS兼容的操作系统,您可以使用它来玩经典的DOS游戏 , 运行商务软件或开发嵌入式系统. 在MS ...

  4. KVM 虚拟机 virsh 命令备忘单

    这是一个全面的 virsh 命令备忘单: virsh 是 KVM 虚拟机的命令管理用户界面.virsh 可用于创建.暂停.重新启动和关闭域.此外,virsh 可用于列出您的虚拟化管理程序平台中可用的当 ...

  5. RedHat Yum 命令备忘单

    Yum 是Red Hat和基于 Red Hat 的 Linux 发行版中的包管理器,它允许您管理 Red Hat 系统上软件的各个方面. 你不会在这里详细学习 Yum 命令,相反,这个 Yum 备忘单 ...

  6. c# ui 滚动 分页_UI备忘单:分页,无限滚动和“加载更多”按钮

    c# ui 滚动 分页 重点 (Top highlight) When you have a lot of content, you have to rely on one of these thre ...

  7. hp-ux 单用户 启动_UX备忘单:搜索与浏览

    hp-ux 单用户 启动 重点 (Top highlight) When designing search results and interest sites, you have to keep i ...

  8. element ui字段_ui备忘单下拉字段

    element ui字段 重点 (Top highlight) Dropdowns get a lot of flak from the UI world – and if we are honest ...

  9. Nmap备忘单:从探索到漏洞利用(Part 2)

    Nmap备忘单:从探索到漏洞利用(Part 2) 东二门陈冠希 2016-03-29 +8 共102110人围观,发现6个不明物体 WEB安全工具 这是我们的第二期NMAP备忘单(第一期在此).基本上 ...

最新文章

  1. 远程桌面连接由于网络错误而丢失
  2. 02/03_Pytorch安装、Conda安装Pythorch,换源、pytorch官网、验证、安装jupyter、卸载、安装、启动jupyter、配置Jupyter notebook、使用
  3. JQuery的Ajax技术
  4. C++编程思想:父类函数隐藏
  5. node --- [express项目] 开发环境下使用morgan控制台输出访问信息
  6. 详解linux下安装python3环境
  7. python获取网络信息_利用psutil获取网络信息
  8. 鸿蒙系统都有谁参与,华为鸿蒙系统功能有什么特别的-华为鸿蒙系统有什么新功能...
  9. 为什么存取周期大于存取时间_为什么开放存取很重要
  10. linux下查找进程及终止进程操作的相关命令
  11. html table导出到Excel中,走后台保存文件,js并调用另保存
  12. Tensor基本操作
  13. Codeforces Round #422 (Div. 2) D. My pretty girl Noora [数论][dp]
  14. 外汇期货市场的组织结构
  15. vba 读取图片尺寸
  16. HEVC编码结构:Slice和Tile
  17. Python第三方库turtle的应用
  18. 微信小程序模板信息【详】
  19. http 状态码查询
  20. Miniconda的安装与使用及pip在conda环境中的安装

热门文章

  1. php提交字符串中有加号(+)时会后台得到空格的问题
  2. spark视频-spark机器学习
  3. [转载] Python杂谈 | (6) numpy中array()和asarray()的区别
  4. [转载] 【Python进阶】4-2 多态 | 什么是多态 / 静态语言vs动态语言 / python中多态
  5. [转载] Java获取嵌套的json串里的返回结果
  6. Sun Virtualbox说明文件的Bug
  7. [Luogu P2893][USACO08FEB]修路Making the Grade
  8. Cabloy-CMS:动静结合,解决Hexo痛点问题
  9. 13 MySQL--存储过程
  10. 固定日期时间倒计时,倒计时不可点击,普通倒计时