apply plugin: NestedNamedDomainObjectSetsPlugin
shelf {
name = "Mike's Shelf"
books {
quickStart {
}
userGuide {
}
developerGuide {
}
}
magazines {
foo {
}
bar {
}
baz {
}
}
}
task inspectShelf << {
println "$shelf.name has ${shelf.books.size()} books"
shelf.books.each { book ->
println " $book.name -> $book.sourceFile"
}
println "$shelf.name has ${shelf.magazines.size()} magazines"
shelf.magazines.each { magazine ->
println " $magazine.name -> $magazine.sourceFile"
}
}
class NestedNamedDomainObjectSetsPlugin implements Plugin<Project> {
void apply(Project project) {
project.extensions.create("shelf", Shelf, project)
// MUST UNCOMMENT TO ACCESS 'shelf.books'
// project.extensions.books = project.shelf.books
// MUST UNCOMMENT TO ACCESS 'shelf.magazines'
// project.extensions.magazines = project.shelf.magazines
}
}
class Shelf {
String name
final NamedDomainObjectSet<Book> books
final NamedDomainObjectSet<Magazine> magazines
Shelf(Project project) {
books = project.container(Book)
books.all {
sourceFile = project.file("src/books/$name")
}
magazines = project.container(Magazine)
magazines.all {
sourceFile = project.file("src/magazines/$name")
}
}
}
class Book {
final String name
File sourceFile
Book(String name) {
this.name = name
}
}
class Magazine {
final String name
File sourceFile
Magazine(String name) {
this.name = name
}
}
I fixed the example, test with gradle inspectShelf
I want my plugin to create an extension called ‘shelf’ of type Shelf. Shelf has a name and two NamedDomainObjectSets, books and magazines. My assumption was that when I registered Shelf as an extension, it would see books and magazines and make them available so I could configure them as I try to do in the example.
It only works if I explicitly add books and magazines as extensions:
project.extensions.books = project.shelf.books
project.extensions.magazines = project.shelf.magazines
It does not work if I try any other name:
project.extensions.hiddenBooks = project.shelf.books
project.extensions.hiddenMagazines = project.shelf.magazines
I’m relatively new to Gradle and Groovy but have lots of Java experience. It seems like what I want to do should be possible, but I’m not grasping the problem or why it works when I explicitly set books and magazines as extensions to the project. I can access them fine using shelf.books and shelf.magazines, but it will not configure properly without setting them as extensions.
Thanks,
Michael