0

In the root build.gradle we are creating a task under allprojects that should run before the build of the project that is running the task.

// root build
apply plugin: 'cpp'
all projects {
  task.whenTaskAdded {theTask ->
    if (theTask.name == "build") {
      if (theTask.project.<runPreBuildTask?> == true) {
        tasks.create(name: 'preBuildTask' + theTask.name) {
          // do some stuff
        }
        theTask.dependsOn preBuildTask 
      }
    }
  }
  // other stuff model, toolchains, repositories
}

// some sub project build
// :subProject:subSubProject
// no luck using ext properties
ext.set('subSubProjectDoPreBuild', 'true')
model{
  components {
    ...
  }
 }

For some reference we have nearly 800 sub projects and their build.gradle is being auto generate by a script that converts the existing make file. I realize that we will have to hand edit many of them but for this task we would like to have the root build inject the task into all sub project but only if they some flag or something set. This will give us the flexibility to change the task in the future with out having to edit 100's of sub builds. Please let me know if I am approach this task the wrong way.

Michael Hobbs
  • 1,663
  • 1
  • 15
  • 26
  • you're effectively checking every task name on every subproject and if it matches `build`, you're adding a new (sub)task? That seems grossly inefficient to me. Why not just `if (project.flag) project.create(newTask); task('build').dependsOn(newTask)` – RaGe Jan 06 '16 at 15:21
  • Agreed, the question then is how to set the project.flag using model component rule system? We don't actually have any projects defined any where just models/components. On a side note we have cut our build time from 2 hours to 5 minutes so if we have to check every task, so be it. – Michael Hobbs Jan 06 '16 at 16:09
  • If the need for the subtask is not *entirely* arbitrary, if you can devise a rule, such as *if subproject name contains substring* or *subproject uses dependency foo* or maybe even *subproject has source file named foo*, you can use that as a condition for injecting subtask, from the root project itself. Might save you some subproject editing. – RaGe Jan 06 '16 at 16:17
  • Alright, we should be able to trigger off the source files as they all contain something.foo.cc or .h – Michael Hobbs Jan 06 '16 at 16:27
  • I said maybe because I'm not entirely familiar with the C++ side of things. With java you'd check for something like `project.sourcesets.main.getAllSource(){it.name=='foo.java'}`. There might be a C++ equivalent – RaGe Jan 06 '16 at 16:52
  • Is the original question you were asking, how to access ext.flag declared in the subproject from the root build.gradle? Wasn't sure because this question is only in a comment in the code block. – RaGe Jan 06 '16 at 16:56
  • @RaGe that was something I had attempted but was not able to get to work. – Michael Hobbs Jan 06 '16 at 17:44

0 Answers0