I'm trying to add a preprocessor task which runs w...
# gradle
c
I'm trying to add a preprocessor task which runs whenever I compile with
Copy code
task("preprocess", Exec::class) {
    group = "build"
    workingDir = File("..")
    executable = "bin/preproc"
}

tasks.named("desktopRun").dependsOn("preprocess")
but I keep getting "Task with name 'desktopRun' not found in project ':composeApp'.
v
Some general notes: • do not use
task(...)
, it does not leverage task-configuation avoidance • do not use
File(...)
with a relative path that resolves relative the current Gradle daemon working directory and while often this is the root project directory, this is not guaranteed and sometimes also not the case, so better use something like
layout.projectDirectory.dir("..")
or similar • do not use
dependsOn
with a
String
if you can avoid it. When you create a task you get a reference to it back, use that instead if you really need
dependsOn
• I wonder that
tasks.named("desktopRun").dependsOn("preprocess")
even compiles,
tasks.named
returns a
TaskProvider
on which you cannot call
dependsOn
, it should be
tasks.named("desktopRun") { dependsOn(...) }
, if at all • If
tasks.named("desktopRun")
complains that it cannot find the task but you can execute it, then the task is not yet registered. You can instead for example do
tasks.named { it == "desktopRun" }.configureEach { dependsOn(...) }
which will also work for tasks registered later, but also not throw any error if no task with such a name is created at all • Actually, any explicit
dependsOn
where the left-hand side is not a lifecycle task is a code smell that hints at you doing something you should not do or do wrongly, so you might re-think the approach you do. I cannot go into more detail on this one as it heavily depends on the exact situation what that task does, what it is necessary for, and so on.
c
What should I use instead of
task()
?
v
val preprocessTask = tasks.register<Exec>("preprocess") { ... }
or
val preprocess by tasks.registering(Exec::class) { ... }
c
So
preprocess
is a task which needs to run before any build or compilation steps run. Only I have no idea what those steps are — I used the https://kmp.jetbrains.com/ wizard to create this multiplatform project, and it explained nothing about the configuration it created or what any of it meant. Getting the preprocessor to run before the
desktopRun
task is already a huge quality-of-life improvement, and I'd happily move it to where it's supposed to be if I had any idea what that was.
I have no idea how to look up the dependency chain for these tasks.
v
which needs to run before any build or compilation steps
Well, even if you get your code to work like intended, that will not the least achieve your goal. You just configure that before
desktopRun
is executed
preprocess
needs to have been executed. But this can well be after all compilation tasks and so on are already executed, so it does not help at all.
As I said, how to do it properly I cannot say, as I don't know your build and especially not what that task is or does. If you want that the task is executed before each and every other task possible, you can do
Copy code
tasks.configureEach {
    dependsOn(preprocessTask)
}
Not that it is a good idea to do so. 😄
c
It also doesn't work, as it creates a circular dependency on the
preprocessTask
v
Yeah, well
Copy code
tasks.named { it != "preprocess" }.configureEach { dependsOn(preprocess) }
c
That's got it.
It's working now. It doesn't need to run before everything, but it's harmless if it does and it's basically instantaneous, so I'm not that bothered and without knowing the tasks which actually do the builds I can't exactly narrow it down.