Hello - assuming a setup where I have per-sourceSe...
# plugin-development
m
Hello - assuming a setup where I have per-sourceSet task and one group task - what would be the best way to share input with all of the child tasks? I wanted to use an
@Option
but unless I invoke each task with their own option provided it won't work. Calling group task will fail as that
Option
is not registered for it. I know on alternative would be to use a project property but that would enforce project getting reconfigured which I'd rather avoid. Running
./gradlew taskToRunThemAll
invokes all of child tasks (
myTaskMain
,
myTaskTest
etc). This will work fine:
Copy code
./gradlew myTaskMain --my-input=foo myTaskTest --my-input=foo
This will fail as there is no option with the same name declared for the group task:
Copy code
./gradlew taskToRunThemAll --my-input=foo
If I do create another task type and add
my-input
option to it, it will consume it and the parameter won't be shared anyway.
Copy code
class MyPlugin : Plugin<Project> {
  override fun apply(target: Project) {
    val extension = target.extensions.getByType(KotlinProjectExtension::class.java)

    val perSourceTask = extension.sourceSets.all { set ->
      val dirs = set.kotlin.sourceDirectories.toList()
      val taskName = "myTask${set.name.capitalized()}"
      target.tasks.register(taskName, SomeSourceTask::class.java) {
        it.setSource(dirs)
      }    
    }

    val topLevelTask = target.tasks.register("taskToRunThemAll")
    topLevelTask.configure { it.dependsOn(perSourceTask)
  }
}

abstract class SomeSourceTask : SourceTask() {
  @get:Option(
    option = "my-input",
    description = "..."
  )
  @get:Input
  abstract val myInput: Property<String>
}
Another idea is to have a base task and make it spit a file output which then source tasks could use as an input but I'm not sure if that's efficient?
Copy code
tasks.withType(SomeSourceTask::class.java).configureEach {
   topLevelTask.finalizedBy(it)
}
a
What about a project property? When you register the task you can set a conventional value:
target.providers.gradleProperty("myInput")
. And then you can set a default value in
gradle.properties
, or pass one in
./gradlew taskToRunThemAll -PmyInput=foo
m
Thanks @Adam that's a good idea but as far as I understand changing property value will force the entire project to reconfigure
v
What do you mean by "force the entire project to reconfigure"? If you mean changing its value will invalidate a CC-entry, so will changing the option value iirc.