5

Some subprojects have the java plugin applied by their own build.gradle files. In the build.gradle of the root project, I want to apply plugin findbugs to each subproject that already has the java plugin. In build.gradle I have tried:

configure(subprojects.findAll {proj -> proj.getPluginManager().hasPlugin("java")}) {
      apply(plugin: "findbugs")
}

and

subprojects {
  if (getPluginManager().hasPlugin("java")) {
    apply(plugin: "findbugs")
  }
}

The outside loop does indeed run once for each subproject, but the inner closure never runs, in both case. I suspect this is because the subproject build scripts take effect before the outer one. Is there any way around this besides manually applying the findbugs plugin to each subproject?

blzbrg
  • 148
  • 9

1 Answers1

4

You'll want to use PluginContainer.withId() to do this. This will evaluate the given closure immediately if the given plugin has already been applied or in the future when/if the plugin is applied.

subprojects {
    plugins.withId('java') {
        apply plugin: 'findbugs'
    }
}
Mark Vieira
  • 13,198
  • 4
  • 46
  • 39
  • this is not working for me. My build.gradle in the subproject applies java plugin and my root project build.gradle has your code in it. The documentation for withId says "executes an action for a plugin with a given id". The type it accepts is Action super Plugin> which seems to apply actions "against" a plugin. – blzbrg Feb 13 '16 at 21:49
  • https://discuss.gradle.org/t/how-do-i-detect-if-a-sub-project-has-applied-a-given-plugin-e-g-jar-or-war/4974 also seems related, but results in crazy errors for me. – blzbrg Feb 13 '16 at 22:33