Groovy with Grape and AntBuilder classloader issue

I wanted to use groovy for a small ftp script and found this post http://www.hhhhq.org/blog/2009/05/01/ftp-using-groovy-and-ant/ Since there were several dependencies, I wanted to use Grape. All dependencies are resolved and are present in the cache. But I can not get Ant to find additional tasks in other libs. He always says

Caught: : Problem: failed to create task or type ftp Cause: the class org.apache.tools.ant.taskdefs.optional.net.FTP was not found. This looks like one of Ant optional components. Action: Check that the appropriate optional JAR exists in -ANT_HOME\lib -the IDE Ant configuration dialogs Do not panic, this is a common problem. The commonest cause is a missing JAR. This is not a bug; it is a configuration problem at GrabTest.runMe(GrabTest.groovy:15) at GrabTest.run(GrabTest.groovy:26) 

Groovy Version: 1.6.5 JVM: 1.6.0_15

Here is my source code

 @Grab(group='ant', module='ant', version='[1.6.5,)') @Grab(group='ant', module='ant-nodeps', version='[1.0,)') @Grab(group='ant', module='ant-apache-oro', version='[1.0,)') @Grab(group='ant', module='ant-commons-net', version='[1.0,)') @Grab(group='apache-oro', module='jakarta-oro', version='[2.0.8,)') @Grab(group='commons-net', module='commons-net', version='[1.4,)') def runMe() { // works println getClass().getClassLoader().loadClass("org.apache.tools.ant.taskdefs.optional.net.FTP") def ant = new AntBuilder() println getClass().getClassLoader() //groovy.lang.GroovyClassLoader$InnerLoader println ant.getClass().getClassLoader() //org.codehaus.groovy.tools.RootLoader ant.ftp( server:"ftp.foo.com", userid:"user", password:"passwd", passive:"yes", verbose:"yes", remotedir:"/pub/incoming", binary:"yes" ) { fileset( dir:"." ) { include( name:"**/*.gz" ) } } } runMe() 

As you can see, I suspect that the class loader is a problem, it seems that Grapes does not inject dependencies there. Any idea how I can make it work?

+7
classloader groovy ant ftp grape
source share
2 answers

You are right in suspecting that the classloader is at the root of the problem. As your code has already pointed out, AntBuilder is loaded from RootLoader, which does not have access to classes loaded with the @Grab annotation. As shown by GROOVY-3730 , Groovy 1.7 will solve this problem.

However, you can solve your problem using the groovy.grape.Grape.grab(Map dependency) method, in which you can install a specific class loader that should be used to load dependencies:

 import groovy.grape.Grape; Grape.grab(group:'ant', module:'ant', version:'1.6.5', classLoader:this.class.classLoader.rootLoader) Grape.grab(group:'ant', module:'ant-nodeps', version:'1.6.5', classLoader:this.class.classLoader.rootLoader) Grape.grab(group:'ant', module:'ant-apache-oro', version:'1.6.5', classLoader:this.class.classLoader.rootLoader) Grape.grab(group:'ant', module:'ant-commons-net', version:'1.6.5', classLoader:this.class.classLoader.rootLoader) Grape.grab(group:'commons-net', module:'commons-net', version:'1.4.1', classLoader:this.class.classLoader.rootLoader) Grape.grab(group:'oro', module:'oro', version:'2.0.8', classLoader:this.class.classLoader.rootLoader) 
+7
source share

Or just use

 @GrabConfig(systemClassLoader=true) 

Additional information can be found: http://groovy.codehaus.org/Grape

0
source share

All Articles