Gradle - configure subprojects based on plugin presence

1.4k views Asked by At

I am fighting with a fairly simple gradle problem but despite searching I cannot find a solution. Very simple, in a multi project build, I need configure some subproject based on the plugins they load. So If sub project has plugin 'war' or 'ear' do this.. I have tried the following without success:

subprojects {
    if (it.plugins.hasPlugin('war') || (it.plugins.hasPlugin('ear') {
        apply plugin: 'my super special plugin'
        ....
        ....
    }
}

The above never apply plugin: 'my super special plugin'

Any suggestion? Thanks

1

There are 1 answers

0
Alexander Leshkin On

Gradle executes subprojects closure before evaluating build.gradle from subprojects. Therefore, there are no information about plugins from build.gradle at this point. To execute some code after evaluation of the subproject/build.gradle you should use ProjectEvaluationListener. For example:

subprojects {
    afterEvaluate {
      if (it.plugins.hasPlugin('war') || (it.plugins.hasPlugin('ear') {
          it.plugins.apply 'my super special plugin'
          ....
          ....
      }
    }
}

Also note about it.plugins.apply 'my super special plugin' instead of apply plugin: 'my super special plugin'

Another option is to use shared common.gradle wich will configure subprojects. This shared gradle file may be included in the subproject/build.gradle by using apply from: '../common.gradle' in proper place.