Bernhard Posselt
10/02/2024, 7:25 AMtasks {
getByName("jsProcessResources") {
mustRunAfter("copyJson")
}
}
2. Not sure what the appropriate API is to just execute it during the check phase? So far I've got:
tasks {
getByName("check") {
finalizedBy("validateJsonSchema")
}
}
but it looks like finalizedBy is more like a finally block in a try catch and executed regardless of previous failuresVampire
10/02/2024, 7:53 AMexecute a task after another taskThe "correct" way is to not need that. The "even more correct" way is to not even think about it. 🙂 You should always wire task outputs to task inputs and by that get necessary task dependencies and ordering automatically. Whenever you need to do a manual
dependsOn where the left-hand side is not a lifecycle task, you most probably do something wrong (even if Gradle hinted you at it) and in the majority of cases also if you use an ordering constraint like mustRunAfter.
e.g. execute copyJson before jsProcessResourcesYou first say run one task after the other, then your example says the opposite. This is an important distinction which might influence an answer. Besides the fact, that you probably need to elaborate on the use-case, as your question strongly sounds like an XY-problem. So from where does
copyJson copy what to where, and why does it need to be run before jsProcessResources? If the answer for example is, that the copyJson copies something into src/js/resources which jsProcessResources then copies, then the answer indeed is that you shouldn't. For example any other task needing resources will still miss the task dependency and might or might not get the copied json depending on whether the copy task run first or not. And sometimes Gradle 7 will also recognize that and warn or Gradle 8 fail. Not fully sure about KMP, but usually you do something like sourceSets { main { resources { srcDir(copyJson) } } } with copyJson having some dedicated output directory in layout.buildDirectory. Then any task needing resources also gets those resources and automatically has a dependency on the "code generation" task.
run a validateJsonSchema task during checkHere an explicit
dependsOn indeed is the right choice. check is a lifecycle task, so if you want that task to run when someone invokes check, just add a direct task dependency using dependsOn.
Looking at the APIs, there does not seem to be a mustRunBefore APIWell, it's called
dependsOn and you should not use it as written above 🙂
So far I've got:You can do it like that, it's just semantically non-sense.
finalizedBy means "run this task always after that task, even if that task failed during execution.
check does never fail as it is a lifecycle task unless someone evilly added actions directly to it, so it will never fail to execute.
If you want that the task is executed after all tasks that check depends upon, then this construct could help, but usually you just want a dependsOn as written above.Bernhard Posselt
10/02/2024, 7:56 AMIf the answer for example is, that thethat's exactly the use case: I'm concatenating a json file from another folder and want that to appear in the build directorycopies something intocopyJsonwhichsrc/js/resourcesthen copies,jsProcessResources
Bernhard Posselt
10/02/2024, 7:57 AMVampire
10/02/2024, 8:13 AMmustRunAfter is indeed the way to go.
But as you already said that's the use-case, you probably got exactly that message I meant from Gradle like "task uses outputs of that other task without dependency" and there the suggested actions are imho non-sense and only duct-tape instead of a proper solution. 🙂Bernhard Posselt
10/02/2024, 10:28 AMBernhard Posselt
10/02/2024, 10:53 AMBernhard Posselt
10/02/2024, 10:53 AMsourceSets {
val commonMain by getting {
resources.srcDir(tasks.named { it == "combineJsonFiles"})
dependencies {
implementation(libs.kotlinx.serialization.core)
implementation(libs.kotlinx.serialization.json)
implementation(libs.kotlinx.datetime)
implementation(libs.kotlinx.coroutines)
}
}
val commonTest by getting {
dependencies {
implementation(libs.kotlin.test)
}
}
}Bernhard Posselt
10/02/2024, 10:55 AMVampire
10/02/2024, 11:08 AMsrcDir(copyJson) I suppose isn't valid Kotlin, can't find the equivalentif
copyJson is a variable holding the Task or TaskProvider it is.
Be careful with named as I just explained on the Kotlin Slack to someone with your first name, so probably you? 🙂Vampire
10/02/2024, 11:09 AMsetting resources.srcDir does not seem to override resolving existing resources in commonMainCorrect,
srcDir adds additional source dirs.
setSrcDirs would override preexisting source dirs.Bernhard Posselt
10/02/2024, 2:47 PM