格罗维+ Antbuilder:如何将 Grape 与 taskdef 一起使用?

发布于 2024-08-22 19:23:42 字数 1962 浏览 8 评论 0原文

这与 .我正在尝试使用 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() 使 maven-ant-tasks 可用于 AntBuilder,如下所示:

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 引擎使用的类加载器不同的类加载器中。因此,我采纳了 这个 AntBuilder 类路径问题 的建议并制作了 Grape使用根类加载器:

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)

有关使其正常工作的任何提示吗?或者,整个事情是个坏主意吗?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幸福还没到 2024-08-29 19:23:42

我发现了这个: 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')

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')
月下客 2024-08-29 19:23:42

我们可以使用 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);
    }
}

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文