Getting the correct build path in a multi project

Hi,

I am (again) facing a problem using gradle. I have a multi project with project A and B. Both projects create a WAR file, and the war task depends on an other task:

task(hello) << {
    prinln "hello ${project.buildDir}"
}
  project(":A") {
   ...
   war.dependsOn hello
}
    project(":B") {
   ...
   war.dependsOn hello
}

My project structure looks as follows:

+ build.gradle
 |
+ projectA
         |
        - src
 + projectB
         |
        - src

When I executed the war task in a subproject, the hello task always prints the build directory of the root.

$> gradle projectA:war
$> hello build/

It never prints projectA/build or projectB/build. I find this very irritating, because the hello task is executed within different projects and therefore I expect different build directories.

Is there a way to get the actual build directory of the subproject?

Again, I appreciate your help!

Best regards, Daniel

Looking at the script snippet above, it looks like you are defining the hello task on the root project, rather than the subprojects (I assume the script is the root project’s build.gradle). If those are the only subprojects in your build, then something like the following should work:

subprojects { subproject ->
  task(hello) << {
    println "hello ${subproject.buildDir}"
  }
}

Hi Gary! Yes, that’s correct. The hello task is defined in the root project.

So, you mean I’ve to define this task on all subprojects. Why does it make a difference?

Thanks for your help!

A task defined on the root project is just that (a task defined on the root project), so it will print

out the buildDir of the root project.

So, what you did originally was create a task that prints out the buildDir of the root project, and then made your the various war tasks on your project depend on that one task. So it will have printed the buildDir of the root project and it will have only printed once.

what you actually want to do is create a task for each subproject that will print the buildDir of that subproject.