Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get project name of task in gradle

Tags:

gradle

groovy

I have a multi-project build. Sub-projects are in tree structure (not flat). In root build.gradle file I have a method that does some common thing for compilation phase.

I would like to have separate target dir for each subproject. So when I'm compiling whole project I will have own artifacts for each subproject. Method I mentioned above does compilation, so it needs to know which project it compiles.

If I use "gradle :subproject1:subsubproject1:compile" command then project.name still contains root project name. But I need in runtime to know project of task that called for method.

Questions are:

  1. Is it good idea at all to have separate targets for subprojects?
  2. How can I understand project of task that called for method inside this method?
like image 456
mrzodiak Avatar asked Mar 31 '15 12:03

mrzodiak


1 Answers

If the task is defined in the subproject then you can simply use project.name to get the name of the project the task is run under. You can try it out, for example by adding the following code to build.gradle located on the root project:

allprojects {
    task printProjName << {
        println ">> " + project.name
    }
}

Then, executing the printProjName task will result in an output similar to the one below:

:printProjName
>> Gradle
:projA:printProjName
>> projA
:projB:printProjName
>> projB
:projA:projA1:printProjName
>> projA1
:projA:projA2:printProjName
>> projA2
like image 158
Amnon Shochot Avatar answered Nov 13 '22 21:11

Amnon Shochot