2014-02-20 142 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到根項目,作爲項目屬性從父遺傳給孩子的項目。