I need to run a copy task everytime the task kspKo...
# gradle
c
I need to run a copy task everytime the task kspKotlin successfully passes, I have the following code which seems to work okay:
Copy code
afterEvaluate {
    tasks.named("kspKotlin") {
        finalizedBy(copyFeatures)
    }
}
I read somewhere whilst learning that finalizedBy happens irrespective of kspKotlin being successful or not, is there an alternative that works only on success? Also is there anyway to avoid the afterEvaluate because I can’t seem to find that task without that block?
a
what you might want is instead to hook into a ‘lifecycle task’ https://docs.gradle.org/current/userguide/more_about_tasks.html#sec:lifecycle_tasks so instead of making
kspKotlin
be finaized by
copyFeatures
, instead 1. make
copyFeatures { dependOn(tasks.kspKotlin) }
2. make a lifecycle task, like
assemble
, depend on
copyFeatures
That way every time
assemble
runs, it will run
kspKotlin
and
copyFeatures
in the right order. But… what does
copyFeatures
do? Does it copy generated source code into a src dir?
c
Copy features is essentially this:
Copy code
val copyFeatures by tasks.registering(Copy::class) {
    from(file("$rootDir/xxx/build/generated/ksp/main/resources/features-gen.yml"))
    rename { "features.yml" }
    into(file("$rootDir/.xxx/"))
}
One of my KSP outputs is a yaml file
a
Yeah okay, that makes sense. What happens to the yaml file get used for afterwards? Are there any other tasks that require it exists? I ask because it’s possible to convert the
copyFeatures
task into a ‘file provider’ and use it as a srcDir in a source set, or something, and that makes sorting out task ordering easier
c
The yaml file is actually picked up by a process in CI, its entirely unrelated to anything else
a
gotcha
c
So this is okay?
Copy code
tasks.named(LifecycleBasePlugin.BUILD_TASK_NAME) {
    dependsOn(copyFeatures)
}
I actually need it to run on build as opposed to assemble
a
yes that makes sense 👍
c
Thank you Adam !
a
you’re welcome!
v
You can also have kspKotlin finalized by copyFeatures and copyFeatures dependsOn kspKotlin. Then it is always run after successful kspKotlin execution if that really is what you want.
c
Oddly the lifecycle task block never seems to trigger the copyFeatures task
I tried assemble also
neither seem to be triggered when building my android application
v
Run with
-m
and you will see which tasks would get executed.
c
Yeah the copy task doesnt ever seem to get registered, I must be doing something silly
The finalized approach I posted originally works but I was trying to avoid the after evaluate
v
Then find out why the task is not present already. Is the plugin not yet registered? Does it itself use
afterEvaluate
to register the task? ...? Many possibilites.
Worst case:
Copy code
tasks.configureEach {
    if (name == "kspKotlin") {
        finalizedBy(copyFeatures)
    }
}
c
I wonder if its because my android application has variants, so assemble is in fact assembleStaging? Technically they’re different tasks?
v
Of course they are different tasks. But I cannot reason about Android, I don't do Android development.