在将域对象添加到命名对象集合中使用的域对象内时遇到一些麻烦。示例插件:
class DevelopmentEnvironmentInstallation {
String hest
}
class Product {
final String name
DevelopmentEnvironmentInstallation developmentEnvironmentInstallation
Product(String name) {
this.name = name
}
void developmentEnvironmentInstallation(Action<? super DevelopmentEnvironmentInstallation> action) {
action.execute(developmentEnvironmentInstallation)
}
}
class PlatformPlugin implements Plugin<Project> {
void apply(Project project) {
def products = project.container(Product)
project.extensions.products = products
products.all {
developmentEnvironmentInstallation = project.getObjects().newInstance(DevelopmentEnvironmentInstallation)
}
}
}
apply plugin: PlatformPlugin
products {
prod1 {
}
prod2 {
developmentEnvironmentInstallation {
hest = "pony"
}
}
}
task listProducts {
doLast {
products.each { product -> println "$product.name"}
}
}
在分配了“ hest”的语句中,“ hest”似乎捕获了Product对象的作用域,而不是预期的DevelopmentEnvironmentInstallation对象。有关如何正确执行此操作的任何建议。我得到了错误
无法为产品类型的对象设置未知属性“ hest”。
当我只嵌套域对象而不使用命名对象容器时,一切正常。例如工作正常:
class DevelopmentEnvironmentInstallation {
String hest
}
class Product {
DevelopmentEnvironmentInstallation developmentEnvironmentInstallation
void developmentEnvironmentInstallation(Action<? super DevelopmentEnvironmentInstallation> action) {
action.execute(developmentEnvironmentInstallation)
}
}
class PlatformPlugin implements Plugin<Project> {
void apply(Project project) {
project.extensions.product = project.getObjects().newInstance(Product)
project.product.developmentEnvironmentInstallation = project.getObjects().newInstance(DevelopmentEnvironmentInstallation)
}
}
apply plugin: PlatformPlugin
product {
developmentEnvironmentInstallation {
hest = "pony"
}
}