Gradle calculate ant dependencies differently

I found this issue in Gradle 2.2.1, below is a demo for explaining it, all files are in a same directory:

main.xml

<?xml version="1.0" encoding="UTF-8"?>
<project
  default="function"
  name="main"
>
<import file="./sub-script.xml"/>
<target name="function" depends="sub.function, cleanStuff">
    <echo message="I am in main function!"/>
</target>

<target name="function2"  depends="sub.function2, cleanStuff">
    <echo message="I am in main function2!"/>
</target>

<target name="cleanStuff">
    <echo message="I am cleaning!!"/>
</target>

</project>

sub-script.xml

<?xml version="1.0" encoding="UTF-8"?>
<project
  default="function"
  name="sub"
>indent preformatted text by 4 spaces
<target name="function">
    <echo message="I am in sub function!"/>
</target>

<target name="function2">
    <echo message="I am in sub function2!"/>
</target>

<target name="entrance" depends="function, function2"/>

</project>

build.gradle

ant.importBuild file('main.xml')

Under the command line, if I run ant -f main.xml entrance, the output is:

sub.function:
     [echo] I am in sub function!

cleanStuff:
     [echo] I am cleaning!!

function:
     [echo] I am in main function!

sub.function2:
     [echo] I am in sub function2!

function2:
     [echo] I am in main function2!

entrance:

BUILD SUCCESSFUL

While if I run gradle entrance, the output is:

:sub.function
[ant:echo] I am in sub function!
:sub.function2
[ant:echo] I am in sub function2!
:cleanStuff
[ant:echo] I am cleaning!!
:function
[ant:echo] I am in main function!
:function2
[ant:echo] I am in main function2!
:entrance

BUILD SUCCESSFUL

As you can see, the task sequence is different between ant and gradle.

I cannot say if it is a defect for gradle as the calculated sequence does not break any rules, but surely it may cause confusions when developers are migrating from ant to gradle.

Ant attempts to preserve order when multiple target dependencies are listed. Gradle makes no such guarantee. Since every Ant target is created as a Gradle task, the Gradle task execution order rules take effect. If the order is important here you can configure the task ordering to make the Gradle build behave similarly.

Hi Mark,

Thanks a lot for your explanation.