繁体   English   中英

Maven:在一个阶段运行插件两次,与另一个插件交错

[英]Maven: run plugin twice during a phase, interleaved with another plugin

对于我们的end-2-end测试,我们需要执行以下逻辑流程:

  1. 在数据库中创建和设置e2e模式(用户)( pre-integration-test
  2. 运行Liquibase以初始填充模式( pre-integration-test
  3. 将e2e特定的测试数据添加到数据库表( pre-integration-test
  4. 启动Tomcat( pre-integration-test
  5. 使用Protractor在Tomcat( integration-test )中运行Web应用程序
  6. 关闭Tomcat( post-integration-test
  7. 清理数据库:删除架构( post-integration-test

对于运行SQL,使用sql-maven-plugin ,但是此流程不适合常规POM布局:

  • SQL插件必须在liquibase-maven-plugin 之前之后两次pre-integration-test期间运行
  • 在SQL插件有在Tomcat的插件之前运行pre-integration-test ,但是它必须在运行post-integration-test ,这样的Tomcat关闭后的DB模式被丢弃。

据我所知,从Maven文档中可以看出,POM中插件的顺序定义了同一阶段的执行顺序,并且插件在同一个POM中不能被提及两次。

问题 :有没有办法实现这一点,除了编写一个多次调用Maven的shell脚本?

PS发现了一个类似的悬而未决的问题

鉴于下面的样本POM:

<project>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.sample</groupId>
    <artifactId>sample-project</artifactId>
    <version>0.0.2-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.5</version>
                <executions>
                    <execution>
                        <id>print-hello</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <echo message="hello there!" />
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.5.0</version>
                <executions>
                    <execution>
                        <id>exec-echo</id>
                        <phase>validate</phase>
                        <configuration>
                            <executable>cmd</executable>
                            <arguments>
                                <argument>/C</argument>
                                <argument>echo</argument>
                                <argument>hello-from-exec</argument>
                            </arguments>
                        </configuration>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.5</version>
                <executions>
                    <execution>
                        <id>print-hello-2</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <echo message="hello there 2!" />
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

我们实际配置:

  • maven-antrun-pluginhello there!打印hello there! 信息
  • exec-maven-plugin用于打印hello-from-exec消息
  • maven-antrun-plugin打印hello there 2! 信息

目标执行都附加到同一阶段, validate ,我们希望以相同的定义顺序执行。

但是,在调用时( -q选项用于完全且仅具有其输出):

mvn validate -q

我们将作为输出:

main:
     [echo] hello there!

main:
     [echo] hello there 2!
hello-from-exec

也就是说,对于同一阶段,Maven执行已定义的插件,但是为相同的插件合并所有已定义的执行(即使定义为不同的plugin部分),然后按顺序执行它们以合并定义。

不幸的是,没有机制可以避免这种合并 我们配置插件执行行为的唯一选择是:

  • inherited配置条目:

    truefalse ,这个插件的配置是否不应适用于从这一个继承的POM。 默认值为true

  • combine.childrencombine.self

    通过向配置元素的子元素添加属性来控制子POM如何从父POM继承配置。

这些选项都不会对我们有所帮助。 在这种情况下,我们需要在execution元素上使用一种merge属性,或者默认情况下具有不同的行为(也就是说,Maven应该遵循定义顺序)。


从命令行调用单个执行,如下所示:

mvn antrun:run@print-hello exec:exec@exec-echo antrun:run@print-hello-2 -q

我们会改为获得所需的输出:

main:
     [echo] hello there!
hello-from-exec

main:
     [echo] hello there 2!

但在这种情况下:

  • 我们不依赖于任何阶段
  • 我们通过命令行直接调用特定的执行(及其配置)(以及仅在Maven 3.3.1之后可用的新功能)

您可以通过脚本或通过exec-maven-plugin调用maven本身来实现完全相同,但是 - 再次 - 同样适用:没有应用阶段,只有执行序列。

暂无
暂无

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

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