How to use Ant's 'Parallel' target?

I’ve got a task which basically runs unit tests in predefined groups. We want to run these groups in parallel so that it completes quicker. The ‘maxParallelForks’ property of ‘Test’ task doesn’t seem serve our purpose in this case so instead, we thought we can just use ant’s ‘Parallel’ target.

I’ve done something like below:

task runAllTestsByGroups << {

ant.parallel(threadsPerProcessor: 1) {

// Test tasks defined within the same build script

runTestGroup1

runTestGroup2

runTestGroup3

}

}

However, when executing ‘runAllTestsByGroups’, it simply complete directly without actually running any tests.

So my questions are:

  1. what am I doing wrong here? And basically is it possible to reuse Ant’s ‘Parallel’ target in Gradle?

  2. It’s surprising that Gradle does not provide a similar solution for running tasks in parallel like the ‘Parallel’ target provided by Ant. Is there no native way to execute tasks in parallel in Gradle?

You can’t have an Ant target reference a Gradle task in that way. If you wanted to use the Ant parallel task then you’d have to additionally declare your test tasks and Ant tasks. Support for running tasks within a single project in parallel is currently in development and should make it in a Gradle release soon.

Thanks for the quick response Mark!

the three “runTestGroupX” tasks are defined in the same gradle build script. Could you give me a exmaple of how to properly define these tasks and the Ant task?

I’d take a look at the Ant documentation on how to do that.

Sorry, I didn’t quite get what you meant by “additionally declare your test tasks and Ant tasks”.

Did you mean if I want to use Ant’s ‘Parallel’ task in my build script, I have to give it Ant’s task to execute, instead of Gradle tasks? Something like below:

task runAllTestsByGroups << {
      ant.parallel(threadsPerProcessor: 1) {
                    ant.junit (...) {
            // run test group 1
            ...
        }
          ant.junit(...) {
            // run test group 2
            ...
       }
         // run group 3, 4, 5 ...
    }
  }

Correct.