How to access gradle parameter in java code

I have experience with java and I am new to gradle and I joined a project where I need to modify the gradle file.

Here is my build.gradlefile

apply plugin: 'java'
apply plugin: 'idea'

sourceCompatibility = 1.5
version = '1.0'
dependencies {
    testCompile 'org.testng:testng:6.9.10',
                'org.seleniumhq.selenium:selenium-java:2.53.0'
}

test {
    useTestNG()
    testLogging.showStandardStreams = true
}

Then I run my test suite using the following command from the mac terminal ./build test

I want to pass a parameter named environment Based on this value of this parameter, I need to configure my URLs and run tests for this environment. Something like ./build test environment=devor./build test environment=qa

And in my java code I would do something like this

if(env == 'dev') {
    url = "my dev url";
    user = "my dev user name"
} else if(env == 'qa') {
    url = "my qa url";
    user = "my qa user name"
}

How to pass this parameter to the terminal? A small fragment of how I can use this parameter in my code will be very useful (my java code does not have a main method).

. , . .

+4
1

gradle . :

./gradlew test -Denv=dev

./gradle test -Penv=dev

, , build.gradle, , , :

test {
    systemProperty 'env', System.properties['env'] ?: 'dev'
}

:

test {
    systemProperty 'env', project.hasProperty('env') ? project.env : 'dev'
}

:

System.getProperty("env")

.

+12

All Articles