First things first, I’m not called Bjorn. I it is Björn or Bjoern.
tasks.register(“copyStaticResources”)
tasks.copyStaticResources.doLast {
This is a bad idea generally.
By using tasks.register
you leverage task-configuration avoidance which is good.
But right in the next line you break task-configuration avoidance causing the task to always be realized.
Instead do def copyStaticResources = tasks.register("copyStaticResources") { ... }
so you only configure the task if necessary and right away have a reference to the task provider that you then can use as source directory.
tasks.copyStaticResources.doLast {
copy {
You only do the zipTree
and so on at execution time now, but you missed the part about properly declaring inputs and outputs of the task I mentioned.
You should always declare the inputs and outputs of a task properly, otherwise you can neither wire task outputs and inputs together, do not have the necessary task dependencies implicitly, and also the task can never be up-to-date.
In the task configuration but outside the doLast { ... }
use inputs....
and outputs....
to declare the inputs and outputs of the task.
Once you fixed that, it should start working as expected I think.
UPDATE, this seems to work, though I don’t think its what you had in mind as I no longer am defining the sourceSets srcDir:
Yeah, no, that’s not what I meant, see above.
Additionally here now you break task-configuration avoidance of the processResources
task, and you will miss your resource at execution time as it no longer lands in the final artifact.
If you add a doLast
action to the processResources
task, it would be ok to write to the output directory of the processResources
task, as the action is now part of that task.
But the first variant, fixed of the missing parts is imho preferable in your situation.
UPDATE 2 maybe this next iteration better matches what Bjorn suggested
Almost. I might forgot to mention it explicitly, but any explicit dependsOn
where on the left-hand side is not a lifecycle task is a code smell and a sign that you somewhere do something not properly. Instead - as said above - use inputs....
like inputs.files(configurations.staticResources)
, then the task can for example also properly be up-to-date. The need for explicit dependsOn
with non-lifecycle tasks most often means that you did not properly wire task outputs and task inputs together.
So the last version should almost be fine, except for the wrong dependsOn
that should be an inputs...
and the superfluous dependsOn
in the last line.