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

huangapple go评论67阅读模式
英文:

How do I pass run command line arguments from Maven and Surefire plugin as properties in pom.xml?

问题

你可以使用以下命令来将命令行参数传递到pom.xml文件中的属性:

mvn clean test -DargLine="-Dtestnames=iOS"

这将通过argLine参数将-Dtestnames=iOS传递给Surefire插件,以覆盖在pom.xml文件中设置的默认值。

英文:

How do I pass arguments from command line to properties in pom.xml file?

I have the following pom file

<?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>

I tired mvn clean test -Dtestnames=iOS as stated in https://stackoverflow.com/questions/7513319/passing-command-line-arguments-from-maven-as-properties-in-pom-xml but it still ran with the default value set in the pom.xml

答案1

得分: 0

我找到了解决方案,至少对我有效。我不得不在我的POM中创建一个属性,然后将其传递给构建部分。最终看起来像这样:

<properties>
    <maven.compiler.release>11</maven.compiler.release>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <myArgument>默认值</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>
英文:

Figured out the solution, at least one that works for me. I had to create a property in my POM then pass that to the build section.
Ended up looking like this

&lt;properties&gt;
        &lt;maven.compiler.release&gt;11&lt;/maven.compiler.release&gt;
        &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
        &lt;myArgument&gt;DefaultValue&lt;/myArgument&gt;
    &lt;/properties&gt;

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

huangapple
  • 本文由 发表于 2023年3月31日 04:23:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75892701.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定