Andy Nuss
Andy Nuss

Reputation: 745

ant target that iterates all files in the build path with a given extension

I have a simple syntax for script files of a given extension that translates to java class files that I want to create, each containing a given function that can be invoked by reflection.

I want to write a main function that I can invoke from the ant script that can see the ant build directory, traverse it for all the files that have my extension, and translate each one to a java file that I put not in the bin directory but in the src directory package that holds the file (a script) with my extension.

Is this possible?

Andy

Upvotes: 0

Views: 488

Answers (2)

ewan.chalmers
ewan.chalmers

Reputation: 16235

Here is an example of how you could create a .java file in the src directory for every .ext file in the bin directory.

But for code generation, I expect that you would need to implement a custom Ant task.

<project default="test">

    <target name="test">
        <copy todir="src">
            <fileset dir="bin">
                <include name="**/*.ext"/>
            </fileset>
            <globmapper from="*.ext" to="*.java"/>
            <filterchain>
                <!--
                    You will do some custom filtering
                    to create your new Java src file.
                    This is just for illustration.
                 -->
                <headfilter lines="1"/>
                <tokenfilter>
                    <replaceregex pattern=".*" replace="your new content"/>
                </tokenfilter>
            </filterchain>
        </copy>
    </target>

    <!-- use with care! -->
    <target name="clean">
        <delete dir="src">
            <include name="**/*.java"/>
        </delete>
    </target>

</project>

Upvotes: 1

Matt
Matt

Reputation: 11805

Try the ant-contrib project. It has a task which takes a fileset. You could then do whatever you want with it:

<ac:for param="file">
  <fileset ... />
  <sequential>
    <echo message="@{file}" />
  </sequential>
</ac:for>

Caveat: I'm the project owner of this. It does not yet take ant 1.7/1.8 constructs (ie. resource collections), but that effort is underway.

Upvotes: 0

Related Questions