从Gradle使用ANTBuilder时ant插件的类路径 [英] Classpath for ant plugins when using ANTBuilder from Gradle
问题描述
我有一个build.gradle
文件,该文件加载 PMD (从上游Maven下载),然后加载需要PMD的Ant build.xml文件:
I have a build.gradle
file which loads PMD (downloading it from upstream Maven), and then loads an Ant build.xml file which requires PMD:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'pmd:pmd:4.2.5'
}
}
ant.importBuild 'shared-build.xml'
但是,Ant导入失败:
However, the Ant import fails:
taskdef class net.sourceforge.pmd.ant.PMDTask cannot be found
using the classloader AntClassLoader[]
at org.apache.tools.ant.ProjectHelper.addLocationToBuildException(ProjectHelper.java:551)
[...]
at org.gradle.api.internal.project.DefaultAntBuilder.importBuild(DefaultAntBuilder.groovy:76)
如何指示 Gradle的蚂蚁集成使之可用?>
How can Gradle's ant integration be instructed to make this available?
推荐答案
没有任何简便的方法,因为Gradle对此不提供任何API支持.因此,您需要以某种方式对其进行破解.
There's no straighforward way to do it, as Gradle does not offer any API support for this. So you need to hack it some way.
例如,您可以在调用ant.importBuild
For example, you can do something like this, right before calling ant.importBuild
org.apache.tools.ant.Project.class.classLoader.addURL( file('libs/somelib.jar').toURI().toURL() )
或者,您也可以使用通过Gradle的依赖项解析获得的路径来调用addURL()方法(同样,应在调用ant.importBuild之前执行该路径).
Alternatively you can call the addURL() method with the paths you get through the Gradle's dependency resolution (again, this should be executed before the call to ant.importBuild).
configurations { someconf }
dependencies { someconf "org.eclipse.jdt:ecj:3.6.1" }
def antClassLoader = org.apache.tools.ant.Project.class.classLoader
configurations.someconf.each { File f ->
antClassLoader.addURL(f.toURI().toURL())
}
当然,另一种解决方案是在build.xml文件中定义类路径,这样您就不必从Gradle进行任何操作.
Of course, another solution would be to have the classpath defined inside your build.xml file so you won't have to do anything from Gradle.
See some input here http://gradle.1045684.n5.nabble.com/How-to-add-to-classpath-for-ant-importBuild-td3268631.html
这篇关于从Gradle使用ANTBuilder时ant插件的类路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!