使用 Gradle 中的 ANTBuilder 时 ant 插件的类路径 [英] Classpath for ant plugins when using ANTBuilder from Gradle

查看:45
本文介绍了使用 Gradle 中的 ANTBuilder 时 ant 插件的类路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 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 的 ant 集成使其可用?>

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.

在这里查看一些输入 http://gradle.1045684.n5.nabble.com/How-to-add-to-classpath-for-ant-importBuild-td3268631.html

这篇关于使用 Gradle 中的 ANTBuilder 时 ant 插件的类路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆