如何将来自Maven和Surefire插件的run命令行参数作为属性传递到pom.xml中?

slsn1g29  于 2023-05-22  发布在  Maven
关注(0)|答案(1)|浏览(178)

如何将参数从命令行传递到pom.xml文件中的属性?
我有下面的pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>BusniessApp</groupId>
    <artifactId>Test-Automation</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            [...]
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                    <properties>
                        <property>
                            <name>testnames</name>
                            <value>Android,iOS</value>
                        </property>
                    </properties>
          [...........]
    </build>

    <dependencies>
        [...]
    </dependencies>
</project>

我按照Passing command line arguments from Maven as properties in pom.xml中的说明运行了mvn clean test -Dtestnames=iOS,但它仍然使用pom.xml中设置的默认值运行。

rqqzpn5f

rqqzpn5f1#

找到解决办法了,至少对我有用。我必须在POM中创建一个属性,然后将其传递给构建部分。最后变成这样

<properties>
        <maven.compiler.release>11</maven.compiler.release>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <myArgument>DefaultValue</myArgument>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>11</release>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                    <properties>
                        <testnames>${myArgument}</testnames>
                    </properties>
                </configuration>
            </plugin>
        </plugins>
    </build>

相关问题