I did some investigation why this is not working. When using the @Grap annotation in a script the exception below is thrown. Adding the Ivy library to the plugin did not fix the problem.
Each plugin is loaded with a separate classloader and the plugin classloaders are children of the Jenkins classloader. Since Groovy is provided by Jenkins, the Groovy classes are loaded by the Jenkins classloader. But the Ivy classes are not visible to the Jenkins classloader, they are only visible to the plugin classloader. So they can not be loaded when reference from Groovy classes which leads to the exception.
There are at least two solutions for this problem:
- The Ivy library has to be included in Jenkins.
- The plugin has to bring its own Groovy library and some classloader magic has to be added so that the GroovyScriptEngine is loaded by the plugin classloader.
FATAL: org/apache/ivy/core/report/ResolveReport
java.lang.NoClassDefFoundError: org/apache/ivy/core/report/ResolveReport
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2436)
at java.lang.Class.getDeclaredMethods(Class.java:1793)
at org.codehaus.groovy.reflection.CachedClass$3$1.run(CachedClass.java:84)
at java.security.AccessController.doPrivileged(Native Method)
at org.codehaus.groovy.reflection.CachedClass$3.initValue(CachedClass.java:81)
at org.codehaus.groovy.reflection.CachedClass$3.initValue(CachedClass.java:79)
at org.codehaus.groovy.util.LazyReference.getLocked(LazyReference.java:46)
at org.codehaus.groovy.util.LazyReference.get(LazyReference.java:33)
at org.codehaus.groovy.reflection.CachedClass.getMethods(CachedClass.java:250)
at groovy.lang.MetaClassImpl.populateMethods(MetaClassImpl.java:341)
at groovy.lang.MetaClassImpl.fillMethodIndex(MetaClassImpl.java:291)
at groovy.lang.MetaClassImpl.initialize(MetaClassImpl.java:2900)
at org.codehaus.groovy.reflection.ClassInfo.getMetaClassUnderLock(ClassInfo.java:166)
at org.codehaus.groovy.reflection.ClassInfo.getMetaClass(ClassInfo.java:182)
at groovy.grape.GrapeIvy.$getStaticMetaClass(GrapeIvy.groovy)
at groovy.grape.GrapeIvy.<init>(GrapeIvy.groovy:69)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:357)
at java.lang.Class.newInstance(Class.java:310)
at groovy.grape.Grape.getInstance(Grape.java:101)
at groovy.grape.Grape.grab(Grape.java:136)
at groovy.grape.GrabAnnotationTransformation.visit(GrabAnnotationTransformation.java:283)
at org.codehaus.groovy.transform.ASTTransformationVisitor$3.call(ASTTransformationVisitor.java:302)
at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:843)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:548)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:524)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:501)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:306)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:287)
at groovy.util.GroovyScriptEngine$ScriptClassLoader.parseClass(GroovyScriptEngine.java:197)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:267)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:214)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:224)
at javaposse.jobdsl.dsl.DslScriptLoader.runDslEngine(DslScriptLoader.java:75)
at javaposse.jobdsl.plugin.ExecuteDslScripts.perform(ExecuteDslScripts.java:130)
at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:19)
at hudson.model.AbstractBuild$AbstractRunner.perform(AbstractBuild.java:703)
at hudson.model.Build$RunnerImpl.build(Build.java:178)
at hudson.model.Build$RunnerImpl.doRun(Build.java:139)
at hudson.model.AbstractBuild$AbstractRunner.run(AbstractBuild.java:473)
at hudson.model.Run.run(Run.java:1410)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:46)
at hudson.model.ResourceController.execute(ResourceController.java:88)
at hudson.model.Executor.run(Executor.java:238)
Caused by: java.lang.ClassNotFoundException: org.apache.ivy.core.report.ResolveReport
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at winstone.classLoader.WebappClassLoader.loadClass(WebappClassLoader.java:83)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 48 more
I did some investigation why this is not working. When using the @Grap annotation in a script the exception below is thrown. Adding the Ivy library to the plugin did not fix the problem.
Each plugin is loaded with a separate classloader and the plugin classloaders are children of the Jenkins classloader. Since Groovy is provided by Jenkins, the Groovy classes are loaded by the Jenkins classloader. But the Ivy classes are not visible to the Jenkins classloader, they are only visible to the plugin classloader. So they can not be loaded when reference from Groovy classes which leads to the exception.
There are at least two solutions for this problem: