[GRADLE-771] Handle name clashes between Gradle tasks and imported Ant targets Created: 07/Dec/09  Updated: 10/Nov/14  Resolved: 27/Aug/14

Status: Resolved
Project: Gradle
Affects Version/s: None
Fix Version/s: 2.2-rc-1

Type: Improvement
Reporter: Tomek Kaczanowski Assignee: Luke Daley
Resolution: Fixed Votes: 11


a typical migration scenario of your Ant Java project starts with build.gradle looking like this:
ant.importBuild 'build.xml'
usePlugin 'java'

it will probably fail because of name clash of tasks from build.xml and those provided by Java plugin:
FAILURE: Build failed with an exception.

  • Where:
    Build file '/home/tomek/temp/reportng/reportng/build.gradle' line: 3
  • What went wrong:
    A problem occurred evaluating root project 'reportng'.
    Cause: Cannot add task ':clean' as a task with that name already exists.

Possible solutions (ideas from mailing list discussion):
a) implement an "ovewriteTaskByDefault" feature in Gradle
b) I think the solution lives on AntBuilder or some other Ant-specific thing. Some options:

  • ant.import() allows you to specify a namespace for the imported tasks, eg prefix each task with 'ant.'
  • ant.import() allows you to specify which tasks to include or exclude when importing.

Comment by Taylor Brown [ 07/Feb/12 ]

Being able to put a namespace on my imported tasks would be helpful. I am in the process of migrating from ant, and this feature would have helped me. I use the GradleFx plugin which defines a "test" task. So does my ant build. I will be editing my build.xml of course, but it would have been nice to say, "Put all of these tasks into my custom namespace".

Comment by Rohitashva Mathur [ 27/Mar/12 ]

You can create your own namespace for these ant tasks by leveraging an intermediate ant build file that references the original ant build file and attaches a namespace to the tasks defined therein.

So, if the original build file is build.xml, create another ant build file called wrapper.xml and define it like the following fragment -

<project default="test">

<include file="build.xml" as="somename"/>


in the gradle build script instead of referring build.xml, refer wrapper.xml

//ant.importBuild 'build.xml'
ant.importBuild 'wrapper.xml'

All the ant tasks are now available under namespace "somename". A task named 'clean' in build.xml can now be invoked via gradle with names as somename.clean, and build.gradle can have its own version of clean.

gradle clean – clean in build.gradle
gradle somename.clean – clean from the build.xml

Comment by Peter Sipos [ 20/Mar/13 ]

This would also be very useful when using multiple plugins. Actually even for simple use-cases like a flex build which publishes to a maven repo, you need maven plugin, which requires Java plugin and gradleFx both defining the basic tasks, like clean, test. I also encountered the migration issue when imported legacy build.xml - it will surely have 'clean' and similar basic tasks names. This feature is a MUST.

Comment by Erik Vonderheid [ 06/Mar/14 ]

Here is another solution without a wrapper XML:

Thanks for the solution Peter.

C:\Users\mahmood\dev\gradle\ant-with-local-buildXml>tree /F /A | findstr "build settings ant"
| build.gradle
| build.xml
| settings.gradle

C:\Users\mahmood\dev\gradle\ant-with-local-buildXml>type build.gradle

project(':ant') {
ant.importBuild '../build.xml'

task hellogradle << {
println 'hello from gradle'

task helloant << {
println 'hello from gradle to ant'
C:\Users\mahmood\dev\gradle\ant-with-local-buildXml>type settings.gradle
include 'ant'
C:\Users\mahmood\dev\gradle\ant-with-local-buildXml>gradle tasks

Other tasks

Total time: 3.603 secs
C:\Users\mahmood\dev\gradle\ant-with-local-buildXml>gradle helloant
hello from gradle to ant
[ant:echo] hello from ant


Total time: 3.248 secs
C:\Users\mahmood\dev\gradle\ant-with-local-buildXml>gradle :ant:helloant
[ant:echo] hello from ant


Comment by Peter Butkovic [ 28/Apr/14 ]

not sure why, but workaround proposed by @RohitashvaMathur didn't work for me.

Generated at Wed Jun 30 11:38:29 CDT 2021 using Jira 8.4.2#804003-sha1:d21414fc212e3af190e92c2d2ac41299b89402cf.