简体   繁体   English

Maven 目标和阶段是什么,它们有什么区别?

[英]What are Maven goals and phases and what is their difference?

What is the difference/relation between Maven goals and phases? Maven 目标和阶段之间的区别/关系是什么? How they are related to each other?它们之间有什么关系?

Goals are executed in phases which help determine the order goals get executed in. The best understanding of this is to look at the default Maven lifecycle bindings which shows which goals get run in which phases by default.目标分阶段执行,这有助于确定目标执行的顺序。对此的最佳理解是查看默认的 Maven 生命周期绑定,它显示默认情况下哪些目标在哪些阶段运行。 The compile phase goals will always be executed before the test phase goals, which will always be executed before the package phase goals and so on. compile阶段目标将始终在test阶段目标之前执行,而test阶段目标将始终在package阶段目标之前执行,依此类推。

Part of the confusion is exacerbated by the fact that when you execute Maven you can specify a goal or a phase.当您执行 Maven 时,您可以指定一个目标或一个阶段,这一事实加剧了部分混乱。 If you specify a phase then Maven will run all phases up to the phase you specified in order (eg if you specify package it will first run through the compile phase and then the test phase and finally the package phase) and for each phase it will run all goals attached to that phase.如果您指定一个阶段,那么 Maven 将运行所有阶段,直到您按顺序指定的阶段(例如,如果您指定包,它将首先运行编译阶段,然后是测试阶段,最后是包阶段),对于每个阶段,它将运行运行与该阶段相关的所有目标。

When you create a plugin execution in your Maven build file and you only specify the goal then it will bind that goal to a given default phase.当您在 Maven 构建文件中创建插件执行并且您仅指定目标时,它将将该目标绑定到给定的默认阶段。 For example, the jaxb:xjc goal binds by default to the generate-resources phase.例如, jaxb:xjc目标默认绑定到generate-resources阶段。 However, when you specify the execution you can also explicitly specify the phase for that goal as well.但是,当您指定执行时,您也可以明确指定该目标的阶段。

If you specify a goal when you execute Maven then it will run that goal and only that goal.如果您在执行 Maven 时指定一个目标,那么它将运行该目标并且仅运行该目标。 In other words, if you specify the jar:jar goal it will only run the jar:jar goal to package your code into a jar.换句话说,如果你指定了jar:jar目标,它只会运行jar:jar目标来将你的代码打包成一个 jar。 If you have not previously run the compile goal or prepared your compiled code in some other way this may very likely fail.如果您之前没有运行编译目标或以其他方式准备编译代码,这很可能会失败。

Life cycle is a sequence of named phases .生命周期是一系列命名阶段
Phases executes sequentially.阶段按顺序执行。 Executing a phase means executes all previous phases.执行一个阶段意味着执行所有先前的阶段。

Plugin is a collection of goals also called MOJO ( M aven O ld J ava O bject).插件是目标的集合也被称为MOJO(M艾文ØLD的J avaØbject)。
Analogy : Plugin is a class and goals are methods within the class.类比:插件是一个类,目标是类中的方法。

Maven is based around the central concept of a Build Life Cycles . Maven 基于构建生命周期的中心概念。 Inside each Build Life Cycles there are Build Phases , and inside each Build Phases there are Build Goals .在每个构建生命周期内构建阶段,在每个构建阶段内构建目标

We can execute either a build phase or build goal.我们可以执行构建阶段或构建目标。 When executing a build phase we execute all build goals within that build phase.在执行构建阶段时,我们执行该构建阶段内的所有构建目标。 Build goals are assigned to one or more build phases.构建目标被分配到一个或多个构建阶段。 We can also execute a build goal directly.我们也可以直接执行构建目标。

There are three major built-in Build Life Cycles :有三个主要的内置构建生命周期

  1. default默认
  2. clean干净的
  3. site地点

Each Build Lifecycle is Made Up of Phases 每个构建生命周期都由阶段组成

For example the default lifecycle comprises of the following Build Phases :例如, default生命周期包括以下构建阶段

◾validate - validate the project is correct and all necessary information is available
◾compile - compile the source code of the project
◾test - test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
◾package - take the compiled code and package it in its distributable format, such as a JAR.
◾integration-test - process and deploy the package if necessary into an environment where integration tests can be run
◾verify - run any checks to verify the package is valid and meets quality criteria
◾install - install the package into the local repository, for use as a dependency in other projects locally
◾deploy - done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

So to go through the above phases, we just have to call one command:所以要完成上述阶段,我们只需要调用一个命令:

mvn <phase> { Ex: mvn install }

For the above command, starting from the first phase, all the phases are executed sequentially till the 'install' phase.对于上述命令,从第一个阶段开始,所有阶段都按顺序执行,直到“安装”阶段。 mvn can either execute a goal or a phase (or even multiple goals or multiple phases) as follows: mvn可以执行一个目标或一个阶段(甚至多个目标或多个阶段),如下所示:

mvn clean install plugin:goal  

However, if you want to customize the prefix used to reference your plugin, you can specify the prefix directly through a configuration parameter on the maven-plugin-plugin in your plugin's POM.但是,如果要自定义用于引用插件的前缀,可以直接通过插件 POM 中maven-plugin-plugin上的配置参数指定前缀

A Build Phase is Made Up of Plugin Goals构建阶段由插件目标组成

Most of Maven's functionality is in plugins. Maven 的大部分功能都在插件中。 A plugin provides a set of goals that can be executed using the following syntax:插件提供了一组可以使用以下语法执行的目标

 mvn [plugin-name]:[goal-name]

For example, a Java project can be compiled with the compiler-plugin's compile-goal by running mvn compiler:compile .例如,可以通过运行mvn compiler:compile使用编译器插件的 compile-goal 编译 Java 项目。

Build lifecycle is a list of named phases that can be used to give order to goal execution.构建生命周期是一个命名阶段列表,可用于为目标执行下达命令。

Goals provided by plugins can be associated with different phases of the lifecycle.插件提供的目标可以与生命周期的不同阶段相关联。 For example, by default, the goal compiler:compile is associated with the compile phase , while the goal surefire:test is associated with the test phase .例如,默认情况下,目标compiler:compilecompile phase相关联,而目标surefire:testtest phase相关联。 Consider the following command:考虑以下命令:

mvn test

When the preceding command is executed, Maven runs all goals associated with each of the phases up to and including the test phase.执行上述命令时,Maven 会运行与每个阶段相关的所有目标,直到并包括test阶段。 In such a case, Maven runs the resources:resources goal associated with the process-resources phase, then compiler:compile , and so on until it finally runs the surefire:test goal.在这种情况下,Maven 运行与process-resources阶段关联的resources:resources目标,然后是compiler:compile ,依此类推,直到它最终运行surefire:test目标。

However, even though a build phase is responsible for a specific step in the build lifecycle, the manner in which it carries out those responsibilities may vary.然而,即使构建阶段负责构建生命周期中的特定步骤,它执行这些职责的方式可能会有所不同。 And this is done by declaring the plugin goals bound to those build phases.这是通过声明绑定到这些构建阶段的插件目标来完成的。

A plugin goal represents a specific task (finer than a build phase) which contributes to the building and managing of a project.插件目标表示有助于构建和管理项目的特定任务(比构建阶段更精细)。 It may be bound to zero or more build phases.它可能绑定到零个或多个构建阶段。 A goal not bound to any build phase could be executed outside of the build lifecycle by direct invocation.未绑定到任何构建阶段的目标可以通过直接调用在构建生命周期之外执行。 The order of execution depends on the order in which the goal(s) and the build phase(s) are invoked.执行顺序取决于调用目标和构建阶段的顺序。 For example, consider the command below.例如,考虑下面的命令。 The clean and package arguments are build phases, while the dependency:copy-dependencies is a goal (of a plugin). cleanpackage参数是构建阶段,而dependency:copy-dependencies是(插件的)目标。

mvn clean dependency:copy-dependencies package

If this were to be executed, the clean phase will be executed first (meaning it will run all preceding phases of the clean lifecycle, plus the clean phase itself), and then the dependency:copy-dependencies goal, before finally executing the package phase (and all its preceding build phases of the default lifecycle).如果要执行此操作,将首先执行clean阶段(意味着它将运行 clean 生命周期的所有先前阶段,加上clean阶段本身),然后是dependency:copy-dependencies目标,最后执行package阶段(以及默认生命周期的所有先前构建阶段)。

Moreover, if a goal is bound to one or more build phases, that goal will be called in all those phases.此外,如果目标绑定到一个或多个构建阶段,则该目标将在所有这些阶段中调用。

Furthermore, a build phase can also have zero or more goals bound to it.此外,构建阶段也可以绑定零个或多个目标。 If a build phase has no goals bound to it, that build phase will not execute.如果构建阶段没有与之绑定的目标,则该构建阶段将不会执行。 But if it has one or more goals bound to it, it will execute all those goals.但是如果它绑定了一个或多个目标,它将执行所有这些目标。

Built-in Lifecycle Bindings 内置生命周期绑定
Some phases have goals bound to them by default.默认情况下,某些阶段具有与其绑定的目标。 And for the default lifecycle, these bindings depend on the packaging value.对于默认生命周期,这些绑定取决于打包值。

Maven Architecture: Maven架构:

在此处输入图片说明

Reference 1 参考文献 1
Reference 2 参考文献2

Eclipse sample for Maven Lifecycle Mapping Maven 生命周期映射的 Eclipse 示例

Maven 生命周期映射的 Eclipse 示例

The chosen answer is great, but still I would like to add something small to the topic.选择的答案很棒,但我仍然想在主题中添加一些小东西。 An illustration.一个插图。

It clearly demonstrates how the different phases binded to different plugins and the goals that those plugins expose.它清楚地展示了不同阶段如何绑定到不同的插件以及这些插件公开的目标。

So, let's examine a case of running something like mvn compile :所以,让我们检查一个运行类似mvn compile的例子:

  • It's a phase which execute the compiler plugin with compile goal这是一个以编译目标执行编译器插件阶段
  • Compiler plugin got different goals.编译器插件有不同的目标。 For mvn compile it's mapped to a specific goal, the compile goal.对于mvn compile它被映射到一个特定的目标,即 compile 目标。
  • It's the same as running mvn compiler:compile和运行mvn compiler:compile

Therefore, phase is made up of plugin goals .因此,阶段由插件目标组成

在此处输入图片说明

Link to the reference链接到参考

The definitions are detailed at the Maven site's page Introduction to the Build Lifecycle , but I have tried to summarize :这些定义在Maven 站点的构建生命周期介绍页面中有详细说明,但我试图 总结

Maven defines 4 items of a build process: Maven 定义了构建过程的 4 个项目:

  1. Lifecycle生命周期

    Three built-in lifecycles (aka build lifecycles ): default , clean , site .三个内置生命周期(又名构建生命周期): defaultcleansite ( Lifecycle Reference ) 生命周期参考

  2. Phase阶段

    Each lifecycle is made up of phases , eg for the default lifecycle: compile , test , package , install , etc.每个生命周期由阶段组成,例如对于default生命周期: compiletestpackageinstall等。

  3. Plugin插入

    An artifact that provides one or more goals.提供一个或多个目标的工件。

    Based on packaging type ( jar , war , etc.) plugins' goals are bound to phases by default.基于打包类型( jarwar等),插件的目标默认绑定到阶段。 ( Built-in Lifecycle Bindings ) 内置生命周期绑定

  4. 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 .在 POM 中配置插件时需要指定一个或多个目标。 Additionally, in case a plugin does not have a default phase defined, the specified goal(s) can be bound to a phase.此外,如果插件没有定义默认阶段,则可以将指定的目标绑定到阶段。

Maven can be invoked with: Maven 可以通过以下方式调用:

  1. a phase (eg clean , package )一个阶段(例如cleanpackage
  2. <plugin-prefix>:<goal> (eg dependency:copy-dependencies ) <plugin-prefix>:<goal> (例如dependency:copy-dependencies
  3. <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal> (eg org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile ) <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal> (例如org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile

with one or more combinations of any or all, eg:具有任何或全部的一种或多种组合,例如:

mvn clean dependency:copy-dependencies package

I believe a good answer is already provided, but I would like to add an easy-to-follow diagram of the different 3 life-cycles ( build , clean , and site ) and the phases in each.我相信已经提供了一个很好的答案,但我想添加一个易于理解的图表,说明不同的 3 个生命周期( buildcleansite )以及每个生命周期中的阶段。

在此处输入图片说明

The phases in bold - are the main phases commonly used.粗体的阶段 - 是常用的主要阶段。

Credit to Sandeep Jindal and Premraj.感谢 Sandeep Jindal 和 Premraj。 Their explanation help me to understand after confused about this for a while.他们的解释帮助我在对此困惑了一段时间后理解了。

I created some full code examples & some simple explanations here https://www.surasint.com/maven-life-cycle-phase-and-goal-easy-explained/ .我在这里创建了一些完整的代码示例和一些简单的解释https://www.surasint.com/maven-life-cycle-phase-and-goal-easy-explained/ I think it may help others to understand.我认为这可能有助于其他人理解。

In short from the link, You should not try to understand all three at once, first you should understand the relationship in these groups:简而言之,从链接中,您不应该试图同时了解所有三个,首先您应该了解这些组中的关系:

  • Life Cycle vs Phase生命周期与阶段
  • Plugin vs Goal插件与目标

1. Life Cycle vs Phase 1. 生命周期与阶段

Life Cycle is a collection of phase in sequence see here Life Cycle References .生命周期是一系列阶段的集合,请参见此处的生命周期参考 When you call a phase , it will also call all phase before it.当你调用一个阶段时,它也会调用它之​​前的所有阶段

For example, the clean life cycle has 3 phases ( pre-clean, clean, post-clean ).例如,清洁生命周期有 3 个阶段(清洁前、清洁、清洁后)。

mvn clean

It will call pre-clean and clean .它将调用pre-cleanclean

2. Plugin vs Goal 2. 插件 vs 目标

Goal is like an action in Plugin . Goal就像Plugin 中的一个动作。 So if plugin is a class, goal is a method.所以如果插件是一个类,目标就是一个方法。

you can call a goal like this:你可以这样称呼目标:

mvn clean:clean

This means "call the clean goal, in the clean plugin" (Nothing relates to the clean phase here. Don't let the word"clean" confusing you, they are not the same!)这意味着“在清理插件中调用清理目标”(这里没有与清理阶段相关的内容。不要让“清理”这个词混淆你,它们是不一样的!)

3. Now the relation between Phase & Goal: 3.现在阶段和目标之间的关系:

Phase can (pre)links to Goal (s).For example, normally, the clean phase links to the clean goal.阶段可以(预)链接到目标。例如,正常情况下,清理阶段链接到清理目标。 So, when you call this command:所以,当你调用这个命令时:

mvn clean

It will call the pre-clean phase and the clean phase which links to the clean:clean goal.它将调用 pre-clean 阶段和链接到 clean:clean 目标的 clean 阶段。

It is almost the same as:它几乎与以下相同:

mvn pre-clean clean:clean

More detail and full examples are in https://www.surasint.com/maven-life-cycle-phase-and-goal-easy-explained/更多细节和完整示例在https://www.surasint.com/maven-life-cycle-phase-and-goal-easy-explained/

Maven working terminology having phases and goals. Maven 工作术语具有阶段和目标。

Phase:Maven phase is a set of action which is associated with 2 or 3 goals阶段:Maven 阶段是一组与 2 或 3 个目标相关联的动作

exmaple:- if you run mvn clean示例:- 如果您运行 mvn clean

this is the phase will execute the goal mvn clean:clean这是将执行目标 mvn clean:clean 的阶段

Goal:Maven goal bounded with the phase目标:Maven目标以阶段为界

for reference http://books.sonatype.com/mvnref-book/reference/lifecycle-sect-structure.html供参考http://books.sonatype.com/mvnref-book/reference/lifecycle-sect-structure.html

There are following three built-in build lifecycles:有以下三个内置构建生命周期:

  • default默认
  • clean干净的
  • site地点

Lifecycle default -> [validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy]生命周期默认-> [验证、初始化、生成源、处理源、生成资源、处理资源、编译、处理类、生成测试源、处理测试源、生成测试资源、处理-测试资源,测试编译,过程测试类,测试,准备包,包,预集成测试,集成测试,后集成测试,验证,安装,部署]

Lifecycle clean -> [pre-clean, clean, post-clean]生命周期清洁-> [预清洁、清洁、后清洁]

Lifecycle site -> [pre-site, site, post-site, site-deploy]生命周期站点-> [站点前、站点、站点后、站点部署]

The flow is sequential, for example, for default lifecycle, it starts with validate , then initialize and so on...流程是顺序的,例如,对于默认生命周期,它从验证开始,然后初始化等等......

You can check the lifecycle by enabling debug mode of mvn ie, mvn -X <your_goal>您可以通过启用mvn调试模式来检查生命周期,即mvn -X <your_goal>

In reference to Pace's answer ,关于佩斯的回答

If you specify a goal when you execute Maven then it will run that goal and only that goal.如果您在执行 Maven 时指定了一个目标,那么它将运行该目标并且只运行该目标。 In other words, if you specify the jar:jar goal it will only run the jar:jar goal to package your code into a jar.换句话说,如果您指定 jar:jar 目标,它只会运行 jar:jar 目标来将您的代码打包到一个 jar 中。

there is an exception to this statement.此声明有一个例外。 Maven Plugin API allows a goal to trigger execution of a lifecycle phase. Maven Plugin API允许目标触发生命周期阶段的执行。

Consider the following project:考虑以下项目:

<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>

When you execute the goal run defined in spring-boot-maven-plugin当你执行spring-boot-maven-plugin中定义的目标run

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

it prints它打印

[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] ------------------------------------------------------------------------

This is because the goal definition in spring-boot-maven-plugin-XXXjar/META-INF/maven/plugin.xml contains <executePhase>test-compile</executePhase> , which executes test-compile and all the preceding phases.这是因为spring-boot-maven-plugin-XXXjar/META-INF/maven/plugin.xml中的目标定义包含<executePhase>test-compile</executePhase> ,它执行 test-compile 和所有前面的阶段。

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

Moreover, because of the default bindings for the "jar" packaging, few other goals are executed.此外,由于“jar”包装的默认绑定,很少有其他目标被执行。 If the packaging is changed to "pom", the same command results with如果包装更改为“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] ------------------------------------------------------------------------

because there are no default bindings for test-compile or any previous phase and this packaging type.因为没有针对测试编译或任何先前阶段和此包装类型的默认绑定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM