【发布时间】:2017-01-20 20:14:36
【问题描述】:
我想为现有类型添加一个函数,在我的例子中是NativeLibrarySpec。
我尝试使用gradle extensions,它即将工作,但现在我想概括它以便能够使用它,就像它是NativeLibrarySpec 的 DSL 的标准功能一样。
问题是我只能在它的配置(包含我的函数的块)之后访问实例,所以它失败了,因为它在我能够链接它之前尝试调用specialConfig...
这里是代码(不要在意这个例子是针对本地软件 C++ 的):
// File: build.gradle
apply plugin: 'cpp'
class SpecialConfig {
NativeComponentSpec componentSpec
SpecialConfig(NativeComponentSpec componentSpec) {
this.componentSpec = componentSpec
}
def something(boolean enabled) {
componentSpec.sources {
cpp {
// Some important stuffs
}
}
}
}
model {
components {
main(NativeLibrarySpec) {
// How to bring this out ??
project.extensions.create('specialConfig', SpecialConfig, it)
// This is the new functionality I want to use
specialConfig {
something(true)
}
}
}
}
这是另一个示例,但它仅适用于项目。* https://dzone.com/articles/gradle-goodness-extending-dsl
【问题讨论】: