Ant task that processes multiple files

拈花ヽ惹草 提交于 2019-12-24 11:00:48

问题


in Ant I want to execute a Java task on a fileset. I use the Java task to run rhino which runs a JS beautifier. The later works without any problems, except that it might seem little bit awkward to use rhino+js to acutally have a working JS beautifier/indenter. But all working JS beautifiers I have found are all written in JavaScript.

I also want to run jsmin on the files as well, but as part of the distribution task. So the same problem will appear again, e.g running a java task on multiple files. The java/JS implementation of jsmin is preferred since it works on all platforms that run ant.

I cannot find a generic "foreach" task in ant that processes a fileset or similar structure. I have googled ant found an add-on package that provides "foreach" but since its not part of the standard ant core tasks I get the feeling that I'am on the wrong track.

The exec task can iterate over a fileset, but I want to run rhino inside the JVM or at least on a forked JVM. But as a last resort it is of course possible to exec java externally, maybee that is the preferred way.


回答1:


The answer is ant-contrib.

Specifically foreach task




回答2:


From my answer at https://stackoverflow.com/a/9715078/438319 Here is way to do this using javascript and the ant scriptdef task, you don't need ant-contrib for this code to work since scriptdef is a core ant task.

<scriptdef name="bzip2-files" language="javascript">
<element name="fileset" type="fileset"/>
<![CDATA[
  importClass(java.io.File);
  filesets = elements.get("fileset");

  for (i = 0; i < filesets.size(); ++i) {
    fileset = filesets.get(i);
    scanner = fileset.getDirectoryScanner(project);
    scanner.scan();
    files = scanner.getIncludedFiles();
    for( j=0; j < files.length; j++) {

        var basedir  = fileset.getDir(project);
        var filename = files[j];
        var src = new File(basedir, filename);
        var dest= new File(basedir, filename + ".bz2");

        bzip2 = self.project.createTask("bzip2");        
        bzip2.setSrc( src);
        bzip2.setDestfile(dest ); 
        bzip2.execute();
    }
  }
]]>
</scriptdef>

<bzip2-files>
    <fileset id="test" dir="upstream/classpath/jars/development">
            <include name="**/*.jar" />
    </fileset>
</bzip2-files>


来源:https://stackoverflow.com/questions/1947437/ant-task-that-processes-multiple-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!