Skip to content

Can’t pass parameters/sytem properties with gradlew command line

I try to pass parameters with gradlew command line, but can’t. I successfully pass the Optional parameter with XML file, but need have the possibility to pass with gradle CMD

Gradle task:

    task task1(type: Test) {
        useTestNG() {
            suites './src/test/resources/testng/task1.xml'
        }
    }

XML:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="test1">

    <test name="test1">
        <parameter name="ipAmount" value="20"/>
        <classes>
            <class name="tests.traffic.Test1"/>
        </classes>
    </test>

</suite>

Code:

    @Test(testName = "Name", description = "description")
    @Parameters({"ipAmount"})
    public void createTrafficTest(
            @Optional("1") Integer ipAmount
    ){
        final String IP_AMOUNT_PARAMETER = System.getProperty("ipAmount");

        System.out.println(ipAmount);//20
        System.out.println(IP_AMOUNT_PARAMETER);//null
    }

gradle CMD:

    gradlew clean task1 -DipAmount=2 
    gradlew clean task1 -PipAmount=2 

In both cases I get the same result: ipAmount=20, IP_AMOUNT_PARAMETER = null

How to correctly pass parameters from the gradle cmd?

Answer

As explained also in the comment!

All you need to do is:

task task1(type: Test) {
systemProperties project.properties.subMap(["ipAmount"])
        useTestNG() {
            suites './src/test/resources/testng/task1.xml'
        }
    }

Then you can pass the property using gradlew clean task1 -PipAmount=2

Finally the systemProperties is a map so you can add more variables like ["ipAmount", "anotherParam", "moreParam"]