So I’m assuming from your post that you will not be able to change the ant build files at all.
Adding the taskdef line to my ant build.xml:
<project>
<taskdef resource="net/sf/antcontrib/antlib.xml" />
<target name="test">
<foreach list="Alice, why, is, a, raven, like, a, writing, desk?" param="x" target="igetcalled" />
</target>
<target name="igetcalled">
<echo message="${x}" />
</target>
</project>
and executing it via gradle with the same build.gradle as above gives the following:
nadurra:anttest mbjarland$ gradle callAnt
:callAnt
PATH: /Users/mbjarland/.gradle/caches/artifacts-3/ant-contrib/ant-contrib/c12498cf18507aa6433a94eb7d3e77d5/jars/ant-contrib-1.0b3.jar:/Users/mbjarland/.gradle/caches/artifacts-3/ant/ant/c12498cf18507aa6433a94eb7d3e77d5/jars/ant-1.5.jar
[ant:taskdef] Could not load definitions from resource net/sf/antcontrib/antlib.xml. It could not be found.
[ant:taskdef] Could not load definitions from resource net/sf/antcontrib/antlib.xml. It could not be found.
[ant:echo] Alice
[ant:taskdef] Could not load definitions from resource net/sf/antcontrib/antlib.xml. It could not be found.
[ant:echo]
why
[ant:taskdef] Could not load definitions from resource net/sf/antcontrib/antlib.xml. It could not be found.
[ant:echo]
is
[ant:taskdef] Could not load definitions from resource net/sf/antcontrib/antlib.xml. It could not be found.
[ant:echo]
a
[ant:taskdef] Could not load definitions from resource net/sf/antcontrib/antlib.xml. It could not be found.
[ant:echo]
raven
[ant:taskdef] Could not load definitions from resource net/sf/antcontrib/antlib.xml. It could not be found.
[ant:echo]
like
[ant:taskdef] Could not load definitions from resource net/sf/antcontrib/antlib.xml. It could not be found.
[ant:echo]
a
[ant:taskdef] Could not load definitions from resource net/sf/antcontrib/antlib.xml. It could not be found.
[ant:echo]
writing
[ant:taskdef] Could not load definitions from resource net/sf/antcontrib/antlib.xml. It could not be found.
[ant:echo]
desk?
BUILD SUCCESSFUL
Total time: 4.427 secs
nadurra:anttest mbjarland$
in oher words, the build works as the custom tasks are already defined from gradle, but the ant execution prints out warnings because it can not execute the taskdef calls. So this is now only a question of polluting the execution output.
There is a slightly hacky way to get around this…and this is getting ugly. In ant it is possible to override an existing task definition using, for example, a macrodef. So as it is the ‘taskdef’ task printing out all those warnings above, we can redefine it.
Resulting files:
build.gradle
repositories {
mavenCentral()
}
configurations {
antcp {
description = 'ant classpath'
transitive = true
//exclude module: 'ant'
}
}
dependencies {
antcp "ant-contrib:ant-contrib:1.0b3"
}
task callAnt << {
println "PATH: ${configurations.antcp.asPath}"
ant.taskdef resource: "net/sf/antcontrib/antcontrib.properties",
classpath: configurations.antcp.asPath
println "- Overriding task 'taskdef' from gradle, you can ignore the warning on the line below -"
ant.macrodef(name: 'taskdef') {
attribute name: "resource",
default: "foo"
attribute name: "name",
default: "foo"
attribute name: "classname", default: "foo"
sequential {
//do nothing, a no-op 'taskdef'
//echo message: "Ignoring taskdef as call is coming from gradle"
}
}
ant.ant antfile: "build.xml", target: "test", dir: "."
}
build.xml
<project>
<taskdef resource="net/sf/antcontrib/antlib.xml" />
<target name="test">
<foreach list="Alice, why, is, a, raven, like, a, writing, desk?" param="x" target="igetcalled" />
</target>
<target name="igetcalled">
<echo message="${x}" />
</target>
</project>
resulting execution log:
$ gradle callAnt :callAnt PATH: /Users/mbjarland/.gradle/caches/artifacts-3/ant-contrib/ant-contrib/c12498cf18507aa6433a94eb7d3e77d5/jars/ant-contrib-1.0b3.jar:/Users/mbjarland/.gradle/caches/artifacts-3/ant/ant/c12498cf18507aa6433a94eb7d3e77d5/jars/ant-1.5.jar - Overriding task 'taskdef' from gradle, you can ignore the warning on the line below - Trying to override old definition of task taskdef [ant:echo] Alice [ant:echo] why [ant:echo] is [ant:echo] a [ant:echo] raven [ant:echo] like [ant:echo] a [ant:echo] writing [ant:echo] desk?
BUILD SUCCESSFUL
Total time: 5.081 secs
…not saying this is pretty…but it does work. Let me know if we managed to solve your issue this time : )