Is there a way to trigger build on a module's dependencies before executing a custom task that compileJava task depends on?


(aadi4287) #1

We’ve been trying to migrate from ant to gradle, but we’re facing the following problem. I’ll try to explain the problem we’ve run into with the setup below.

Consider the following 2 projects “a”, “b” where “b” depends on “a”.

root
|
---> a
---> b

Common configurations and tasks for both projects:

task codeGeneratorTask <<
{
 // calls an ant task class using ant.taskdef which generates some
 // java source files required before compilation
 ant.taskdef(name: "codGenTask",
   classname: "org.something.CodegenTask",
   classpath: configurations.codeGenConf.asPath)
   ant.codeGenTask(properties: "something")
}
  configurations
 {
  codeGenConf
  codeGenConf.extendsFrom(compile)
}

build.gradle of project “a”

dependencies
{
 // Needed by the ant code generator task
 codeGenConf "org.something:codegen-module:1.0"
    // other dependencies for project "a"
}
  compileJava.dependsOn(codeGeneratorTask)

build.gradle of project “b”

dependencies
{
 // Needed by the ant code generator task
 codeGenConf "org.something:codegen-module:1.0"
   compile project(":a")
 // other dependencies for project b
}
  compileJava.dependsOn(codeGeneratorTask)

In both projects, there is an ant task that generates a few java source files. These are required before the running the compile java task. In addition, the ant task requires the compile classpath + additional jars (defined in codeGenConf) of the project to work correctly.

Here is the problem we’re facing. After triggering a build from the root, the following things happen: 1. “b” evaluation happens. 2. Figures out codeGeneratorTask must be run for “b” before compileJava. 3. Runs code generator task for “b”, but without first building “a”. This causes a build failure as “a” needs to be in the classpath for code generator to work correctly for “b”.

If we did not have the code generator task at all, then before compileJava on “b” is called, “a” is first built and then “b” is compiled and built. Because of the dependency added on compileJava to codeGeneratorTask, it always executes codeGeneratorTask on “b” first, before building dependenent projects.

Is there any way to execute build on dependenencies first (“a” in this case) before running the code generator task on “b”?

Thanks in advance for the help. Please let me know if anything seems unclear.


(aadi4287) #2

Turns out putting a dependency on compileJava for the custom task was the wrong approach. Changed compileJava.dependsOn(codeGeneratorTask) to compileJava.doFirst { tasks.codeGeneratorTaks.execute() } and now the ordering works perfectly!