# How do I detect if a sub project has applied a given plugin (e.g. jar or war)

**URL:** <https://discuss.gradle.org/t/how-do-i-detect-if-a-sub-project-has-applied-a-given-plugin-e-g-jar-or-war/4974>\
**Category:** Old Forum Archive\
**Created:** [March 13, 2014, 1:53pm UTC](https://discuss.gradle.org/t/how-do-i-detect-if-a-sub-project-has-applied-a-given-plugin-e-g-jar-or-war/4974 "2014-03-13T13:53:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_Ahrer](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/martin_ahrer/32/9977_2.png) [@Martin\_Ahrer](https://discuss.gradle.org/u/Martin_Ahrer)\
**Post date:** [March 13, 2014, 1:53pm UTC](https://discuss.gradle.org/t/how-do-i-detect-if-a-sub-project-has-applied-a-given-plugin-e-g-jar-or-war/4974/1 "2014-03-13T13:53:00Z")

</div>

I have a root project with 2 sub projects,

```gradle
.
├── build.gradle
├── settings.gradle
└── jar
    ├── build.gradle
└── war
    ├── build.gradle

```

jar/build.gradle

```gradle
apply plugin: 'java'

```

war/build.gradle

```gradle
apply plugin: 'war'

```

I’m trying to detect whether a given plugin is present in a project and take different actions from the root/build.gradle:

build.gradle

```gradle
//subprojects {
//
  if (it.plugins.withType(org.gradle.api.plugins.WarPlugin)) {
//
      println "Project $it.name: WAR == ${it.plugins.withType(org.gradle.api.plugins.WarPlugin).size() > 0}"
//
  }
//
  if (it.plugins.withType(org.gradle.api.plugins.JavaPlugin)) {
//
      println "Project $it.name: JAR == ${it.plugins.withType(org.gradle.api.plugins.JavaPlugin).size() > 0}"
//
  }
//}
  subprojects {
    if (it.plugins.hasPlugin('war')) {
        println "Project $it.name: WAR == ${it.plugins.withType(org.gradle.api.plugins.WarPlugin).size() > 0}"
    }
    if (it.plugins.hasPlugin('java')) {
        println "Project $it.name: JAR == ${it.plugins.withType(org.gradle.api.plugins.JavaPlugin).size() > 0}"
    }
}

```

With the respective plugin applied in the sub project build.gradle, the plugins are not reported as applied. However once I move the apply plugin statement to the root project it works:

```gradle
project (':war') {
    apply plugin: 'war'
}

```

What needs to be done to make the plugins reported properly and still have the apply plugin within the sub project? I don’t want to move that to the root as I want to keep the sub projects as portable as possible.

---

<div class="post-metadata">

**Author:** ![Peter\_Niederwieser](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Peter\_Niederwieser](https://discuss.gradle.org/u/Peter_Niederwieser)\
**Post date:** [March 13, 2014, 5:21pm UTC](https://discuss.gradle.org/t/how-do-i-detect-if-a-sub-project-has-applied-a-given-plugin-e-g-jar-or-war/4974/2 "2014-03-13T17:21:00Z")

</div>

The root project’s build script gets evaluated before the build scripts for subprojects. One way out is to resort to callbacks:

```gradle
subprojects {
    plugins.withType(JavaPlugin) {
        ...
    }
    plugins.withType(WarPlugin) {
        ...
    }
}

```

An alternative is to have your own script plugins that subprojects apply as needed (e.g. ‘apply from: “$rootDir/gradle/java.gradle”’).

---

<div class="post-metadata">

**Author:** ![Martin\_Ahrer](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/martin_ahrer/32/9977_2.png) [@Martin\_Ahrer](https://discuss.gradle.org/u/Martin_Ahrer)\
**Post date:** [March 13, 2014, 6:15pm UTC](https://discuss.gradle.org/t/how-do-i-detect-if-a-sub-project-has-applied-a-given-plugin-e-g-jar-or-war/4974/3 "2014-03-13T18:15:00Z")

</div>

Thanks, I understand that with the callback I will receive a call when a plugin has been added. Just implemented it and works perfectly.
