Gradle - Add additional task to existing task

8.5k Views Asked by At

I'm working on a project that uses EJB2s. The created EJB Jars require additional processing by the application server before they're bundled in the war/ear and deployed.

I have created a custom task that works to do the additional processing if I invoke it explicitly (gradle ejbDeploy), but am having trouble fitting it into the gradle multi-project lifecyle. I need to somehow add it to the build graph to execute automatically after the jar task.

My first attempt was to add it to jar with

jar.doLast{
    ejbDeploy.execute()
} 

which seems to work for arbitrary code blocks, but not for tasks

What's the recommended solution for this? I see three approaches:

  1. Hook into the build graph and add it explicitly after the jar task.
  2. Set it up somehow in jar.doLast{}
  3. Set it up as a prerequisite for the WAR task execution

Is there a recommended approach?

Thanks!

3

There are 3 best solutions below

1
Snekse On BEST ANSWER

I'm new to Gradle, but I would say the answer really depends on what you're trying to accomplish.

If you want to task to execute when someone runs the command gradle jar, then approach #3 won't be sufficient.

Here's what I did for something similar

classes {
    doLast {
        buildValdrConstraints.execute()
    }
}

task buildValdrConstraints(type: JavaExec) {
    main = 'com.github.valdr.cli.ValdrBeanValidation'
    classpath = sourceSets.main.runtimeClasspath
    args '-cf',valdrResourcePath + '/valdr-bean-validation.json'
}
2
David Levesque On

I would go for approach #3 and set it up as a dependency of the war task, e.g.:

war {
    it.dependsOn ejbDeploy
    ...
}
0
Vyacheslav Shvets On

Add the following, and then ejbDeploy will be executed right after jar, but before war

jar.finalizedBy ejbDeploy

See Gradle Docs. 18.11. Finalizer tasks