Groovy + Antbuilder: How to use Grape to with taskdef?
This is related to . I'm trying to dynamically add the maven-ant-tasks jars with Grape, simulating this:
<taskdef uri="antlib:org.apache.maven.artifact.ant"
resource="org/apache/maven/artifact/ant/antlib.xml"
开发者_JAVA百科 classpathref="ant.classpath" />
I've tried to use Grape.grab() to make maven-ant-tasks available to AntBuilder like this:
import groovy.grape.Grape
println "grab..."
Grape.grab(group:'ant', module:'ant', version:'1.7.0', classLoader:this.class.classLoader.rootLoader)
Grape.grab(group: 'org.apache.maven', module: 'maven-ant-tasks', version: '2.0.9')
println "ant taskdef..."
def ant = new AntBuilder()
ant.taskdef (resource: "org/apache/maven/artifact/ant/antlib.xml" )
but that doesn't work because Grape adds the modules to a different ClassLoader from the one that ANT engine is using. So, I took the advice from this AntBuilder classpath question and made Grape use the root ClassLoader:
import groovy.grape.Grape
println "grab..."
Grape.grab(group:'ant', module:'ant', version:'1.7.0', classLoader:this.class.classLoader.rootLoader)
Grape.grab(group: 'org.apache.maven', module: 'maven-ant-tasks', version: '2.0.9', classLoader: this.class.classLoader.rootLoader)
println "ant taskdef..."
def ant = new AntBuilder()
ant.taskdef (resource: "org/apache/maven/artifact/ant/antlib.xml" )
Now it throws a LinkageError:
Caught: : java.lang.LinkageError: loader constraint violation: when resolving overridden method "org.apache.tools.ant.helper.ProjectHelper2$RootHandler.setDocumentLocator(Lorg/xml/sax/Locator;)V" the class loader (instance of org/codehaus/groovy/tools/RootLoader) of the current class, org/apache/tools/ant/helper/ProjectHelper2$RootHandler, and its superclass loader (instance of <bootloader>), have different Class objects for the type org/xml/sax/Locator used in the signature
at test.mavenanttasks.run(mavenanttasks.groovy:11)
Any hints on getting this to work? Or, is the whole thing a bad idea?
I found this: http://groovy.codehaus.org/Using+Ant+Libraries+with+AntBuilder
First, put maven-ant-tasks in the root classloader with:
@Grapes([
@Grab(group = 'org.apache.maven', module = 'maven-ant-tasks', version = '2.1.3'),
@GrabConfig(systemClassLoader = true)
])
Alternatively, for older versions of Groovy:
Grape.grab(group: 'org.apache.maven', module: 'maven-ant-tasks', version: '2.1.3', classLoader: this.class.classLoader.rootLoader)
The Maven ANT tasks are an 'AntLib', and they can be loaded like this:
import groovy.xml.NamespaceBuilder
def ant = new AntBuilder()
def mvn = NamespaceBuilder.newInstance(ant, 'antlib:org.apache.maven.artifact.ant')
We can create ant path(classpath) using java.net.URLClassLoader or it's subclass eg:
<!-- language: groovy -->
@Grapes([
@Grab(group='org.jacoco', module='org.jacoco.ant', version= '0.7.4.201502262128'),
@Grab(group='org.jacoco', module='org.jacoco.core', version= '0.7.4.201502262128'),
@Grab('org.jacoco:org.jacoco.report:0.7.4.201502262128'),
])
class Tool {
def dump(args){
println "do dump...:$args"
println "DumpTask:"+org.jacoco.ant.DumpTask.classLoader;
def ant = new AntBuilder();
def path1 = ant.path{
org.jacoco.ant.DumpTask.classLoader.getURLs().each {
ant.pathelement(location:it.getFile())
}
}
println "path11:$path1";
// classname way
// ant.taskdef(name:"dump",classname:"org.jacoco.ant.DumpTask",classpath:path1);
// ant.dump(args);
//namespace way
ant.taskdef(uri:'antlib:org.jacoco.ant',resource:'org/jacoco/ant/antlib.xml',classpath:path1);
def jacoco=groovy.xml.NamespaceBuilder.newInstance(ant, 'antlib:org.jacoco.ant')
jacoco.dump(args);
}
}
精彩评论