Thanks for quick replay I think my last example was really unfortunate and didn’t present what I am trying to achieve. Hopefully this one will present the problem that I am struggling with.
apply plugin: DocumentationPlugin
publisher {
info {
name 'Publisher'
}
books {
quickStart {
title = 'Quick Start'
}
userGuide {
title = 'User Guide'
}
developerGuide {
title = 'Developer Guide'
}
}
}
class DocumentationPlugin implements Plugin<Project> {
void apply(Project project) {
def publisher = project.extensions.create('publisher', Publisher)
def publisherInfo = publisher.extensions.create('info', PublisherInfo)
def books = project.container(Book)
books.all { book ->
println "Creating task $book.name -> Title: $book.title, "
project.tasks.create("displayBook${book.name.capitalize()}", DisplayBook) {
publisherName = publisherInfo.name
title = book.title
}
}
publisher.extensions.books = books
}
}
class PublisherInfo {
String name
}
class Publisher {
}
class Book {
final String name
String title
Book(String name) {
this.name = name
}
}
class DisplayBook extends DefaultTask {
String publisherName
String title
@TaskAction
def displayBook() {
println "$name -> Title: $title, Publisher : $publisherName"
}
}
I am trying to create a task that will use DSL to sets itself. Based on the DSL also it will create a list of the tasks. If you run following code you will get following list of the tasks:
displayBookDeveloperGuide
displayBookQuickStart
displayBookUserGuide
Now this is what I am struggling with and what I am expecting to get after running e.g. displayBookDeveloperGuide
// Expected
displayBookDeveloperGuide -> Title: Developer Guide, Publisher : Publisher
But what I am actually getting is:
// This is what happens title is not set
displayBookDeveloperGuide -> Title: null, Publisher : Publisher
Hopefully this example presents the problem better.