helpermethod helpermethod - 2 months ago 7x
Groovy Question

Building a project with Groovy extension methods in it

I'm trying to build a simple Groovy project which contains a package with a Groovy extension method.

When I try to compile the project the classes using the extension method can't find it. The descriptor and the class are picked up correctly by the IDE.

I guess the problem is that the extension method is needed at compile time by which it probably hasn't been compiled yet.
I've tried creating different sourceSets but with no success.

dependencies {
compile 'org.codehaus.groovy:groovy:2.4.7'
compile 'org.membrane-soa:service-proxy-core:4.2.2'
testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
extensionCompile 'org.codehaus.groovy:groovy:2.4.7'

sourceSets {
extension {
groovy {
include '**/ClosureExtension.groovy'
main {
groovy {
compileClasspath += extension.output

The extension method lives in src/main/groovy/com/predic8/membrane/dsl/extension and the rest of the project in src/main/groovy/com/predic8/membrane/dsl/.

P.S.: The project classes are all annotated with @CompileStatic.


One approach: separate the extension code into its own project, and produce a jar from that project. Then, in another project, the DSL code can use that jar and specify the extension jar as a dependency.

This would make a huge answer, so I mocked it up and placed it on GitHub here (with all due credit to mrhaki's post). There are two projects: extension and dsl. Assuming you understand multi-project builds in Gradle, the essence is this line from dsl/build.gradle:

compileGroovy.dependsOn tasks.getByPath(':extension:jar')