Divide a fileTree into parts to run in separate ant batches

I have a gradle task that collects a bunch of tests in a filetree and then uses ant.junit to run them

includedTests = fileTree(dir: "${targetDir}",
              include: ["**/*Test*.class"],
              exclude: [ "**/TestNotToBeRun.class"])
      includedTests = includedTests.collect { it }

ant.junit(...) {
      ...
     ....
     batchtest(todir: "${testDir}") {
         ant.filelist(dir: "${targetDir}", files: includedTests.join(","))
    }
}

I want to run these tests in separate jvms and the number will be provided as a property say 4. I wanted to do this by using forkmode: “perBatch”. Hence I am trying to divide the Batch into 4 parts. Is there a way I can divide the filetree into 4 parts? Or can I divide the ant filelist into 4 parts?

I was thinking of doing something like this post: https://stackoverflow.com/questions/2373589/how-to-split-an-fileset-in-ant-into-multiple-sets

  def filelist = ant.filelist(dir: "${targetTestDir}/build", files: includedTests.join(","))
  def batchSize = (includedTests.length/4) + 1;
  def batch = 1;
  for (def i = 0; i < includedTests.length; i += batchSize)
  {
    // Create a new fileset to hold the sub-Fileset.
    def filelistN = project.createDataType( "filelist" );
    filelistN.setDir(filelist.getDir());

    // Give the new Fileset an id and associate with the project.
    project.addReference( "Filelist" + batch, filelistN );

    // Populate the sub-Fileset.
    for (def j = 0; j < batchSize && ( i + j ) < includedTests.length; j++ )
    {
      filelistN.setIncludes( includedTests[i + j] );
    }

    batch++;
  }
  //Not sure this is possible. Or what the correct format would be for the following
  batchtest(todir: "${testDir}") {
         filelist1
  }
FileTree allTests = fileTree(...) 
int batchCount = 4
List<List<File>> batches = (1..batchCount).collect {[]} 
allTests.eachWithIndex { file, index ->
   batches[index%batchCount] << file
} 
ant.junit(...) {
   batches.each { batch ->
      batchtest(todir: "${testDir}") {
         ant.filelist(dir: "${targetDir}", files: batch.join(","))
      }
   }    
} 
1 Like

Thanks so much. Is there a way to define a separate system property per batch? I want to set a runner number. I tried to put sysProperty(key: “runner”, value: i) in the batchtest block, where the value of i increments for each batch. Like below:

  batchtest(todir: "${testDir}") {
     ant.filelist(dir: "${targetDir}", files: batch.join(","))
  }

But I get the following error: batchtest doesn’t support the nested “sysProperty” element
Is there a way to set a system property for each of the batch tests? Assuming they are run on separate JVMs

That sounds like an ant question