https://kotlinlang.org logo
Title
a

andylamax

08/08/2022, 2:06 AM
I have a custom gradle that I'd like to run before executing any KMP task (i.e. compileKotlinJvm, compileKotlinJs, compileKotlinLinuxX64, e.t.c) is there a common task that all of these platform specific task do depend on that I can hook my custom task there?
I know have it as
targets.configureEach {
        compilations.all {
            compileKotlinTask.dependsOn("generateCode")
        }
    }
Is there another better way?
e

ephemient

08/08/2022, 2:24 AM
going by the "generateCode" name, surely it would be better to add the task output to the sourceset? then all the compile dependencies would work automatically
v

Vampire

08/08/2022, 7:30 AM
As well as other tasks needing sources like doc tasks, source jar tasks, analysis tasks, ...
a

andylamax

08/08/2022, 3:47 PM
So, yes. The task output is already added the commonMain sourceSet, however, I was looking for any other better alternative (if it exists) so that when I call
compileKotlinJvm
then
generateCode
will be called as a dependency. Just adding the output to the sourceSet works if the code is already generated, and it doesn't if the code is not generated at all in the first place
e

ephemient

08/08/2022, 3:49 PM
is this Android? there's a bit of a mess there
but with normal JVM projects, assuming you're adding it correctly (e.g.
task.outputDir
not
file(dir)
), the task dependencies are tracked
a

andylamax

08/08/2022, 3:54 PM
Its not android at all. Let me try with
task.outputDir
I had it with
kotlin.srcDirs
v

Vampire

08/08/2022, 3:56 PM
You don't even need
task.outputDir
, just use
task
(as long as
outputDir
is the only output of the task
Implicit task dependencies are practically always preferable over explicit
dependsOn
which always is a code smell
a

andylamax

08/08/2022, 3:59 PM
roger, will test this again when I am back at my desk