9

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!

Marcel Stör
  • 22,695
  • 19
  • 92
  • 198
babbitt
  • 871
  • 11
  • 22

3 Answers3

6

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

war {
    it.dependsOn ejbDeploy
    ...
}
David Levesque
  • 22,181
  • 8
  • 67
  • 82
  • I'm relatively new to gradle. Would the complete syntax be: war { it.dependsOn project('ejbProject').tasks['ejbDeploy'] } ? – babbitt Sep 14 '12 at 15:09
  • 1
    It depends how you've organized your projects and subprojects. The above syntax works fine if the task is included in the current project (and it should probably be imo). You can use the `allprojects` and `subprojects` sections to inject common tasks in a project hierarchy. See: http://www.gradle.org/docs/current/userguide/multi_project_builds.html – David Levesque Sep 14 '12 at 15:24
3

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'
}
Snekse
  • 15,474
  • 10
  • 62
  • 77
  • 1
    This is the answer to my question, but I've since really taken a close look at what I was trying to do and decided that it should be its own task. Thanks for answering :). – babbitt Aug 25 '14 at 19:36
1

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

Vyacheslav Shvets
  • 1,735
  • 14
  • 23