Preserve Original Filenames in Groovy Ant Task

2019-09-11 14:19发布

问题:

I have the following code:

new AntBuilder().zip( destFile: "${file}.zip" ) {
        fileset( dir: srcDir ) {
            include( name:pattern )
        }
    }

In this example I'd like ant to create a zip with the same name as the original file, but with a .zip added to the end. Is there a way to do this without knowing the original file's name ahead of time in ant? I'd like to be able to do the same thing with other ant tasks as well.

To put it another way, I'd like the filename to become whatever "pattern" resolves to for each file.

回答1:

Something like this?

<target name="zip-files">
    <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>

    <dirset id="dirsToZip" dir="src">
        <include name="dir*"/>
    </dirset>

    <groovy>
        project.references.dirsToZip.each { 
            ant.zip(destfile: "${it}.zip", basedir: it)
        }
    </groovy>
</target>

If find the groovy task's ability to iterate thru a fileset or dirset a very useful feature.



标签: ant groovy