gpt4 book ai didi

Groovy + Antbuilder : How to use Grape to with taskdef?

转载 作者:行者123 更新时间:2023-12-02 07:41:21 27 4
gpt4 key购买 nike

这与 .我正在尝试使用 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 引擎使用的类加载器不同的类加载器中。所以,我接受了this AntBuilder classpath question的建议并使 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)

关于让它发挥作用有什么提示吗?或者,整个事情是个坏主意吗?

最佳答案

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

关于Groovy + Antbuilder : How to use Grape to with taskdef?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2347571/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com