How to force a custom task to run before the jar task


(shanloid) #1

Inside a custom plugin, I am adding a rule which subsequently creates a number of new tasks based on that rule to the project. One of the things that these tasks do is to write some data about the build to the MANIFEST.MF file inside any jars that are output by the build.

The simplified task graph looks like this: taskA -> jar -> taskB

Task A can modify the manifest just fine, but any attempts to modify the manifest in task B do nothing. This mostly makes sense - the jar has already been written to disk by the time task B executes. Obviously I need to manipulate the ordering of tasks to for the jar task to execute AFTER all of my custom tasks, so I’m trying to do this:

// Inside the apply method of my plugin
project.tasks.addRule(...) {
    ...
    Task t = project.tasks.create(...) {
        ...
    }
    project.tasks.findAll { it instanceof Jar }.each { Jar jar ->
        jar.mustRunAfter(t)
        LOG.info('Jar {} mustRunAfter {}', jar.archiveName, t.name)
    }
}

This logs what I expect against all the jar tasks in my project, BUT when the complete task graph is printed to the console, the jar task is still located in between my custom tasks, i.e. the mustRunAfter call is not taking effect.

Any ideas on what I’m doing wrong, or what I need to do to ensure that my custom task(s) can execute before the jar task runs?