groovy inside ant: how to access refids from grooy that are defined by ant tags

StackOverflow https://stackoverflow.com/questions/225063

  •  03-07-2019
  •  | 
  •  

Question

I'm using a groovy code snippet in an ant build file. Inside the groovy code I'm trying to reference a fileset that has been defined outside of the groovy part, like this:

<target name="listSourceFiles" >
    <fileset id="myfileset" dir="${my.dir}">
         <patternset refid="mypatterns"/>
    </fileset>
    <groovy>
        def ant = new AntBuilder()

        scanner = ant.fileScanner {
            fileset(refid:"myfileset")
        }

    ...
    </groovy>
</target>

When I execute this I get the following error message:

Buildfile: build.xml

listSourceFiles:   
   [groovy]

BUILD FAILED
d:\workspace\Project\ant\build.xml:13:
Reference myfileset not found.

What am I missing?

Was it helpful?

Solution

According to the Groovy Ant Task documentation, one of the bindings for the groovy task is the current AntBuilder, ant.

So modifying your script to drop the clashing 'ant' def I got it to run with no errors:

<project name="groovy-build" default="listSourceFiles">

<taskdef name="groovy"
         classname="org.codehaus.groovy.ant.Groovy"/>

<patternset id="mypatterns">
  <include name="../*.groovy"/>
</patternset>
<target name="listSourceFiles" >
    <fileset id="myfileset" dir="${my.dir}">
         <patternset refid="mypatterns"/>
    </fileset>
    <groovy>
        scanner = ant.fileScanner {
            fileset(refid:"myfileset")
        }
    </groovy>
</target>
</project>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top