是 pre
和 post
执行相关阶段时总是执行阶段?例如,如果我这样做 mvn clean
, 这会执行 mvn post-clean
相也?
我在看 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.
所以严格来说,自从
post-clean
在 clean
之后, 如果我只是执行 mvn clean
就不应该执行.但我的直觉是不同的——虽然,我没有找到一种方法来验证这一点,因为 maven stdout 不会打印它正在执行的阶段。任何人都可以权衡答案以及您如何验证吗?
最佳答案
您可以将插件(例如 echo-maven-plugin
)绑定(bind)到 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(也许没有别的)没有绑定(bind)到 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
时调用post-clean
是 仅限 当你显式调用 post-clean
时调用(注意:直接调用 pre-
和 post-
阶段是不寻常的)关于Maven `pre` 和 `post` 阶段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52469611/