开发者

Ant task that processes multiple files

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 las开发者_C百科t resort it is of course possible to exec java externally, maybee that is the preferred way.


The answer is ant-contrib.

Specifically foreach task


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜