如何为目录中的每个文件运行java任务?

如何为目录中的每个文件运行java任务?,java,ant,directory,Java,Ant,Directory,如何使用ApacheAnt为目录中的每个文件运行java任务?看起来只允许运行可执行文件。或者像这样将apply与可执行文件java.exe一起使用: <apply executable="path/to/java.exe"> <arg value="..."/> <arg value="..."/> ... <fileset dir="..."/> ... </apply> ,请参阅: 问题:编译java源代

如何使用ApacheAnt为目录中的每个文件运行java任务?看起来
只允许运行可执行文件。

或者像这样将apply与可执行文件java.exe一起使用:

<apply executable="path/to/java.exe">
  <arg value="..."/>
  <arg value="..."/>
   ...
  <fileset dir="..."/>
   ...
</apply>
,请参阅:
问题:编译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”扩展名
file=replace(文件,,.java)
; 将fileseparator替换为“.”
; 在windows上运行时,您必须使用:
; 替换(文件“\”,“${file.separator}${file.separator}”)
file=replace(文件“\.”,“${file.separator}”)
在${classroot}中启动=>{file}。。
使用来自的任务。但它不能应用于文件集。