Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle - Add additional task to existing task

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!

like image 452
babbitt Avatar asked Sep 14 '12 14:09

babbitt


People also ask

How do I run multiple tasks in Gradle?

Executing Multiple Tasks You can execute multiple tasks from a single build file. Gradle can handle the build file using gradle command. This command will compile each task in such an order that they are listed and execute each task along with the dependencies using different options.

What is Buildscript in Gradle?

The "buildscript" configuration section is for gradle itself (i.e. changes to how gradle is able to perform the build). So this section will usually include the Android Gradle plugin.

What is afterEvaluate in Gradle?

> gradle -q test Adding test task to project ':project-a' Running tests for project ':project-a' This example uses method Project. afterEvaluate() to add a closure which is executed after the project is evaluated. It is also possible to receive notifications when any project is evaluated.


2 Answers

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

war {
    it.dependsOn ejbDeploy
    ...
}
like image 165
David Levesque Avatar answered Sep 27 '22 19:09

David Levesque


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'
}
like image 31
Snekse Avatar answered Sep 27 '22 20:09

Snekse