Accessing environmen t variables inside buildscrip t {..}

I would like to access some environment variables inside buildscript {…} closure, so that I can set some more variables based on that, and then use them to resolve my dependancies. I keep my PROD dependancy stuff under release repo, and the Test version of those stuff under snapshot repo.

I am trying to come up with a single build.gradle file, which I should be calling with a -Denv switch, for “prod” or “test” , and based on that the dependencies will be downloaded and resolved.

Some thing like:

buildscript {

repositories {

maven {

name ‘release’

url “XXSomeReleaseRepoXX”

}

maven {

name ‘snapshot’

url “XXSomeSnapshotRepoXX”

}

}

environment = System.properties[“env”] if (environment == “prod”) {

version = “1.0” } else {

version = “1.0-SNAPSHOT” }

dependencies {

classpath “com.XXX:abc:${version}+”

classpath “com.XXX:abc:${version}+”

} }

Is this kind of configuration supported inside buildscript {…} closure since buildscript gets the special treatment and gets processed as soon as the script is loaded?

I am receiving errors suggesting me that neither the properties nor the variable exist for this closure or the rootProject configuration!!

Thanks in advance for help.

First, do you mean system properties or environment variables? These are two different things. Second, can you be more specific about the errors you get?

Why are you doing all of this inside ‘buildscript { … }’? I think it would be better to version your build script dependencies separately from the dependencies of the software you are building.

Thanks for the reply.

I meant system properties here, which I will be setting up while calling gradle with -Dxxx. For the buildscript { … } section, I was also assuming something similar to what you stated,

So is the assumption is made in gradle, that build script dependencies should be stable/fixed, and set inside buildscript { … } section, and can not be configured?

And I can put the dependencies of the software I am building in a configurable manner in other places of the build script.

So is the assumption is made in gradle, that build script dependencies should be stable/fixed, and set inside buildscript { … } section, and can not be configured?

Gradle doesn’t make any assumptions about this (did you see the edit to my post?). I’m just saying that in general, it’s undesirable to have a build system whose behavior can change at any time, without any action on the user’s side, and without the user being in control.

Thanks for the reply.

Actually in this scenario, the user will decide by passing command line option, to select the dependencies for the buildscript itself.

The build script may use different dependencies, and user will tell it which one to select.

If possible, may you please suggest a way to achieve this?

You may refer to the snippet below, where I am trying access command line option inside buildscript closure but its failing. Is there any alternate way to achieve this?

buildscript {

repositories {

maven {

name ‘release’

url “XXSomeReleaseRepoXX”

}

maven {

name ‘snapshot’

url “XXSomeSnapshotRepoXX”

}

}

environment = System.properties[“env”]

if (environment == “prod”) {

version = “1.0”

} else {

version = “1.0-SNAPSHOT”

}

dependencies {

classpath “com.XXX:abc:${version}+”

classpath “com.XXX:abc:${version}+”

}

}

You ask for help without telling me what exactly you tried, and how it is failing…