basse
basse

Reputation: 1339

Add environment variable to Cucumber task with Gradle

How do I add an environment variable to Gradle's Cucumber task? My configuration is straight out of Gradle's getting started:

task cucumber() {
    dependsOn assemble, compileTestJava
    doLast {
        javaexec {
            main = "io.cucumber.core.cli.Main"
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
            args = ['--plugin', 'pretty', '--glue', 'gradle.cucumber', 'src/test/resources']
        }
    }
}

I managed to set the variable for unit tests like so:

test {
    environment 'OHTU_TS_ENV', 'test'
    testLogging.showStandardStreams = true
    systemProperties System.getProperties()
}

But environment method doesn't work for Cucumber's task unless I make it type type:Exec which breaks the rest of the task.

Thank you!

Upvotes: 2

Views: 973

Answers (2)

Ilya K
Ilya K

Reputation: 21

Try using System.getenv().

task publishResults {
    doLast {
        exec {
            workingDir "build/cucumber-report/"
            def curlCommand = "curl -v --basic --user " + System.getenv('CREDS') + ' -F file=@output_results.zip ' + jiraUrl
            commandLine curlCommand.split(' ')
        }
    }
}

Upvotes: 2

Chriki
Chriki

Reputation: 16338

I wonder how you tried to set the environment variable? Doing it like so should actually work (tested with Gradle 6.0.1):

task cucumber() {
    dependsOn assemble, compileTestJava
    doLast {
        javaexec {
            main = "io.cucumber.core.cli.Main"
            environment 'OHTU_TS_ENV', 'test'
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
            args = ['--plugin', 'pretty', '--glue', 'gradle.cucumber', 'src/test/resources']
        }
    }
}

Upvotes: 0

Related Questions