Fredrick Eisele
07/16/2024, 3:04 PMThomas Broyer
07/16/2024, 3:11 PMproject.getTasks().withType(MyTask.class).configureEach(task -> {
task.getProperty().convention(extension.getProperty())
})Fredrick Eisele
07/16/2024, 3:22 PMapply method of the plugin?Vampire
07/16/2024, 3:38 PMVampire
07/16/2024, 3:39 PMThomas Broyer
07/16/2024, 4:13 PMPmd task inherit their defaults from the project-level pmd extension, and default to use the project-level java toolchain (I was looking at this just this weekend as I was coding a replacement plugin for de.thetaphi.forbiddenapis (before realizing that I should rather code that DefaultLocale check for Error Prone)).
IMO, if a plugin provides task types (in its JAR) and registers a project-level extension to configure defaults for those tasks, then those defaults should apply to all tasks, whether registered by the plugin itself or manually.Vampire
07/16/2024, 4:23 PMVampire
07/16/2024, 4:24 PMFredrick Eisele
07/16/2024, 4:36 PMclass ExamplePlugin : Plugin<Project> {
companion object {
private lateinit var extension: ExampleExtension
fun getExtension(): ExampleExtension {
if (this::extension.isInitialized) return extension
return extension
}
}
⢠Provide a default (convention) to all of the tasks of a certain type
override fun apply(project: Project): Unit = project.run {
extension = project.extensions.create<ExampleExtension>("example-dsl")
tasks {
withType<ExampleTask>().configureEach {
// repeat the following for each of the properties
this.firstProperty.convention(extension.firstPropertyDefault)
}
}
}
There are some details that need to be worked out but...Fredrick Eisele
07/16/2024, 4:37 PMFredrick Eisele
07/16/2024, 4:41 PMval ext = ExamplePlugin.getExtension()Vampire
07/16/2024, 4:44 PMVampire
07/16/2024, 4:46 PMgetExtension does not make too much sense.
If the task is used without the plugin, the extension will not be set.
And if the plugin is applied, then the opinion is already employed.
The task should definitely not pull the opinion from the extension.
A task should usually never be aware of any extensions.Vampire
07/16/2024, 4:47 PMFredrick Eisele
07/16/2024, 4:47 PMThomas Broyer
07/16/2024, 4:50 PMVampire
07/16/2024, 4:52 PMclass ExamplePlugin ... {
fun apply(...) {
val foo by tasks.registering(ExampleTask::class) {
employOpinion(this)
}
}
companion object {
fun employOpinon(task: ExampleTask) {
task.propertyA = extension.propertyA
task.propertyB = extension.propertyB
}
}
}
then if someone creates a task of that type and wants to inherit the opinion, he can just do the same
val bar by tasks.registering(ExampleTask::class) {
ExamplePlugin.employOpinion(this)
}
and if he does not want the opinion inherited he can just use the plain opinionless task.Fredrick Eisele
07/16/2024, 4:56 PMVampire
07/16/2024, 7:43 PMPhilip W
07/16/2024, 8:39 PMVampire
07/16/2024, 9:03 PM