Noel Yap
Noel Yap

Reputation: 19768

How to set Ant reference to a FileSet from within <groovy/> Ant task?

I have something like:

<groovy>
    import org.apache.tools.ant.types.FileSet
    import org.apache.tools.ant.types.selectors.FilenameSelector

    def aoeu = new FileSet()
    aoeu.setDir(new File('aoeu'))

    def snth = new new FilenameSelector()
    snth.setName('snth')

    aoeu.add(snth)

    project.references['aoeu'] = aoeu
</groovy>

But:

<echo message="${toString:aoeu}"/>

emits a NullPointerException.

How can the above be fixed?

Upvotes: 1

Views: 1116

Answers (1)

Mark O&#39;Connor
Mark O&#39;Connor

Reputation: 77961

The groovy task has preconfigured bindings to an instance of AntBuilder and the Ant "project" object. It makes groovy very attractive for creating programmed logic within the build.

The following example demonstrates how the fileset can be created within the groovy script:

<project name="demo" default="process-text-files">

    <path id="build.path">
        <pathelement location="/path/to/jars/groovy-all-2.1.1.jar"/>
    </path>

    <target name="process-text-files">
        <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>
        <groovy>
            ant.fileset(id:"textfiles",dir:"src", includes:"**/*.txt")

            project.references.textfiles.each {
                println it
            }
        </groovy>
    </target>

</project>

The following example demonstrates referencing a normal fileset created externally to the script (my preferred way to do it):

Upvotes: 1

Related Questions