Maven `pre` 和 `post` 阶段

标签 maven

prepost执行相关阶段时总是执行阶段?例如,如果我这样做 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-cleanclean 之后, 如果我只是执行 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/

    相关文章:

    javax.ws.rs.NotSupportedException : Could not find message body reader for type: class

    java - 无法在kafka流中加载类 "org.slf4j.impl.StaticLoggerBinder"?

    java - 我的配置 pom.xml 已跳过测试。但现在我想用命令行运行一次测试。命令如何?

    git-flow:制作 "release candidates"/QA 网络 Artifact 的工作流程

    eclipse - 如何在 Eclipse 中将 Spark 添加到 Maven 项目?

    java - Maven 无法解析 C3P0 依赖项

    eclipse - Maven Eclipse 多模块阴影依赖

    java - 由于 pom.xml 中的相对路径错误,Keycloak Authenticator SPI 示例无法工作

    java - 有哪些与 java、spring、hibernate、maven 相关的好博客值得阅读?

    java - Gradle Application 插件的 Maven 替代(等效)