如何为Apache Ant目录中的每个文件运行java任务?看起来<apply>
只允许运行可执行文件
在可执行java.exe中使用apply:
<apply executable="path/to/java.exe">
<arg value="..."/>
<arg value="..."/>
...
<fileset dir="..."/>
...
</apply>
或者使用一些提供for循环的AntAddon,例如Flaka,参见Wiki examples/Files + Directories:
问题:在编译我的java源代码后如何运行相应的类?
解决方案:遍历包含java源代码的文件集,并使用replace函数调用相应的类文件。
<project xmlns:fl="antlib:it.haefelinger.flaka">
<property name="srcroot" value="path/to/srcrootdir"/>
<property name="classroot" value="path/to/classrootdir"/>
<!-- seek all classes with main method -->
<fileset dir="${srcroot}" includes="**/*.java" id="mainclasses">
<contains text="public static void main"/>
</fileset>
<!-- iterate over classes with main method and call
corresponding classfile -->
<fl:for var="file" in="split('${toString:mainclasses}', ';')">
<fl:let>
; strip the '.java' extension
file = replace(file, '', '.java')
; replace fileseparator with '.'
; when running on windows you have to use :
; replace(file, '.', '${file.separator}${file.separator}')
file = replace(file, '.', '${file.separator}')
</fl:let>
<fl:echo>
starting => #{file} in ${classroot}..
</fl:echo>
<java classname="#{file}">
<classpath>
<!--
when using a fileset you'll get a
java.util.zip.ZipException because you're
referencing not jarfiles but classfiles
therefore you've to use pathelement location
-->
<pathelement location="${classroot}"/>
</classpath>
</java>
</fl:for>
</project>
从这里使用<java>
任务。但它不能应用于文件集。示例