Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gradle set a global property for all projects from gradle

Tags:

gradle

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()
    }
}

Edit

I would like this property to be defined in settings.gradle and be visible in all other .gradle file

Edit2

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

like image 323
iggy Avatar asked Jul 30 '15 08:07

iggy


People also ask

What is subprojects in 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.


1 Answers

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 }

like image 176
msuper Avatar answered Oct 12 '22 22:10

msuper