Search code examples
javaxmlmaventestngmaven-surefire-plugin

How do I pass run command line arguments from Maven and Surefire plugin as properties in 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 Passing command line arguments from Maven as properties in pom.xml but it still ran with the default value set in the pom.xml


Solution

  • 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

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