2014-02-20 40 views
0

我有具有公共属性共享属性

def custom = [ 
status: 'SNAPSHOT', 
group: 'com.custom.proj', 
version: [ 
    core: '1.2.0.0', 
    modle: '1.2.0.0', 
    base: '1.2.0.0' 
    ] 
] 

和的build.gradle

apply from: 'file:///E:/gradle/common.gradle' 

task props << { 
    println "group" + custom.group 
} 

使用它,当我运行gradle这个文件common.gradle这没有关系道具变得如下错误

出错了: 任务':道具'的执行失败。 无法在任务':道具'上找到属性“自定义”。

尝试: 与--stacktrace选项获取堆栈跟踪运行。使用--info或--debug选项运行以获取更多日志输出。

回答

1

def custom =声明了一个局部变量,这个变量在commons.gradle之外是不可见的。相反,你可以在Project对象上声明额外的属性

ext.custom = ... // shorthand for `project.ext.custom = ...` 

的使用将保持不变(例如custom.groupext.custom.group)。

如果你想分享custom在所有建立在相同(多项目)脚本生成,这是不够好,适用commons.gradle到根项目,作为项目属性从父遗传给孩子的项目。