简体   繁体   English

Maven“前”和“后”阶段

[英]Maven `pre` and `post` phases

Are the pre and post phases always executed when I execute the associated phase?当我执行相关阶段时,是否总是执行pre阶段和post阶段? For example, if I do mvn clean , will this execute the mvn post-clean phase, too?例如,如果我执行mvn clean ,这是否也会执行mvn post-clean阶段?

I was looking at https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference , in which it said:我在看https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference ,其中说:

The following lists all build phases of the default, clean and site lifecycles, which are executed in the order given up to the point of the one specified.下面列出了默认、清理和站点生命周期的所有构建阶段,它们按照指定的时间点之前的顺序执行

So strictly speaking, since post-clean comes after clean , it should not be executed if I just do mvn clean .所以严格来说,由于post-clean是在clean之后进行的,所以如果我只执行mvn clean就不应该执行它。 But my gut feel is different - although, I didn't find a way to verify this, as the maven stdout doesn't print the phase it's executing.但是我的直觉是不同的 - 虽然,我没有找到一种方法来验证这一点,因为 Maven 标准输出不打印它正在执行的阶段。

Could anyone weigh in on the answer and how you verified?任何人都可以权衡答案以及您如何验证?

You can bind a plugin (such as echo-maven-plugin ) to the phases of the clean lifecycle to help verify if/when each phase is executed.您可以将插件(例如echo-maven-plugin )绑定到clean生命周期的阶段,以帮助验证是否/何时执行每个阶段。

For example, given the following plugin definition:例如,给定以下插件定义:

<plugin>
    <groupId>com.github.ekryd.echo-maven-plugin</groupId>
    <artifactId>echo-maven-plugin</artifactId>
    <version>1.2.0</version>
    <executions>
        <execution>
            <id>pre-clean</id>
            <phase>pre-clean</phase>
            <goals>
                <goal>echo</goal>
            </goals>
            <configuration>
                <message>In 'pre-clean'</message>
            </configuration>
        </execution>
        <execution>
            <id>clean</id>
            <phase>clean</phase>
            <goals>
                <goal>echo</goal>
            </goals>
            <configuration>
                <message>In 'clean'</message>
            </configuration>
        </execution>
        <execution>
            <id>post-clean</id>
            <phase>post-clean</phase>
            <goals>
                <goal>echo</goal>
            </goals>
            <configuration>
                <message>In 'post-clean'</message>
            </configuration>
        </execution>
    </executions>
</plugin>

Invoking mvn clean will result in the following output:调用mvn clean将产生以下输出:

$ mvn clean
[INFO] Scanning for projects...
[INFO] 
[INFO] --- echo-maven-plugin:1.2.0:echo (pre-clean) @ sandbox ---
[INFO] In 'pre-clean'
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ sandbox ---
[INFO] Deleting target
[INFO] 
[INFO] --- echo-maven-plugin:1.2.0:echo (clean) @ sandbox ---
[INFO] In 'clean'

So, there's no invocation of the post-clean phase there.所以,那里没有调用post-clean阶段。

Invoking mvn clean compile will result in the following output:调用mvn clean compile将产生以下输出:

$ mvn clean compile
[INFO] Scanning for projects...
[INFO] 
[INFO] --- echo-maven-plugin:1.2.0:echo (pre-clean) @ sandbox ---
[INFO] In 'pre-clean'
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ sandbox ---
[INFO] 
[INFO] --- echo-maven-plugin:1.2.0:echo (clean) @ sandbox ---
[INFO] In 'clean'
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ sandbox ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.5.1:compile (default-compile) @ sandbox ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 5 source files to ...

Again, there's no invocation of the post-clean phase there.同样,那里没有调用post-clean阶段。 This implies that the maven-clean-plugin (and perhaps nothing else) is not bound to post-clean .这意味着 maven-clean-plugin(也许没有别的)不受post-clean约束。

Invoking mvn post-clean will result in the post-clean phase being invoked ...调用mvn post-clean导致调用post-clean阶段......

$ mvn post-clean
[INFO] Scanning for projects...
[INFO] 
[INFO] --- echo-maven-plugin:1.2.0:echo (pre-clean) @ sandbox ---
[INFO] In 'pre-clean'
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ sandbox ---
[INFO] Deleting target
[INFO] 
[INFO] --- echo-maven-plugin:1.2.0:echo (clean) @ sandbox ---
[INFO] In 'clean'
[INFO] 
[INFO] --- echo-maven-plugin:1.2.0:echo (post-clean) @ sandbox ---
[INFO] In 'post-clean'

So, based on the above test I think the following statements are true:因此,基于上述测试,我认为以下陈述是正确的:

  • post-clean is not invoked when you call clean调用clean不会调用post-clean clean
  • post-clean is only called when you explicitly invoke post-clean (note: invoking the pre- and post- phases directly is unusual) post-clean在您显式调用post-clean时调用(注意:直接调用pre-post-阶段是不寻常的)

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

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