# Custom plugin & multi-project project

**URL:** <https://discuss.gradle.org/t/custom-plugin-multi-project-project/7378>\
**Category:** Old Forum Archive\
**Created:** [November 6, 2013, 3:44pm UTC](https://discuss.gradle.org/t/custom-plugin-multi-project-project/7378 "2013-11-06T15:44:00Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![denzhdanov](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/denzhdanov/32/161_2.png) [@denzhdanov](https://discuss.gradle.org/u/denzhdanov)\
**Post date:** [November 6, 2013, 3:44pm UTC](https://discuss.gradle.org/t/custom-plugin-multi-project-project/7378/1 "2013-11-06T15:44:00Z")

</div>

Hello,

Have a question regarding custom plugin appliance at a multi-project environment.

Consider a build.gradle of a root project:

```gradle
subprojects {
    apply plugin: 'java'
      buildscript {
        repositories {
            maven {
                url 'http://myurl/nexus/content/repositories/releases/'
            }
        }
        dependencies {
            classpath 'mygroup:nonnull-instrumenter:1.0'
        }
    }
    apply plugin: 'nonnull'
      sourceCompatibility = 1.7
      ...
}

```

Here ‘nonnull’ is my custom plugin. I get the following on attempt to build from a root project dir:

```gradle
FAILURE: Build failed with an exception.
  * Where:
Build file '/Users/denis/project/java/xxx/build.gradle' line: 31
  * What went wrong:
A problem occurred evaluating root project 'search'.
> Plugin with id 'nonnull' not found.
  * Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
  BUILD FAILED
  Total time: 4.269 secs

```

Note that here line 31 mentioned at the error message is a line with text “apply plugin: ‘nonnull’”.

The problem is fixed if I add the same buildscript initialization at the root project:

```gradle
buildscript {
    repositories {
        maven {
            url 'http://myurl/nexus/content/repositories/releases/'
        }
    }
    dependencies {
        classpath 'mygroup:nonnull-instrumenter:1.0'
    }
}
    subprojects {
    apply plugin: 'java'
      buildscript {
        repositories {
            maven {
                url 'http://myurl/nexus/content/repositories/releases/'
            }
        }
        dependencies {
            classpath 'mygroup:nonnull-instrumenter:1.0'
        }
    }
    apply plugin: 'nonnull'
      sourceCompatibility = 1.7
      ...
}

```

It seems that the problem is that root project is unaware of my custom plugin but it’s not clear why does it try to evaluate it at all because the plugin appliance statement is located at ‘subprojects’ block. Tried to google the answer but no luck so far. Any ideas?

Regards, Denis

---

<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:** [November 6, 2013, 3:50pm UTC](https://discuss.gradle.org/t/custom-plugin-multi-project-project/7378/2 "2013-11-06T15:50:00Z")

</div>

I think the problem is different, namely that ‘buildscript’ needs to be top-level and cannot be nested inside ‘subprojects’. (‘buildscript’ is very special; it gets extracted from the build script before compiling/evaluating the script.) Hence your nested ‘buildscript’ block doesn’t have any effect, but the one for the root project also affects subprojects (due to Gradle’s current class loader hierarchy).

---

<div class="post-metadata">

**Author:** ![denzhdanov](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/denzhdanov/32/161_2.png) [@denzhdanov](https://discuss.gradle.org/u/denzhdanov)\
**Post date:** [November 6, 2013, 4:13pm UTC](https://discuss.gradle.org/t/custom-plugin-multi-project-project/7378/3 "2013-11-06T16:13:00Z")

</div>

Thanks, that works!

Btw, /unrelated to the thread’s question/ is it possible to get a classloader which knows either about compiled project source classes or project binary dependencies (including transitive)? It looks like defining classloader for plugin’s class is aware only of compiled project source classes ☹

---

<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:** [November 6, 2013, 4:29pm UTC](https://discuss.gradle.org/t/custom-plugin-multi-project-project/7378/4 "2013-11-06T16:29:00Z")

</div>

Are you perhaps looking for ‘project.buildScript.classLoader’?

---

<div class="post-metadata">

**Author:** ![denzhdanov](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/denzhdanov/32/161_2.png) [@denzhdanov](https://discuss.gradle.org/u/denzhdanov)\
**Post date:** [November 6, 2013, 8:14pm UTC](https://discuss.gradle.org/t/custom-plugin-multi-project-project/7378/5 "2013-11-06T20:14:00Z")

</div>

Thank you for the advice but it seems I was not clear enough. Basically, I need to get a classloader which is backed by the same classpath as the one used during compilation

---

<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:** [November 6, 2013, 8:25pm UTC](https://discuss.gradle.org/t/custom-plugin-multi-project-project/7378/6 "2013-11-06T20:25:00Z")

</div>

During compilation of what exactly? The build script? The project’s source code?

---

<div class="post-metadata">

**Author:** ![denzhdanov](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/denzhdanov/32/161_2.png) [@denzhdanov](https://discuss.gradle.org/u/denzhdanov)\
**Post date:** [November 6, 2013, 8:44pm UTC](https://discuss.gradle.org/t/custom-plugin-multi-project-project/7378/7 "2013-11-06T20:44:00Z")

</div>

The project’s source code. But I think I found the solution - url classloader for project.configurations.getByName(“compile”).dependencies

Denis

---

<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:** [November 6, 2013, 9:13pm UTC](https://discuss.gradle.org/t/custom-plugin-multi-project-project/7378/8 "2013-11-06T21:13:00Z")

</div>

I’d recommend to create a class loader based on ‘sourceSets.main.compileClasspath’. This is the “source of truth” and defaults to ‘configurations.compile’ (but is sometimes reconfigured). Depending on what you are doing, you might also have to take other source sets into account, e.g. ‘test’ or a manually declared source set.

---

<div class="post-metadata">

**Author:** ![denzhdanov](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/denzhdanov/32/161_2.png) [@denzhdanov](https://discuss.gradle.org/u/denzhdanov)\
**Post date:** [November 6, 2013, 9:16pm UTC](https://discuss.gradle.org/t/custom-plugin-multi-project-project/7378/9 "2013-11-06T21:16:00Z")

</div>

How can I access sourceSets in case of java plugin? I don’t see a method like getSourceSets() at Project class given to the plugin’s apply() method ☹

---

<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:** [November 6, 2013, 9:45pm UTC](https://discuss.gradle.org/t/custom-plugin-multi-project-project/7378/10 "2013-11-06T21:45:00Z")

</div>

The concept of source sets is introduced by the Java (base) plugin. In a plugin implemented in Groovy, you would do:

```gradle
plugins.withType(JavaPlugin) { // only execute if/when Java plugin has been applied
        def mainSourceSet = project.sourceSets.main
            ...
    }

```

In Java, the inner part becomes:

```gradle
JavaPluginConvention javaConvention = project.getConvention().getPlugins().get("java");
    SourceSet mainSourceSet = javaConvention.getSourceSets().getByName("main");

```

---

<div class="post-metadata">

**Author:** ![denzhdanov](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/denzhdanov/32/161_2.png) [@denzhdanov](https://discuss.gradle.org/u/denzhdanov)\
**Post date:** [November 7, 2013, 10:14am UTC](https://discuss.gradle.org/t/custom-plugin-multi-project-project/7378/11 "2013-11-07T10:14:00Z")

</div>

Thanks, Peter!

That did the trick

Regards, Denis
