在 pom.xml 中配置 Maven 插件两次

Configuring a Maven plugin twice in a pom.xml

我正在处理一个大型 Maven Java 项目,其中包含 TestNG 自动化测试套件和 Cucumber 自动化测试套件。我意识到这并不理想,但不同的测试套件是由不同的子团队在项目的不同阶段编写的。展望未来,我们打算将这个项目拆分成更小的项目,但目前我们仍然坚持这种组合。

surefire 插件可用于 运行 来自 Maven 的这些测试,但需要为我们的 pom.xml 中的每个测试配置不同的插件。

对于Cucumber,我们将其与cucumber-jvm-parallel-plugin结合使用,其配置如下:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.20.1</version>
            <configuration>
                <forkCount>${threads}</forkCount>
                <reuseForks>true</reuseForks>
                <includes>
                    <include>**/Parallel*IT.class</include>
                </includes>
            </configuration>
        </plugin>

对于我们的 TestNG 测试,配置如下:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.20.1</version>
            <configuration>
                <suiteXmlFiles>${file}</suiteXmlFiles>
                <skipTests>false</skipTests>
                <properties>
                    <property>
                        <name>suitethreadpoolsize</name>
                        <value>${threads}</value>
                    </property>
                </properties>
            </configuration>
        </plugin>

我们不是 Maven 专家,因此,目前,我们只是注释掉我们不想用于 运行ning 套件的插件版本。显然,这很麻烦而且不是最佳实践,因此我将非常感谢您提供有关我们应如何解决此问题的任何建议。我们能否在 pom.xml 中合法地定义插件两次,并以某种方式传递一个标志来指示哪个版本应该是 运行?非常感谢您阅读我的问题。

使用maven profiles到select正确的配置:

添加此代码段:

<profiles>
    <profile>
        <id>Cucumber</id>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <forkCount>${threads}</forkCount>
                    <reuseForks>true</reuseForks>
                    <includes>
                        <include>**/Parallel*IT.class</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </profile>
    <profile>
        <id>TestNG</id>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <suiteXmlFiles>${file}</suiteXmlFiles>
                    <properties>
                        <property>
                            <name>suitethreadpoolsize</name>
                            <value>${threads}</value>
                        </property>
                    </properties>
                </configuration>
            </plugin>
        </plugins>
    </profile>
</profiles>

并改变这个

<plugins>
  <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.20.1</version>
    </plugin>
   <!-- delete on of the entries -->
</plugins>

您必须在命令行中指定所需的配置文件:

   mvn test -P Cucumber
   mvn test -P TestNG 

bit 你也可以同时运行:

mvn test -P Cucumber -P TestNG 
mvn test -P Cucumber,TestNG 

为您想要的每组测试创建一个下游 Maven 项目运行。前期工作更多,但很快就会有回报。