Groovy + Antbuilder:如何在taskdef中使用Grape?

时间:2010-02-27 14:01:33

标签: ant groovy classloader grape taskdef

这与此有关。我正在尝试用Grape动态添加maven-ant-tasks jar,模拟这个:

  <taskdef uri="antlib:org.apache.maven.artifact.ant"
           resource="org/apache/maven/artifact/ant/antlib.xml"
           classpathref="ant.classpath" />

我尝试使用Grape.grab()为AntBuilder提供maven-ant-tasks,如下所示:

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" )

但这不起作用,因为Grape将模块添加到与ANT引擎正在使用的类不同的ClassLoader中。所以,我接受了this AntBuilder classpath question的建议,并使Grape使用了根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" )

现在它抛出一个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)

任何关于让它发挥作用的提示?或者,这是一个坏主意吗?

2 个答案:

答案 0 :(得分:2)

我发现了这个:http://groovy.codehaus.org/Using+Ant+Libraries+with+AntBuilder

首先,将maven-ant-tasks放在根类加载器中:

@Grapes([
@Grab(group = 'org.apache.maven', module = 'maven-ant-tasks', version = '2.1.3'),
@GrabConfig(systemClassLoader = true)
])

或者,对于旧版本的Groovy:

Grape.grab(group: 'org.apache.maven', module: 'maven-ant-tasks', version: '2.1.3', classLoader: this.class.classLoader.rootLoader)

Maven ANT任务是一个'AntLib',它们可以像这样加载:

import groovy.xml.NamespaceBuilder
def ant = new AntBuilder()
def mvn = NamespaceBuilder.newInstance(ant, 'antlib:org.apache.maven.artifact.ant')

答案 1 :(得分:2)

我们可以使用java.net.URLClassLoader或它的子类创建ant路径(classpath) 例如:

<!-- 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);
    }
}