ExpandoMetaClass.enableGlobally() causes error in Gradle internals

I have a library that depends on adding functionality to types like String and List and requires enableGlobally to be called on the ExpandoMetaClass. If I do that I get the exception below when running the build.

Is this a known limitation of Gradle?

org.gradle.api.GradleScriptException: A problem occurred evaluating root project 'b-one-crm'.
 at org.gradle.groovy.scripts.DefaultScriptRunnerFactory$ScriptRunnerImpl.run(DefaultScriptRunnerFactory.java:51)
 at org.gradle.configuration.DefaultScriptPluginFactory$ScriptPluginImpl.apply(DefaultScriptPluginFactory.java:127)
 at org.gradle.configuration.BuildScriptProcessor.evaluate(BuildScriptProcessor.java:38)
 at org.gradle.configuration.DefaultProjectEvaluator.evaluate(DefaultProjectEvaluator.java:38)
 at org.gradle.api.internal.project.AbstractProject.evaluate(AbstractProject.java:487)
 at org.gradle.api.internal.project.AbstractProject.evaluate(AbstractProject.java:71)
 at org.gradle.configuration.ProjectEvaluationConfigurer.execute(ProjectEvaluationConfigurer.java:23)
 at org.gradle.configuration.ProjectEvaluationConfigurer.execute(ProjectEvaluationConfigurer.java:21)
 at org.gradle.configuration.DefaultBuildConfigurer$1.execute(DefaultBuildConfigurer.java:38)
 at org.gradle.configuration.DefaultBuildConfigurer$1.execute(DefaultBuildConfigurer.java:35)
 at org.gradle.api.internal.project.AbstractProject.configure(AbstractProject.java:463)
 at org.gradle.api.internal.project.AbstractProject.allprojects(AbstractProject.java:458)
 at org.gradle.configuration.DefaultBuildConfigurer.configure(DefaultBuildConfigurer.java:35)
 at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:141)
 at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradleLauncher.java:112)
 at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLauncher.java:80)
 at org.gradle.launcher.RunBuildAction.execute(RunBuildAction.java:41)
 at org.gradle.launcher.RunBuildAction.execute(RunBuildAction.java:27)
 at org.gradle.launcher.ExceptionReportingAction.execute(ExceptionReportingAction.java:32)
 at org.gradle.launcher.ExceptionReportingAction.execute(ExceptionReportingAction.java:21)
 at org.gradle.launcher.CommandLineActionFactory$WithLoggingAction.execute(CommandLineActionFactory.java:219)
 at org.gradle.launcher.CommandLineActionFactory$WithLoggingAction.execute(CommandLineActionFactory.java:203)
 at org.gradle.launcher.Main.execute(Main.java:55)
 at org.gradle.launcher.Main.main(Main.java:40)
 at org.gradle.launcher.ProcessBootstrap.runNoExit(ProcessBootstrap.java:46)
 at org.gradle.launcher.ProcessBootstrap.run(ProcessBootstrap.java:28)
 at org.gradle.launcher.GradleMain.main(GradleMain.java:24)
Caused by: groovy.lang.MissingMethodException: No signature of method: org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.fileTree() is applicable for argument types: (org.codehaus.groovy.runtime.GStringImpl) values: [/home/tk/workspace/com.one.crm/b-one-crm/dependent-jars]
 at org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.methodMissing(DefaultDependencyHandler.groovy:81)
 at org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.invokeMethod(DefaultDependencyHandler.groovy)
 at build_uad1vfubd7eikgsgomrlujmlb$_run_closure20_closure82.doCall(/home/tk/workspace/com.one.crm/b-one-crm/build.gradle:174)
 at org.gradle.util.ConfigureUtil.configure(ConfigureUtil.java:61)
 at org.gradle.util.ConfigureUtil.configure(ConfigureUtil.java:31)
 at org.gradle.api.internal.project.AbstractProject.dependencies(AbstractProject.java:893)
 at build_uad1vfubd7eikgsgomrlujmlb$_run_closure20.doCall(/home/tk/workspace/com.one.crm/b-one-crm/build.gradle:173)
 at org.gradle.util.ConfigureUtil.configure(ConfigureUtil.java:61)
 at org.gradle.util.ConfigureUtil.configure(ConfigureUtil.java:31)
 at org.gradle.api.internal.project.AbstractProject.configure(AbstractProject.java:874)
 at org.gradle.api.internal.project.AbstractProject.configure(AbstractProject.java:879)
 at org.gradle.api.internal.BeanDynamicObject.invokeMethod(BeanDynamicObject.java:158)
 at org.gradle.api.internal.CompositeDynamicObject.invokeMethod(CompositeDynamicObject.java:93)
 at org.gradle.api.internal.project.DefaultProject_Decorated.invokeMethod(Unknown Source)
 at org.gradle.groovy.scripts.BasicScript.methodMissing(BasicScript.groovy:68)
 at build_uad1vfubd7eikgsgomrlujmlb.run(/home/tk/workspace/com.one.crm/b-one-crm/build.gradle:159)
 at org.gradle.groovy.scripts.DefaultScriptRunnerFactory$ScriptRunnerImpl.run(DefaultScriptRunnerFactory.java:49)
 ... 26 more

Are you using your library in the build script? Can you post the build script, especially the part that causes the exception (around line 174, use of fileTree())?

I’ve isolated the problem down to this:

  // ExpandoMetaClass.enableGlobally()

 configure(subprojects) {







dependencies {





 fileTree("$rootProject.projectDir/dependent-jars").include('*.jar').each {





 }



}

  }  

Running gradle -t with the enableGlobally call commented out runs ok but if it is commented in I get:

  Build file '/home/tk/workspace/com.one.crm/b-one-crm/build.gradle' line: 6

 * What went wrong:  A problem occurred evaluating root project 'b-one-crm'.  Cause: No signature of method: org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.fileTree() is applicable for argument types: (org.codehaus.groovy.runtime.GStringImpl) values: [/home/tk/workspace/com.one.crm/b-one-crm/dependent-jars]  

You forgot to specify a configuration like “compile” before “fileTree”. Maybe that will solve the problem.

I should be able to iterate over a filetree inside a dependency closure regardless, or? My original code was:

dependencies {
        fileTree("$rootProject.projectDir/dependent-jars").include('*.jar').each {
            compile ":${it.name[0..-5]}::"
        }
    }

I removed the compile because it didn’t have any effect on the error.

I noticed that my cut-down example actually works if I run it in a clean directory, but not if I run it in my project directory. I’ll try to work around this by rewriting the library to use categories rather than expandometaclass.