我有一个具有共同属性的文件 common.gradle
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 props 时出现以下错误
What went wrong: Execution failed for task ':props'. Could not find property 'custom' on task ':props'.
Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
最佳答案
def custom =
声明一个局部变量,该变量在 commons.gradle
外部不可见。相反,您可以在 Project
对象上声明一个额外属性:
ext.custom = ... // shorthand for `project.ext.custom = ...`
用法将保持不变(例如 custom.group
,不是 ext.custom.group
)。
如果您想在同一(多项目)构建中的所有构建脚本之间共享custom
,那么将commons.gradle
应用到根项目就足够了,因为项目属性是从父项目继承到子项目的。
关于java - gradle在不相关的不同项目之间共享属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21917123/