I would like to set a property from within gradle (for example from settings.gradle) in a way similar to gradle.properties
or -D
. Is it possible?
The following code demonstrates what I am trying to do but its not working:
import org.gradle.internal.os.OperatingSystem
def getArchitecture() {
return System.getProperty("os.arch")
}
def getName() {
if(OperatingSystem.current().isMacOsX()) {
return "darwin"
} else if(OperatingSystem.current().isLinux()) {
return "linux"
} else {
throw Exception("The operating system you use is not supported.")
}
}
allprojects {
ext {
// this variable has to be visible from all the projects
// and .gradle files in the same way as if it was set
// from gradle.properties file
buildMachine = getName() + "_" + getArchitecture()
}
}
I would like this property to be defined in settings.gradle and be visible in all other .gradle file
I have a number of build machines and I don't want to specify the value of the variable buildMachine
(in settings and in other .gradle
files) and I wouldn't like it to be passed with -P
or with gradle.properties
, because it seems its possible to figure out this property purely within gradle
The subproject producer defines a task named buildInfo that generates a properties file containing build information e.g. the project version. You can then map the task provider to its output file and Gradle will automatically establish a task dependency.
You could simply set this property for the rootProject and in all other gradle scripts or subprojects, read from it..
For clarity sake..
in your root build.gradle
ext {
buildMachine = getName() + "_" + getArchitecture()
}
and in your subprojects..
ext {
println rootProject.buildMachine
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With