我认为本机 Ant 中没有任何东西可以做到这一点,但这个示例是 Groovy 的GPars支持的“数据流并发”模型的绝对理想用例,因此您可以使用脚本任务执行某些操作:
<project name="gpars-test">
<path id="groovy.path">
<pathelement location="groovy-all-1.8.8.jar" />
<pathelement location="gpars-0.12.jar" />
</path>
<!-- Target to build one file - expects a property "filetobuild"
containing the name of the file to build -->
<target name="buildOneFile">
<echo>Imagine I just built ${filetobuild}...</echo>
</target>
<target name="main">
<script language="groovy" classpathref="groovy.path"><![CDATA[
import static groovyx.gpars.dataflow.Dataflow.task
import groovyx.gpars.dataflow.Dataflows
// load dependencies
def deps = new Properties()
new File(basedir, 'dependencies.properties').withInputStream {
deps.load(it)
}
def df = new Dataflows()
// spawn one "task" per file to be compiled
deps.each { file, dependencies ->
task {
if(dependencies) {
// wait for dependencies - reading df.something will suspend
// this task until another task has written the same variable
dependencies.split(/ /).each { dep ->
def dummy = df."${dep}"
}
}
// now we know all our dependencies are done, call out to build
// this one.
def compiler = project.createTask('antcall')
compiler.target = "buildOneFile"
def prop = compiler.createParam()
prop.name = "filetobuild"
prop.value = file
try {
compiler.perform()
} catch(Exception e) {
// do something
} finally {
// we're done - this will release any other tasks that are blocked
// depending on us
df."${file}" = "done"
}
}
}
// now wait for all tasks to complete
deps.each { file, dependencies ->
def dummy = df."${file}"
}
println "finished"
]]></script>
</target>
</project>
请注意,这确实假设您要构建的所有文件都列在依赖项文件中,包括那些没有依赖项的文件,即
fileA=fileB fileC
fileB=fileC
fileC=
如果 no-deps 文件名未在属性中列出,那么您将不得不做更多的摆弄才能将它们放在那里 - 就在def df = new Dataflows()
您需要添加一些内容之前
deps.values().collect().each { depString ->
depString.split(/ /).each {
// if we find a fileX that something depends on but which does not
// itself appear in the deps map, assume it has no dependencies
if(!deps.containsKey(it)) {
deps.setProperty(it, "")
}
}
}