Maven目标和阶段之间的区别/关系是什么?它们之间有什么关系?


当前回答

这些定义在Maven站点的构建生命周期介绍页面中有详细说明,但是我已经尝试总结:

Maven定义了构建过程的4项:

Lifecycle Three built-in lifecycles (aka build lifecycles): default, clean, site. (Lifecycle Reference) Phase Each lifecycle is made up of phases, e.g. for the default lifecycle: compile, test, package, install, etc. Plugin An artifact that provides one or more goals. Based on packaging type (jar, war, etc.) plugins' goals are bound to phases by default. (Built-in Lifecycle Bindings) Goal The task (action) that is executed. A plugin can have one or more goals. One or more goals need to be specified when configuring a plugin in a POM. Additionally, in case a plugin does not have a default phase defined, the specified goal(s) can be bound to a phase.

Maven可以通过以下方式调用:

阶段(如清洁、打包) <plugin-prefix>:<goal>(例如:copy-dependencies) <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>(例如:org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile)

混合的:任意或全部的一个或多个组合,例如:

mvn clean dependency:copy-dependencies package

其他回答

有以下三个内置的构建生命周期:

默认的 清洁 网站

生命周期默认值->[验证、初始化、生成源、流程源、生成资源、流程资源、编译、流程类、生成测试源、流程测试源、生成测试资源、流程测试资源、测试编译、流程测试类、测试、准备包、包、预集成测试、集成测试、后集成测试、验证、安装、部署]

生命周期清洁->[预清洁,清洁,后清洁]

生命周期站点->[站点前,站点,站点后,站点部署]

流是顺序的,例如,对于默认的生命周期,它从验证开始,然后初始化等等…

您可以通过启用mvn的调试模式来检查生命周期,即mvn -X <your_goal>

归功于Sandeep Jindal和Premraj。我一时糊涂,他们的解释让我明白了。

我在这里创建了一些完整的代码示例和一些简单的解释https://www.surasint.com/maven-life-cycle-phase-and-goal-easy-explained/。我想这可以帮助别人理解。

简而言之,你不应该试图一次理解所有这三个组,首先你应该理解这三个组之间的关系:

生命周期vs阶段 插件vs目标

1. 生命周期vs阶段

生命周期是按顺序的阶段的集合,请参阅这里的生命周期参考。当你调用一个相位时,它也会调用它之前的所有相位。

例如,清洁生命周期有3个阶段(清洁前、清洁后)。

mvn clean

它会调用pre-clean和clean。

2. 插件vs目标

目标就像Plugin中的一个动作。如果plugin是一个类,goal是一个方法。

你可以这样称呼一个进球:

mvn clean:clean

这意味着“在清洁插件中调用清洁目标”(这里与清洁阶段无关。不要让“干净”这个词混淆了你,它们不是一样的!)

3.现在,阶段和目标之间的关系:

阶段可以(预)链接到目标。例如,通常,清洁阶段与清洁目标相关联。所以,当你调用这个命令时:

mvn clean

它将调用预清洁阶段和清洁阶段,后者与清洁目标相关联。

它几乎等同于:

mvn pre-clean clean:clean

更多细节和完整的例子见https://www.surasint.com/maven-life-cycle-phase-and-goal-easy-explained/

关于佩斯的回答,

如果您在执行Maven时指定了一个目标,那么它将运行该目标,且仅运行该目标。换句话说,如果您指定了jar:jar目标,它将只运行jar:jar目标来将您的代码打包到一个jar中。

这种说法有一个例外。Maven插件API允许目标触发生命周期阶段的执行。

考虑以下项目:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.example</groupId>
  <artifactId>simple-maven-project</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>
</project>

当你执行spring-boot-maven-plugin中定义的目标运行时

mvn org.springframework.boot:spring-boot-maven-plugin:run

它打印

[INFO] ------------------< org.example:simple-maven-project >------------------
[INFO] Building simple-maven-project 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] >>> spring-boot-maven-plugin:3.0.0:run (default-cli) > test-compile @ simple-maven-project >>>
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ simple-maven-project ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\Bartosz\IdeaProjects\simple-maven-project\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ simple-maven-project ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ simple-maven-project ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\Bartosz\IdeaProjects\simple-maven-project\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ simple-maven-project ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] <<< spring-boot-maven-plugin:3.0.0:run (default-cli) < test-compile @ simple-maven-project <<<
[INFO] 
[INFO] 
[INFO] --- spring-boot-maven-plugin:3.0.0:run (default-cli) @ simple-maven-project ---
[INFO] ------------------------------------------------------------------------

这是因为spring-boot-maven-plugin-X.X.X.jar/META-INF/maven/plugin.xml中的目标定义包含<executePhase>test-compile</executePhase>,它执行test-compile和所有前面的阶段。

<mojo>
    <goal>run</goal>
    (...)
    <executePhase>test-compile</executePhase>
    (...)
</mojo>

此外,由于“jar”打包的默认绑定,很少执行其他目标。如果将打包更改为“pom”,则相同的命令将生成

[INFO] ------------------< org.example:simple-maven-project >------------------
[INFO] Building simple-maven-project 1.0-SNAPSHOT
[INFO] --------------------------------[ pom ]---------------------------------
[INFO] 
[INFO] >>> spring-boot-maven-plugin:3.0.0:run (default-cli) > test-compile @ simple-maven-project >>>
[INFO] 
[INFO] <<< spring-boot-maven-plugin:3.0.0:run (default-cli) < test-compile @ simple-maven-project <<<
[INFO] 
[INFO] 
[INFO] --- spring-boot-maven-plugin:3.0.0:run (default-cli) @ simple-maven-project ---
[INFO] ------------------------------------------------------------------------

因为对于test-compile或任何之前的阶段和此打包类型没有默认绑定。

我相信已经提供了一个很好的答案,但我想添加一个易于理解的图,说明不同的3个生命周期(构建、清洁和站点)以及每个生命周期的阶段。

粗体的相位是常用的主要相位。

这些定义在Maven站点的构建生命周期介绍页面中有详细说明,但是我已经尝试总结:

Maven定义了构建过程的4项:

Lifecycle Three built-in lifecycles (aka build lifecycles): default, clean, site. (Lifecycle Reference) Phase Each lifecycle is made up of phases, e.g. for the default lifecycle: compile, test, package, install, etc. Plugin An artifact that provides one or more goals. Based on packaging type (jar, war, etc.) plugins' goals are bound to phases by default. (Built-in Lifecycle Bindings) Goal The task (action) that is executed. A plugin can have one or more goals. One or more goals need to be specified when configuring a plugin in a POM. Additionally, in case a plugin does not have a default phase defined, the specified goal(s) can be bound to a phase.

Maven可以通过以下方式调用:

阶段(如清洁、打包) <plugin-prefix>:<goal>(例如:copy-dependencies) <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>(例如:org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile)

混合的:任意或全部的一个或多个组合,例如:

mvn clean dependency:copy-dependencies package