最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Ant script with embedded javascript trying to read files - Stack Overflow

programmeradmin3浏览0评论

I have the following ant build target :

<target name="analyze">
    <script language="javascript">
    <![CDATA[
            importPackage(java.lang);
            var path = project.getProperty("PROJECT_HOME") + "/oms";
            System.out.println("path = " +path);                
        ]]>
    ]]>
    </script>
</target>

I'd like to find all files in the directory recursively and if they end in .java print them out. Is this possible?

I have the following ant build target :

<target name="analyze">
    <script language="javascript">
    <![CDATA[
            importPackage(java.lang);
            var path = project.getProperty("PROJECT_HOME") + "/oms";
            System.out.println("path = " +path);                
        ]]>
    ]]>
    </script>
</target>

I'd like to find all files in the directory recursively and if they end in .java print them out. Is this possible?

Share Improve this question asked Jun 30, 2011 at 0:39 Amir AfghaniAmir Afghani 38.5k17 gold badges86 silver badges126 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 10

There is an example in the Ant script task docs that basically does this. Here's a simplified version:

<script language="javascript">
<![CDATA[
    importClass(java.io.File);

    fs = project.createDataType("fileset");
    dir = "src";
    fs.setDir( new File( dir ) );
    fs.setIncludes( "**/*.java" );

    // create echo Task via Ant API
    echo = project.createTask("echo");

    // iterate over files found.
    srcFiles = fs.getDirectoryScanner( project ).getIncludedFiles( );
    for ( i = 0; i < srcFiles.length; i++ ) {
        var filename = srcFiles[i];

        // use echo Task via Ant API
        echo.setMessage( filename );
        echo.perform( );
    }]]>
</script>

This uses an Ant FileSet to find the files. Here an includes rule is set on the fileset so that only .java files found are returned by the iterator - saves using string operations on the filenames to discard any other files.

If you need to set exclusion rules you can do so by means of the setExcludes() method of the FileSet class (actually of the AbstractFileset class). See the docs for patterns to understand a little more about Ant wildcards.

You could also do this with core Ant tasks, without using javascript:

  <fileset dir="java" id="java.files.ref">
    <include name="**/*.java"/>
  </fileset>
  <pathconvert pathsep="${line.separator}" property="java.files" refid="java.files.ref"/>
  <echo>${java.files}</echo>
发布评论

评论列表(0)

  1. 暂无评论