How can I set environment variables via gradle script

  Kiến thức lập trình

I have a gradle script that in the test section sets a lot of testing environment variables. These testing variables are used across different packages and I would like to extract this into a separate gradle file that I could have all the variables in a single place.

Currently I have something like

test {
   environment('XXXX', System.getenv("XXXX"))
   ....
}

I attempted to extract all this into a single gradle script at the root like so:

ext.setupTestEnv = {
   environment('XXXX', System.getenv("XXXX"))
   ....
}

and then call it like:

test {
    useJUnitPlatform()
    apply from: '../../gradle/testEnvSetup.gradle'
    setupTestEnv()
}

However, this failed I think because environment can only be called from test. I tried a couple other things but was unable to figure out how to actually setup the environment variables in setupTestEnv. I was wondering how I might be able to set/unset the environment variables from a root gradle script so that I can maintain them in a single place.

LEAVE A COMMENT