eygraber
04/23/2021, 2:22 AMFleshgrinder
04/23/2021, 6:25 AMFleshgrinder
04/23/2021, 6:26 AMFleshgrinder
04/23/2021, 6:27 AMVampire
04/23/2021, 7:30 AMRegular task is enough
Check
a task with an input file, output directory
Check
register the output as source set
No, register the task itself as
srcDir
for a source set, this also adds an implicit task dependency automatically whenever the sources are needed.
register the output as generated in idea plugin
Optional, but check
make compileKotlin dependOn.
No, better do as advised above, there are for example also other tasks that need sources like a source jar task and so on. Always better going with an implicit task dependency if possible.
Example?
This is how you properly register a source generation task as source files:
val mySourceGeneratingTask by tasks.registering(...) {
...
}
val generate by tasks.registering {
dependsOn(mySourceGeneratingTask)
}
sourceSets {
main {
java {
srcDir(mySourceGeneratingTask)
}
}
}
TheĀ generateĀ task is fully optional, but I like to have a lifecycle task that depends on all tasks that generate something.eygraber
04/23/2021, 8:00 AMFleshgrinder
04/23/2021, 7:46 PMorg.gradle.api.Task
there, or should it be a specific one?Vampire
04/23/2021, 9:45 PMFleshgrinder
04/23/2021, 9:46 PMeygraber
05/16/2021, 10:25 PM@CacheableTask
open class GenerateTask : DefaultTask() {
@get:InputFile
@PathSensitive(PathSensitivity.RELATIVE)
val inputFile: File = project.file("src/main/assets/file.json")
@get:OutputFile
val outputFile: File = project.file("build/generated/File.kt")
@TaskAction
fun generate() {
outputFile.writeText(inputFile.readText())
}
}
val generatorTask by tasks.registering(GenerateTask::class)
val generate by tasks.registering {
dependsOn(generatorTask)
}
android.sourceSets {
getByName("main") {
java {
srcDir(generatorTask.get().outputFile)
}
}
}
And that would make any task that requires source run my task?
And if I wanted to have an IDEA plugin that would run this task if it detects source changes, I would have it invoke the generate
task or the generatorTask
?Vampire
05/16/2021, 10:51 PMFile
instead of for example RegularFileProperty
, I ignore that, as your task seems to be just a quick and dirty playground anyway.
Regarding the wiring, that looks ok, except for the part that you did not go like I suggested, except if Android is not capable of doing it the normal way, but no have no idea about Android development.
Your IJ plugin would of course invoke the generatorTask
. As I said, the generate
task is a fully optional lifecycle task I like to have that depends on all tasks that generate something. It would be ridiculous to call that from your IJ plugin just because one of the generate tasks is outdated.eygraber
05/19/2021, 2:39 AMRegularFileProperty
, wasn't aware of that.
Regarding getByName("main")
I was getting errors if I used main
both with sourceSets
and android.sourceSets
.
srcDir
I originally had it like you did, but then I was trying a few different things because the task wasn't getting invoked when I thought it should (like before a build, etc...). I think I did something wrong elsewhere, so I still need to play around with it some more.srcDir
Vampire
05/19/2021, 9:49 AMOh good to know aboutĀhttps://docs.gradle.org/current/userguide/lazy_configuration.html Imho every new task / extension / plugin should use that. š, wasn't aware of that.RegularFileProperty
RegardingĀIt was from a Java or Kotlin project where theĀ I was getting errors if I usedĀgetByName("main")
both withĀmain
Ā andĀsourceSets
.android.sourceSets
main
source set exists and is available as type-safe accessor.
Obviously for Android this does not work and the normal source set is not available with Android, so the getByName
is probably ok, even though I'd normally use the lazy api everywhere (named
).
But probably that collection is not evaluated lazily anyway, so getByName
is probably ok too.
I just tend to always use the new api for consistency and future proofness.
Actually it looks like Android only accepts files forĀAs I said, I didn't really touch Android dev yet, my code was from normal project. But from a quick look you are right.srcDir
AndroidSourceDirectorySet#srcDir
evaluates according to Project#file
while
SourceDirectorySet#srcDir
evaluates according to Project#files
and only the latter allows `Task`s directly as tasks can have multiple output properties.
But by doing srcDir(generatorTask.get().outputFile)
I think you loose the implicit task dependency as you unwrap the provider and additionally you make the task be realized immediately instead of lazily.
When keeping the property as File
, I think the correct way while staying lazy and keeping the implicit task dependency should srcDir(generatorTask.map { it.outputFile })
.
And if you switch to the recommended RegularFileProperty
or Provider<RegularFile>
(if it should not be editable), then either the same should work, or alternatively srcDir(generatorTask.flatMap { it.outputFile })
.eygraber
05/19/2021, 3:54 PM