如何使用Apache Ant为目录中的每个文件运行java任务?看起来<apply>
只允许运行可执行文件。
答案 0 :(得分:0)
使用apply和可执行的java.exe如下:
<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>
答案 1 :(得分:0)