Home > Mobile >  Can't pass parameters/sytem properties with gradlew command line
Can't pass parameters/sytem properties with gradlew command line

Time:09-25

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?

CodePudding user response:

If your tests are being launched by the gradle JavaExec task then you can configure gradle to pass through its system properties like this:  

// The run task added by the application plugin
// is also of type JavaExec.
tasks.withType(JavaExec) {
    // Assign all Java system properties from
    // the command line to the JavaExec task.
    systemProperties System.properties
}

See the user guide for full details.

CodePudding user response:

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

  • Related