Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle - configure subprojects based on plugin presence

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

like image 256
Andrea Bisiach Avatar asked Sep 13 '25 20:09

Andrea Bisiach


1 Answers

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.

like image 115
Alexander Leshkin Avatar answered Sep 15 '25 23:09

Alexander Leshkin