Marcus Junius Brutus
Marcus Junius Brutus

Reputation: 27286

how to reuse the definition of a number of filesets?

I have a number of tasks in my build.xml that all utilize the same set of filesets. E.g. something like the following (I've retain the actual names of the tasks - related to the cobertura coverage tool - but the nature of the enveloping task is immaterial to this question).

<target name="coverage-report">
    <cobertura:cobertura-report destdir="${coverage.xml.dir}" format="xml">
        <fileset dir="${src.dir}">
            <include name="**/*.java" />
        </fileset>
        <fileset dir="${componentFoo.dir}/src">
            <include name="**/*.java" />
        </fileset>
    </cobertura:cobertura-report>
</target>

<target name="summary-coverage-report">
    <cobertura:cobertura-report destdir="${coverage.summaryxml.dir}" format="summaryXml">
        <fileset dir="${src.dir}">
            <include name="**/*.java" />
        </fileset>
        <fileset dir="${componentFoo.dir}/src">
            <include name="**/*.java" />
        </fileset>
    </cobertura:cobertura-report>
</target>

<target name="alternate-coverage-report">
    <cobertura:cobertura-report destdir="${coverage.html.dir}">
        <fileset dir="${src.dir}">
            <include name="**/*.java"/>
        </fileset>
        <fileset dir="${componentFoo.dir}/src">
            <include name="**/*.java" />
        </fileset>
    </cobertura:cobertura-report>
</target>

What I would like to do is to be able to define once this recurring set of filesets and reuse it. Now, there's a related SO question on how to use the same fileset in multiple places but that's not working here as I have a number of filesets, not just one. I also tried wrapping the filesets inside a macrodef and expanding the macrodef when needed but I get the message that the these tasks "don't support the nested [name of macrodef] element". So I guess macrodefs in Ant can only be used as high-level tasks and cannot be expanded in arbitrary places.

So is there a way to reuse a definition of a number of filesets in Ant?

Upvotes: 0

Views: 252

Answers (1)

Ian Roberts
Ian Roberts

Reputation: 122364

This would be easy if the cobertura tasks accepted arbitrary resource collections instead of being hard-coded to just fileset - it may be worth submitting a feature request to this end. For example with a copy task:

<resources id="filesets">
    <fileset dir="${src.dir}">
        <include name="**/*.java" />
    </fileset>
    <fileset dir="${componentFoo.dir}/src">
        <include name="**/*.java" />
    </fileset>
</resources>


<copy todir="dest">
    <resources refid="filesets"/>
</copy>

You can fall back to the purely XML-level and use an entity reference:

<!DOCTYPE project [
  <!ENTITY cobFilesets '<fileset dir="${src.dir}">
        <include name="**/*.java" />
    </fileset>
    <fileset dir="${componentFoo.dir}/src">
        <include name="**/*.java" />
    </fileset>'>
]>
<project ....>
    <target name="coverage-report">
        <cobertura:cobertura-report destdir="${coverage.xml.dir}" format="xml">
          &cobFilesets;
        </cobertura:cobertura-report>
    </target>
</project>

Upvotes: 2

Related Questions