what are equivalent of Ant <resources>, <patternset> in gradle?

325 Views Asked by At

I am migrating ant based project to gradle project. I have to use a set of files or pattern in many tasks which makes me write duplicate code while it can be simply kept in resources and patternset tags in ant. Is there any simpler way like equivalent to resources or patternset in gradle.

<patternset id="pattern.files.to.instrument">
    <include name="lib/cat*.jar"/>
    <include name="lib/extensions/cat*.jar"/>
    <include name="lib/cas*.jar"/>
    <include name="bld/internal-lib/cat*.jar"/>
    <exclude name="**/*test.jar"/>
    <exclude name="**/*.war"/>
</patternset>

<fileset id="fileset.instrument" dir="${uninstrumented.jars.folder}">
    <patternset refid="pattern.files.to.instrument"/>
</fileset>
1

There are 1 best solutions below

1
On BEST ANSWER

I'm not familiar with Ant, but my first thought on a similar concept in Gradle is a FileTree (or a ConfigurableFileTree). You may configure a FileTree once and (since it implements FileCollection) refer to it everywhere where you want to use the files matching the patterns you specified:

def filesToInstrument = fileTree(projectDir) {
    include 'lib/cat*.jar'
    include 'lib/extensions/cat*.jar'
    include 'lib/cas*.jar'
    include 'bld/internal-lib/cat*.jar'
    exclude '**/*test.jar'
    exclude '**/*.war'
}

task copyFilesToInstrument(type: Copy) {
    from filesToInstrument
    into 'my/destination/path'
}

However, as far as I know, FileTree elements are always bound to a root directory, so if you just want to reuse include/exclude patterns, you may take a look at CopySpec. Elements of type CopySpec may be reused using the method with on tasks that implement CopySpec theirselves (e.g. Copy, Zip, Jar ...):

def filesToInstrumentPattern = copySpec {
    include 'lib/cat*.jar'
    include 'lib/extensions/cat*.jar'
    include 'lib/cas*.jar'
    include 'bld/internal-lib/cat*.jar'
    exclude '**/*test.jar'
    exclude '**/*.war'
}

task zipFilesToInstrument(type: Zip) {
    from fileTree('src')
    with filesToInstrumentPattern
}