使用 Apache Maven - 命令及示例
Apache Maven是一个用于项目管理和理解的开源工具。该工具基于项目对象模型 (POM) 概念。借助 Apache Maven使开发人员能够在最短的时间内理解完整的开发周期和状态。这反过来又使构建过程变得更加容易,改进了开发实践并提供了高质量的项目信息。 与 Apache Maven 相关的惊人功能包括: 本指南将提供使用 Apache Maven 时涉及的常用命令,并在必要时提供示例。 可以按照我们页面上提供的指南在您的系统上安装 Apache Maven: 要验证安装,请运行: 安装 Apache Maven 后,您可以根据需要继续使用它。以下是常用 Apache Maven 命令的列表以及示例: 该命令用于编译项目的主要源代码,位于项目的src/main/java中。该命令编译主目录中的所有 Java 文件。该命令不会执行编译阶段之外的任何任务。 示例输出: 该命令用于为 Maven 项目编译 Java 源代码。它告诉 Maven 在编译代码时使用 maven-compiler-plugin。在这里,可以在 pom.xml 文件中自定义编译器的行为。您可以指定源和目标 Java 版本、编码等选项。要使用它,请运行: 该命令用于编译Maven项目的测试类。 将 Maven 项目构建为 JAR、WAR 等时使用。要使用它,请执行以下命令: 示例输出: 完成后,您将看到打印的 JAR 文件的位置。该命令在继续构建之前执行 此命令将构建并安装 JAR、WAR、pom.xml 等项目文件到本地存储库。要使用它,请运行: 示例输出: 它将工件部署到在 distributionManagement 标签下的 它用于验证并确保 Maven 项目的所有设置和所需信息都是正确的。 用于显示 Maven 项目上的依赖关系树。 示例输出: 此命令可用于查找未使用的依赖项并将其从 pom.xml 文件中删除。 该命令在生成项目框架时非常有用。它生成 JAR、Web 应用程序、Maven 站点和许多其他内容。 您还可以将该命令与所有必需的标志一起使用。例如 该命令用于创建项目的站点。要使用该命令,请运行: 站点目录中将生成包含与您的项目相关信息的 HTML 文件。要查看它们,请访问项目的以下目录。 这在运行 Maven 项目的测试用例时使用。例如: 当您需要验证是否满足质量标准时使用此命令。它运行测试用例并检查以确保一切正常。 示例输出: 该命令在删除Maven项目时使用。它清理并删除目标目录。要使用它,请切换到 Maven 目录并发出: Maven 允许使用多个选项。要查看可用选项的列表,请使用: 以下是一些最常用的 Maven 选项: 该命令用于在离线模式下运行maven build命令。仅当所有必需的 JAR 文件已下载并可在本地存储库中使用时,才使用此命令。 这会以安静模式运行构建过程,仅提供结果和错误。 从另一个路径构建项目时使用该命令。您需要指定 pom.xml 文件的位置: 该命令用于使用提供的计数运行并行 Maven 构建。该命令可用于减少构建时间,因为模块可以同时构建。例如: 该命令用于显示 Maven 版本并以调试模式运行构建。在这里,所有消息都被打印出来。 示例输出: 该命令打印 Maven 版本,然后继续构建过程。 该选项用于在构建过程中跳过测试: 本指南可以作为处理 Apache Maven 项目时的参考资料。我们提供了一些常用的 Maven 命令和示例用法。我希望这可以帮到你。 在我们的页面上查看更多信息:安装 Apache Maven
$ mvn -version
Apache Maven 3.9.4 (dfbb324ad4a7c8fb0bf182e6d91b0ae20e3d2dd9)
Maven home: /opt/maven
Java version: 11.0.20.1, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.15.0-71-generic", arch: "amd64", family: "unix"
使用 Apache Maven – 命令和示例
MVN编译
mvn compile
[INFO] Compiling 1 source file with javac [debug target 1.8] to target/classes
[WARNING] bootstrap class path not set in conjunction with -source 8
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.363 s
[INFO] Finished at: 2023-09-18T13:18:32+03:00
[INFO] ------------------------------------------------------------------------
mvn编译器:编译
$ mvn compiler:compile
[INFO]
[INFO] ----------------------< com.mycompany.app:my-app >----------------------
[INFO] Building my-app 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- compiler:3.11.0:compile (default-cli) @ my-app ---
[INFO] Changes detected - recompiling the module! :input tree
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file with javac [debug target 1.8] to target/classes
[WARNING] bootstrap class path not set in conjunction with -source 8
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.435 s
[INFO] Finished at: 2023-09-18T13:19:02+03:00
[INFO] ------------------------------------------------------------------------
mvn编译器:testCompile
$ mvn compiler:testCompile
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< com.mycompany.app:my-app >----------------------
[INFO] Building my-app 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- compiler:3.11.0:testCompile (default-cli) @ my-app ---
[INFO] Changes detected - recompiling the module! :source
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file with javac [debug target 1.8] to target/test-classes
[WARNING] bootstrap class path not set in conjunction with -source 8
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.614 s
[INFO] Finished at: 2023-09-18T13:19:38+03:00
[INFO] ------------------------------------------------------------------------
MVN包
mvn package
[INFO] Building jar: /home/thor/mvn-projects/my-app/target/my-app-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.826 s
[INFO] Finished at: 2023-09-18T13:20:05+03:00
[INFO] ------------------------------------------------------------------------
compile
、testCompile
和 test
。MVN安装
mvn install
[INFO] Installing /home/thor/mvn-projects/my-app/pom.xml to /home/thor/.m2/repository/com/mycompany/app/my-app/1.0-SNAPSHOT/my-app-1.0-SNAPSHOT.pom
[INFO] Installing /home/thor/mvn-projects/my-app/target/my-app-1.0-SNAPSHOT.jar to /home/thor/.m2/repository/com/mycompany/app/my-app/1.0-SNAPSHOT/my-app-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.838 s
[INFO] Finished at: 2023-09-18T13:21:06+03:00
[INFO] ------------------------------------------------------------------------
MVN部署
pom.xml
文件中配置的远程存储库。要提供身份验证详细信息,请将其包含在 settings.xml
文件中。mvn deploy
MVN验证
$ mvn validate
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< com.mycompany.app:my-app >----------------------
[INFO] Building my-app 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.196 s
[INFO] Finished at: 2023-09-18T13:21:59+03:00
[INFO] ------------------------------------------------------------------------
mvn依赖:树
mvn dependency:tree
[INFO] \- junit:junit:jar:3.8.1: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
.....
mvn 原型:生成
mvn archetype:generate
mvn archetype:generate -DgroupId=com.mycompany.app \
-DartifactId=my-app \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false
mvn 站点:站点
mvn site:site
$ ls target/site/
css/ dependency-info.html index.html plugins.html summary.html
dependencies.html images/ plugin-management.html project-info.html
MVN测试
$ mvn test
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.mycompany.app.AppTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.036 s -- in com.mycompany.app.AppTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.231 s
[INFO] Finished at: 2023-09-18T13:35:08+03:00
[INFO] ------------------------------------------------------------------------
MVN验证
mvn verify
INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.mycompany.app.AppTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.039 s -- in com.mycompany.app.AppTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- jar:3.3.0:jar (default-jar) @ my-app ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.564 s
[INFO] Finished at: 2023-09-18T13:35:56+03:00
[INFO] ------------------------------------------------------------------------
mvn 清洁
$ mvn clean
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< com.mycompany.app:my-app >----------------------
[INFO] Building my-app 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- clean:3.2.0:clean (default-clean) @ my-app ---
[INFO] Deleting /home/thor/mvn-projects/my-app/target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.554 s
[INFO] Finished at: 2023-09-18T13:36:22+03:00
[INFO] ------------------------------------------------------------------------
如何使用 Maven 选项
$ mvn -help
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
--color <arg> Defines the color mode of the
output. Supported are 'auto',
'always', 'never'.
-cpu,--check-plugin-updates Ineffective, only kept for
backward compatibility
-D,--define <arg> Define a user property
-e,--errors Produce execution error messages
-emp,--encrypt-master-password <arg> Encrypt master security password
-ep,--encrypt-password <arg> Encrypt server password
-f,--file <arg> Force the use of an alternate POM
file (or directory with pom.xml)
-fae,--fail-at-end Only fail the build afterwards;
allow all non-impacted builds to
continue
......
mvn -o 包
mvn -o package
mvn -q 包
mvn -q package
mvn -f dir/pom.xml 包
mvn -f dir/pom.xml package
mvn -T 4 全新安装
mvn -T 4 clean install
mvn -X 包
mvn -X package
Apache Maven 3.9.4 (dfbb324ad4a7c8fb0bf182e6d91b0ae20e3d2dd9)
Maven home: /opt/maven
Java version: 11.0.20.1, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.15.0-71-generic", arch: "amd64", family: "unix"
...
INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.832 s
[INFO] Finished at: 2023-09-18T13:38:09+03:00
[INFO] ------------------------------------------------------------------------
[DEBUG] Shutting down adapter factory; available factories [file-lock, rwlock-local, semaphore-local, noop]; available name mappers [discriminating, file-gav, file-hgav, file-static, gav, static]
[DEBUG] Shutting down 'file-lock' factory
[DEBUG] Shutting down 'rwlock-local' factory
[DEBUG] Shutting down 'semaphore-local' factory
[DEBUG] Shutting down 'noop' factory
mvn -V 包
mvn -V package
##OR
mvn -v;mvn package
mvn -DskipTests 包
mvn -DskipTests package
##OR
mvn -Dmaven.test.skip=true package
结论
更新于:4个月前
相关文章
- 【说站】python无法识别命令的解决
- iOS 图片压缩方法的示例代码
- git clean 命令详解
- git switch 命令详解
- git rebase 命令详解
- git stash 命令详解(保存开发进度)
- git fetch 命令详解
- linux 命令之查看文件内容
- git merge 命令详解
- git tag 命令详解
- git checkout 命令详解
- git help 查看命令手册
- git add 命令详解
- linux 命令之 ls 命令详解
- git commit 命令详解
- git 命令别名配置
- git remote 命令详解
- git diff 命令详解
- git log 命令
- git branch 命令详解