Maven的“前”和“后”阶段

时间:2018-09-23 19:38:29

标签: maven

在执行关联的阶段时,是否总是执行var s = "Author: Tiara Jordan Date: 02/23/17 Title: The Big Bang Theory"; var t = "t"; console.log(s.replace( new RegExp("\\b(Date|Author|Title)\\b|" + t.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), "gi"), (x,y) => y ? y : `<strong>${x}</strong>`) )pre阶段?例如,如果我做post,这也将执行mvn clean阶段吗?

我正在看https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference,其中说:

  

以下列出了默认,清理和站点的所有构建阶段   生命周期,按照给定的顺序执行,直到   指定的那个。

严格来说,由于mvn post-cleanpost-clean之后,所以如果我只是clean就不应该执行它。但是我的直觉是不同的-尽管我没有找到一种方法来验证这一点,因为maven stdout不会打印其执行的阶段。

任何人都可以权衡答案以及您如何验证?

1 个答案:

答案 0 :(得分:3)

您可以将插件(例如echo-maven-plugin)绑定到clean生命周期的各个阶段,以帮助验证是否/何时执行每个阶段。

例如,给定以下插件定义:

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

调用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'

因此,那里没有post-clean阶段的调用。

调用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 ...

同样,这里没有调用post-clean阶段。这意味着maven-clean-plugin(也许没有其他)未绑定到post-clean

调用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'

因此,基于以上测试,我认为以下陈述是正确的:

    呼叫post-clean不会调用
  • clean
  • {li> post-clean仅在您明确调用post-clean时被调用(注意:直接调用pre-post-阶段是不寻常的)