gradle为gradle中的所有项目设置全局属性

igg*_*ggy 5 gradle

我想以类似于gradle.properties或的方式在gradle中设置属性(例如来自settings.gradle)-D.可能吗?

以下代码演示了我正在尝试做什么,但它不起作用:

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()
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑

我希望在settings.gradle中定义此属性,并在所有其他.gradle文件中可见

EDIT2

我有许多构建机器,我不想指定变量的值buildMachine(在设置和其他.gradle文件中),我不希望它与-P或一起传递gradle.properties,因为它似乎有可能弄清楚这个属性纯粹在gradle中

小智 1

您可以简单地为 rootProject 设置此属性,并在所有其他 gradle 脚本或子项目中,从中读取。

为了清楚起见..

在你的根 build.gradle 中

ext { buildMachine = getName() + "_" + getArchitecture() }

在你的子项目中..

ext { println rootProject.buildMachine }