Problem using Ant script with tasks already defined by gradle

Hi,

I am using a legacy Ant build script as a basis for migrating a project.

My Ant script has a test target defined. When I try listing the tasks using gradle I am seeing this error message.

* What went wrong:
A problem occurred evaluating root project 'sample-ant-project'.
> Cannot add task ':test' as a task with that name already exists.

This is my build.gradle file

apply plugin: 'java'
ant.importBuild 'build.xml'
  task wrapper(type: Wrapper) {
   gradleVersion = '1.1'
}
  dependencies wrapper{
   test group: 'junit', name: 'junit', version: '4.10'
}

and this is my Ant script

<?xml version="1.0"?>
<project name="sample Ant project" default="test" basedir=".">
    <target name="test" >
    <echo message="The Ant test target was called."/>
  </target>
  </project>

Any suggestions where I am going wrong ?

Regards, Jeremy

Using Gradle 1.1 on Fedora

The Java plugin already defines a task named test. Ant targets get treated as Gradle tasks if you import them into your build script. Therefore, you see a task naming conflict. If you rename your Ant target to something else e.g. test1 you’ll be fine.

Thank you for the suggestion. I had thought of that but it might cause some friction migrating from the old Ant build system. You know how people are sometimes.

I know a patch to ignore targets that conflict with existing tasks would be a solution but before going down that road any alternative suggestions ?

The ‘ant.importBuild’ feature wasn’t designed to be used together with Gradle plugins in the same project. A potential way out is to do a multi-project build.

Hi Peter,

Ok I might try the multi-project build later. In the mean time I will try renaming the target and see what happens.

Regards, Jeremy

Hi Benjamin,

I tried your solution using my sample project. All conflicting targets were pre-pended with the word legacy.

The Ant build file is imported successfully.

But now I am getting a different error message. This time the junit task used in the legacytest target is not able to find JUnit in the classpath.

* What went wrong:
Execution failed for task ':legacytest'.
> JUnit could not be found in your classpath.
You must download and install it from http://junit.org to build and run the test suite.

Now I defined the same junit dependency for all 4 dependency configurations. Is there something I have missed and should be doing to add junit to the classpath ?

build.xml

<junit>
   <formatter type="brief" usefile="false"/>
   <formatter type="xml" usefile="true" />
     <classpath>
     <pathelement location="${jardir}/library.jar" />
   </classpath>
   <test name="org.noddy.Test"/>
</junit>

build.gradle

dependencies {
   runtime group: 'junit', name: 'junit', version: '4.10'
   compile group: 'junit', name: 'junit', version: '4.10'
   testCompile group: 'junit', name: 'junit', version: '4.10'
   testRuntime group: 'junit', name: 'junit', version: '4.10'
}

Regards, Jeremy