# buildSrc access to project modules?

**URL:** <https://discuss.gradle.org/t/buildsrc-access-to-project-modules/7290>\
**Category:** Old Forum Archive\
**Created:** [February 7, 2013, 11:15am UTC](https://discuss.gradle.org/t/buildsrc-access-to-project-modules/7290 "2013-02-07T11:15:00Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jesper\_Thuun\_Peterse](https://avatars.discourse-cdn.com/v4/letter/j/b77776/32.png) [@Jesper\_Thuun\_Peterse](https://discuss.gradle.org/u/Jesper_Thuun_Peterse)\
**Post date:** [February 7, 2013, 11:15am UTC](https://discuss.gradle.org/t/buildsrc-access-to-project-modules/7290/1 "2013-02-07T11:15:00Z")

</div>

I want to create my own plugin type to ease access to our home-grown code generator. The code generator is in a module called ‘remoteserviceframework’.

I have my plugin in the buildSrc directory, and I therefore add a dependency to “:remoteserviceframework” from the top level build.gradle.

Now, everything compiles and looks great in IntelliJ.

Running my build however, my plugin is not recognized.

it is declared in a class like this:

class RemoteService implements Plugin {

void apply(Project project) { … }

and when I apply it, I do so with:

apply plugin: ‘RemoteService’

This, however, is not recognized as a plugin.

Am I missing something here? Is it not an option to have plugins access modules from the project?

---

<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:** [February 7, 2013, 11:36am UTC](https://discuss.gradle.org/t/buildsrc-access-to-project-modules/7290/2 "2013-02-07T11:36:00Z")

</div>

‘buildSrc’ is its own _build_, and is executed before even the _evaluation phase_ of the main build starts. Therefore, you can’t use outputs of the main build from ‘buildSrc’. Sometimes it’s possible to work around this by configuring a source directory of the main build as a source directory of the ‘buildSrc’ build (effectively building it twice), but you’ll have to know what you are doing.

---

<div class="post-metadata">

**Author:** ![Jesper\_Thuun\_Peterse](https://avatars.discourse-cdn.com/v4/letter/j/b77776/32.png) [@Jesper\_Thuun\_Peterse](https://discuss.gradle.org/u/Jesper_Thuun_Peterse)\
**Post date:** [February 7, 2013, 12:00pm UTC](https://discuss.gradle.org/t/buildsrc-access-to-project-modules/7290/3 "2013-02-07T12:00:00Z")

</div>

Peter,

thanks for the quick answer! I understand the problem, but I’d really like to create a plugin for this, and I’d prefer not to build and deploy it as a separate project.

Would it be possible to put my plugin code in the remoteserviceframework module, and still be able to use it via ‘apply plugin’ from other modules?

If not, how do I add a source to the buildSrc?

Thanks! /Jesper Thuun-Petersen

---

<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:** [February 7, 2013, 12:05pm UTC](https://discuss.gradle.org/t/buildsrc-access-to-project-modules/7290/4 "2013-02-07T12:05:00Z")

</div>

> Would it be possible to put my plugin code in the remoteserviceframework module, and still be able to use it via ‘apply plugin’ from other modules?

No, it isn’t possible.

> If not, how do I add a source to the buildSrc?

Add a ‘build.gradle’ in ‘buildSrc’, and then add a source directory to the ‘main’ source set as usual.

---

<div class="post-metadata">

**Author:** ![Jesper\_Thuun\_Peterse](https://avatars.discourse-cdn.com/v4/letter/j/b77776/32.png) [@Jesper\_Thuun\_Peterse](https://discuss.gradle.org/u/Jesper_Thuun_Peterse)\
**Post date:** [February 7, 2013, 12:26pm UTC](https://discuss.gradle.org/t/buildsrc-access-to-project-modules/7290/5 "2013-02-07T12:26:00Z")

</div>

OK, thanks.

I realize this might be too heavy a task to accomplish something fairly simple, so I decided to go with:

```gradle
compileJava <<{
    InterfaceGenerator ig = new InterfaceGenerator()
    ig.generate('com.stibo.services.workbench.beans',
            'com.stibo.services.workbench.interfaces',
            project.buildDir.toString + '/generated-src/remoteservice')
}

```

Now, we may be back at the same problem? Because the InterfaceGenerator, which is a class in the remoteServiceFramework, which I have a compile dependency on, cannot be found at this point. How do I get the class on my build class path?

---

<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:** [February 7, 2013, 12:38pm UTC](https://discuss.gradle.org/t/buildsrc-access-to-project-modules/7290/6 "2013-02-07T12:38:00Z")

</div>

As I’ve tried to explain already, you cannot use outputs of the main build from the ‘buildSrc’ build. One workaround, applicable in some but not all cases, is to add the required code as a source directory of the ‘buildSrc’ build, effectively building it twice.

---

<div class="post-metadata">

**Author:** ![Jesper\_Thuun\_Peterse](https://avatars.discourse-cdn.com/v4/letter/j/b77776/32.png) [@Jesper\_Thuun\_Peterse](https://discuss.gradle.org/u/Jesper_Thuun_Peterse)\
**Post date:** [February 7, 2013, 12:51pm UTC](https://discuss.gradle.org/t/buildsrc-access-to-project-modules/7290/7 "2013-02-07T12:51:00Z")

</div>

Hi Peter, I did understand that.

I’m sorry, if I didn’t explain my new approach properly:

I’ve abandoned the plugin approach. I’m just doing plain gradle files now. In the build-script of one module, I want to call compiled code from another module. Is that possible?

Regards /Jesper

---

<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:** [February 7, 2013, 12:54pm UTC](https://discuss.gradle.org/t/buildsrc-access-to-project-modules/7290/8 "2013-02-07T12:54:00Z")

</div>

You can’t, for exactly the same reasons.

---

<div class="post-metadata">

**Author:** ![Jesper\_Thuun\_Peterse](https://avatars.discourse-cdn.com/v4/letter/j/b77776/32.png) [@Jesper\_Thuun\_Peterse](https://discuss.gradle.org/u/Jesper_Thuun_Peterse)\
**Post date:** [February 12, 2013, 8:11am UTC](https://discuss.gradle.org/t/buildsrc-access-to-project-modules/7290/9 "2013-02-12T08:11:00Z")

</div>

Thanks Peter,

For reference, I solved the problem using ant, as my plugin now includes the following code:

```gradle
ant.java(classname: 'com.stibo.remoteserviceframework.server.impl.InterfaceGenerator', fork: 'true', failOnError: 'true') {
                arg(value: "${sourcePackage}")
                arg(value: "${destinationPackage}")
                arg(value: "${destinationDir}")
                sysproperty(key: "FRAMEWORK_DISABLE_AUTOINJECT", value: "true")
                classpath {
                    pathelement(path: project.sourceSets.main.output.asPath)
                    pathelement(path: project.sourceSets.main.compileClasspath.asPath)
                    pathelement(location: "${project.mainProject}/remoteserviceframework/build/libs/remoteserviceframework-1.0-SNAPSHOT.jar")
                }
            }

```

the interesting point is the direct reference to the artifact containing my code generator. Probably not the most elegant way of handling the problem, but it takes care of my problem for now.

---

<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:** [February 12, 2013, 8:22am UTC](https://discuss.gradle.org/t/buildsrc-access-to-project-modules/7290/10 "2013-02-12T08:22:00Z")

</div>

Running the code generator in a separate process is a good solution (probably better than fussing with a class loader in the build script). Instead of ‘ant.java’, you could use a ‘JavaExec’ task.
