首页 > 解决方案 > 如何使用不同的系统属性值执行两个完全相同的 Maven 配置文件?

问题描述

我的pom文件中有以下提到的实现

<profile>
        <id>test</id>
        <activation>
            <activeByDefault>true</activeByDefault>
            <property>
                <name>test</name>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>xml-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <phase>compile</phase>
                            <goals>
                                <goal>transform</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <transformationSets>
                            <transformationSet>
                                <includes>
                                    <include>set.xml</include>
                                </includes>
                                <dir>test/resources/</dir>
                                <stylesheet>
                                    test/resources/ser.xsl
                                </stylesheet>
                                <outputDir>test/resources</outputDir>
                            </transformationSet>
                        </transformationSets>
                    </configuration>
                    <dependencies>
                        <dependency>
                            <groupId>net.sf.saxon</groupId>
                            <artifactId>saxon</artifactId>
                            <version>8.7</version>
                        </dependency>
                    </dependencies>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                    <inherited>false</inherited>
                    <configuration>
                        <suiteXmlFiles>
                            <suiteXmlFile>testng.xml</suiteXmlFile>
                        </suiteXmlFiles>
                        <systemProperties>
                            <property>
                                <name>maven.test.haltafterfailure</name>
                                <value>false</value>
                            </property>
                            <property>
                                <name>java.io.tmpdir</name>
                                <value>${basedir}/target/</value>
                            </property>
                            <property>
                                <name>isValid</name>
                                <value>false</value>
                            </property>
                        </systemProperties>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>

我希望这个配置文件再次执行,系统属性isValidtrue 当我复制这个配置文件配置并再次添加它时,isValid等于true第一个配置文件配置被覆盖。

从 Maven 文档...

除非使用上述方法之一激活同一 POM 中的另一个配置文件,否则此配置文件将自动对所有构建处于活动状态。当 pom 中的配置文件在命令行上或通过其激活配置激活时,默认情况下处于活动状态的所有配置文件都会自动停用。...

我尝试添加两个执行,如下所示:

                    <executions>
                        <execution>
                            <phase>compile</phase>
                            <goals>
                                <goal>transform</goal>
                            </goals>
                            <configuration>.... </configuration>
                        </execution>
                        <execution>
                            <phase>compile</phase>
                            <goals>
                                <goal>transform</goal>
                            </goals>
                            <configuration>.... </configuration>
                        </execution>
                    </executions>

但我不知道如何使用xml-maven-plugin<suiteXmlFile>设置系统变量和属性<execution>

我需要使用什么插件来实现我的要求?

有没有办法使用系统属性 isValid 运行这 2 个配置文件,true同时false使用不同的<suiteXmlFile>值(不同的 testng 文件)?

标签: javamavenbuildmaven-2maven-3

解决方案


如果您复制配置文件,那么您定义了两次相同的插件,因此配置被覆盖。

相反,定义两个<executions>相同的插件。


推荐阅读