I have a groovy library that I publish as a jar file on a nexus repository. When I use extension methods in the library from another project's Gradle script, I get a MissingMethodException.
As a reference, say I have a String extension method, such as:
static boolean containsIgnoreCase(String self, String str) {
self.toLowerCase().contains(str.toLowerCase())
}
If in my library I call the method using "foobar".containsIgnoreCase("Foo")
, I will get the exception. If I instead call it using StringExtensions.containsIgnoreCase("foobar", "Foo")
, it works, no problem.
My guess is that this is an issue with publishing the Groovy project without the META-INF file that defines the extensions. Here is the project structure:
- Library
- src/main/
- groovy/
- (here are my sources)
- resources/META-INF/groovy
- org.codehaus.groovy.runtime.ExtensionModule (contains details about my extension classes)
My ExtensionModule file looks like this:
moduleName=string-extensions
moduleVersion=1.0
extensionClasses=com.my.project.StringExtensions
In my publish block in build.gradle, I use the following:
plugins {
id 'groovy'
id 'java'
id 'maven-publish'
}
//...
sourceSets {
main.groovy.outputDir = sourceSets.main.java.outputDir
test.groovy.outputDir = sourceSets.test.java.outputDir
}
//...
publishing {
publications {
library(MavenPublication) {
from components.java
}
}
//...
}
What do I need to include in my publication in order to get the extension methods correctly registered when I use this library in another project's Gradle build script? I have added the classpath/repo url for the dependency in my buildscript, and can access methods of the library - these just fail when calling an extension method.