问题
I'm using ant to drive some test automation. I have a flattened folder that has about 100 files of the same type. I'd like to spread these files across 4 folders evenly to spread the execution across a few machines. So the project would create the four folders and then run through the hundred files passing one file to a folder and then continue on. file 1 goes to folder 1, 2 to 2, 3 to 3, 4 to 4, 5 to 1, etc. The names and numbers of files will fluctuate. I can write a small utility to do this but it would be simplier for maintanace if I could do this as part of the ant execution.
回答1:
The following example uses the groovy ANT task which integrates very well into an ANT build:
<project name="demo" default="distribute">
<target name="bootstrap">
<mkdir dir="${user.home}/.ant/lib"/>
<get dest="${user.home}/.ant/lib/groovy-all.jar" src="http://search.maven.org/remotecontent?filepath=org/codehaus/groovy/groovy-all/2.1.0-beta-1/groovy-all-2.1.0-beta-1.jar"/>
</target>
<target name="distribute">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/>
<fileset id="srcFiles" dir="build/src" includes="*.txt"/>
<groovy>
def i = 0
project.references.srcFiles.each {
ant.copy(file:it, todir:"build/dir/${i % 4}", verbose:true)
i++
}
</groovy>
</target>
</project>
Notes:
- Use a modulus operation to distribute files contained in the ANT fileset, into different directories
- The "bootstrap" target is used to download and install the 3rd party task jar from Maven Central
来源:https://stackoverflow.com/questions/14224194/ant-copy-a-list-of-files-evenly-across-multiple-folders