Classpath for ant plugins when using ANTBuilder fr

2019-04-08 09:23发布

问题:

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'

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)

How can Gradle's ant integration be instructed to make this available?

回答1:

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.

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() )

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())
}

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



标签: ant gradle