4

For example

/test/a.jar
/test/b.jar
/test/c.jar

output:
a.jar
b.jar
c.jar

This works for the most part, except it only seems to be getting one of the files, not all =/

       <for param="file">
            <path>
                <fileset dir="${test.dir}/lib">
                    <include name="**/*.jar"/>
                </fileset>
            </path>
            <sequential>
                <basename property="filename" file="@{file}"/>
                <echo message="${filename}"/>
            </sequential>
        </for>

This just gets me:

c.jar
c.jar
c.jar

4 Answers 4

7

The problem you've hit is the immutability of Ant properties - once a property is set the value cannot normally be changed.

The first time around the loop the filename property is being set, and that value 'sticks'.

Since Ant 1.8 the local task allows you to localise a property to the current execution block. For example, your sequential would be:

<sequential>
    <local name="filename" />
    <basename property="filename" file="@{file}"/>
    <echo message="${filename}"/>
</sequential>

Ant forgets the property at the end of the sequential, so a new value can be used in each iteration.

3

It's been a while since I've done anything with ant, but have you tried a foreach?

   <foreach>
        <fileset dir="${test.dir}/lib">
            <include name="**/*.jar"/>
        </fileset>
        <echo message="${foreach.file}"/>
   </foreach>
3
  • foreach doesn't allow nested echo :( I want to output the filenames to a file, so pretty much need to echo out to a file. I can't store them in a list because the number of files can change.
    – Th3sandm4n
    Dec 16, 2011 at 17:07
  • hmm.. check out this answer, maybe it will help: stackoverflow.com/questions/1467991/… Dec 16, 2011 at 17:12
  • Yeah, I've been reading through, I just ended up doing an exec doing ls > out.txt :)
    – Th3sandm4n
    Dec 16, 2011 at 17:25
0

Here's how I list files in a directory.

<fileset id="filesref" dir="path/to/dir" includes="*"/>
<property name="files" refid="filesref"/>

<echo message="Files:${line.separator}" />
<for list="${files}" delimiter=";" param="file" >
    <sequential>
        <echo message="    @{file}"/>
        <!--Do more stuff with the file's name here-->
    </sequential>
</for>
0

To get list of file Names along with their sizes.

E:\ANT>ant -buildfile=nashornFileNames.xml
Buildfile: E:\ANT\nashornFileNames.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<project name="MyProject" basedir="." default="fileNames">

    <property environment="env"/>
    <property name="library.file" value="E:\\ANT\\JAR"/>


    <condition property="fileSep" value="\"><os family="windows"/></condition>
    <condition property="fileSep" value="/"><os family="unix"/></condition>

    <target name="fileNames" depends="showEnv">
        <!-- script run both on rhino and nashorn -->
        <script language="javascript"> <![CDATA[
            // "nashorn:" pseudo URL scheme for nashorn's built-in scripts.
            // To support Rhino load Mozilla compatibility script - which defines global functions like importPackage, importClass for rhino compatibility.
            load("nashorn:mozilla_compat.js");

            // Accessing Java packages and classes from script. Nashorn's recommended way to access Java classes is to use Java.type.
            importPackage(java.io) // Rhino extensions: importPackage global function
            importClass(java.lang.System); // Rhino extensions:  global.importClass

            var File = java.io.File; // Rhino way!
            var String = Java.type("java.lang.String"); // Nashorn way!

            // Access to Ant-Properties by their names
            basedir  = self.getProject().getProperty("basedir");
            fileSep  = project.getProperty("fileSep");
            System.out.println(" [script command-line] Java API : " + fileSep);

            // create and use a Task via Ant API
            echo = self.getProject().createTask("echo");
            function setMessage( msg ) {
                echo.setMessage( msg );
                echo.perform();
            }
            setMessage("[echo Task] Ant API : " + basedir );

                // <fileset dir="" includes=""/> « https://ant.apache.org/manual/Types/fileset.html
                var fileset = project.createDataType("fileset");

                files  = project.getProperty("library.file");
                setMessage("files: " + files);
                fileset.setDir( new File( files ) );
                fileset.setIncludes("**/*.jar");

            // Get the files (array) of that fileset
            ds = fileset.getDirectoryScanner( self.getProject() );

            fileset_files = ds.getIncludedFiles(); // Get only the files

            // iterate over that array
            for (i=0; i < fileset_files.length; i++) {

                var basedir  = fileset.getDir( self.getProject() );
                var filePath = fileset_files[i];
                var folders = filePath.split(fileSep).slice(0,-1).join(fileSep);
                var fileName = filePath.split(fileSep).slice(-1)[0];
                var file = new File(basedir, filePath);
                var fileSize = file.length();

                setMessage(folders + "« FileName : "+ fileName +" : " + fileSize + " byte");
            }
        ]]></script>
    </target>
    <target name="showEnv">
        <echo message="java.io.tmpdir = ${java.io.tmpdir}"/>
        <echo message="windows tmpdir = ${env.TMP}"/>
        <echo message="windows JAVA_HOME = ${env.JAVA_HOME}"/>
        <echo message="windows ANT_HOME = ${env.ANT_HOME}"/>
    </target>
</project>

Oracle Nashorn as a command-line tool and as an embedded interpreter in Java applicationswiki

Nashorn is a JavaScriptECMA-compliant engine developed in the Java programming language by Oracle. It is based on the Da Vinci Machine (JSR 292) and has been released with Java 8. _ jdk.nashorn.api.scripting.NashornScriptEngine openjdk

Until Java SE 7, JDKs shipped with a JavaScript scripting engine based on Mozilla Rhino. Java SE 8 will instead ship with a new engine called Oracle Nashorn, which is based on JSR 292 and invokedynamic. It provides better compliance with the ECMA normalized JavaScript specification and better runtime performance through invokedynamic-bound call sites.

Nashorn extensions and some of Rhino specific extension which are already supported by nashorn. Ex:

var String = Java.type("java.lang.String"); // Nashorn way!
var Array = java.lang.reflect.Array // Rhino way!

// collections are interpreted as arrays.
var arr = [ "hello", "world" ];
var list = Java.to(array, Java.type("java.util.List"))

There are few Rhino/Mozilla extensions that are supported only if you load the compatibility script provided by nashorn. The compatibility script is loaded using "load('nashorn:mozilla_compat.js")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.