4

这与 . 我正在尝试用 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 中。所以,我从这个 AntBuilder 类路径问题中得到了建议,让 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)

关于让它发挥作用的任何提示?或者,整件事是个坏主意?

4

2 回答 2

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')
于 2010-06-24T15:45:06.817 回答
2

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

<!-- 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);
    }
}
于 2015-07-18T12:14:45.903 回答