【问题标题】:Can we execute exec-maven-plugin before maven-surefire-plugin?我们可以在 maven-surefire-plugin 之前执行 exec-maven-plugin 吗?
【发布时间】:2017-05-06 15:40:31
【问题描述】:

是否可以在 maven-surefire-plugin 之前运行 exec-maven-plugin,我在运行期间观察到 maven-surefire-plugin 是第一个执行的,即使标签中的序列是第二个。我的场景是执行 JAVA CLASS(使用 exec-maven-plugin )生成我的 testng.xml 并可以使用(maven-surefire-plugin)运行它。

【问题讨论】:

    标签: maven maven-surefire-plugin exec-maven-plugin


    【解决方案1】:

    首先,如果你有一个exec-maven-plugin 的执行绑定到test 阶段,那么这个执行在maven-surefire-plugin 之后执行是正常的。原因是您可能正在处理将jarwhich has a default binding of the Surefire Plugin 打包到test 阶段的项目。无论插件在 POM 中的何处声明,此默认执行始终是第一个调用的。在日志中,您将发现此执行的 id 为 default-test

    有一种方法可以利用the phases invoked before the phase test 在运行测试之前执行操作。在您的情况下,您的目标是生成测试资源testng.xml,因此使用generate-test-resources 阶段是合适的,其目的是创建测试所需的资源。因此,您只需要指定

    <phase>generate-test-resources</phase>
    

    到执行exec-maven-plugin生成testng.xml

    然后,您可以将生成的testng.xmlsuiteXmlFiles 元素一起使用,请参阅Using Suite XML Files

    【讨论】:

    • 感谢您的回答,我不确定我在这里缺少什么,请检查我添加到我的问题中的代码,并让我知道其中有什么问题和问题。我们将不胜感激。
    • @kalyanchavali 这是一个新问题,与您最初的问题无关,我鼓励您发布一个新问题。 &lt;sourceDirectory&gt;src/test/java&lt;/sourceDirectory&gt; 关心的是:你为什么要这样做? src/test/java 是测试源目录,不是主目录。此外,为什么空标签&lt;source /&gt;&lt;target /&gt;?请发布一个新问题。
    【解决方案2】:

    这就是我的实现方式:

    1. 我已经添加了测试脚本/java 主类,我想在 Cucumber 测试套件之前执行,在以下文件夹中: enter image description here

    2. 在 POM.xml 中添加以下内容...

    <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <executions>
    
                <execution>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
    
                            <source>src/test/java/BeforeSuite</source> <!-- source folder where Before Suite scripts are saved -->
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <executions>
    
                <execution>
                    <id>before-test-suite-scripts</id>
                    <phase>generate-test-resources</phase>
                    <goals>
                        <goal>java</goal>
                    </goals>
                    <configuration>
                        <mainClass>BeforeSuite.HelloBeforeSuiteScript</mainClass> <!-- <packagename>.<className> -->
                    </configuration>
                </execution>
            </executions>
        </plugin>

    当运行mvn clean verify 时,测试套件脚本将在测试套件执行之前运行。 enter image description here

    【讨论】:

      猜你喜欢
      • 2014-04-23
      • 2012-03-13
      • 2012-10-25
      • 1970-01-01
      • 2011-01-12
      • 2016-03-05
      • 2012-02-11
      • 2011-03-29
      相关资源
      最近更新 更多