我有一个类似下面插件的东西,有一个外部命名空间,在其中有一个对象的单一“具体”实例(mother
)和另一个集合(children
)。
family {
mother {
firstname = 'John'
lastname = 'Cleese'
}
children {
son {
firstName = 'John'
lastName = 'Cleese'
}
daughter {
firstName = 'Jane'
lastName = 'Cleese'
}
}
}
我能够根据各种示例添加集合对象并读取变量,但不确定如何添加具体实例。
我该如何在扩展对象上定义它?
以下是显示问题的代码 - 我想将mother
作为插件中的单个实例添加。
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.*
class Person
{
final String name
String firstName
String lastName
Person(String name) { this.name = name }
}
class FamilyExtension {
final NamedDomainObjectContainer<Person> children
Person mother
Person father
FamilyExtension(children) {
this.children = children
}
def children(Closure closure) {
children.configure(closure)
}
}
class FamilyPlugin implements Plugin<Project> {
void apply(Project project) {
project.task('sayHello', type: DefaultTask) << {
println(
"Hello ${project.family.children["son"].firstName} " +
"and hello ${project.family.children["daughter"].firstName} ")
}
def children = project.container(Person)
project.extensions.create("family", FamilyExtension, children)
}
}
apply plugin: FamilyPlugin
family {
// How do I add support for this?
// mother {
// firstname = 'John'
// lastname = 'Cleese'
// }
children {
son {
firstName = 'John'
lastName = 'Cleese'
}
daughter {
firstName = 'Jane'
lastName = 'Cleese'
}
}
}