ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Daniel Blaukopf <Daniel.Blauk...@Sun.COM>
Subject Re: Copying, filtering
Date Thu, 21 Sep 2006 17:37:53 GMT
Hi Kofa,


If you wanted to use vanilla Ant, here's one option:

    <target name="-OneDeploy-copy">
        <delete dir="${dest}"  />
        <copy todir="${dest}">
            <fileset dir="skeleton/A" excludes="${excludes}"
includes="${includes}"/>
            <fileset dir="skeleton/B" excludes="${excludes}"
includes="${includes}"/>
            <fileset dir="skeleton/C" excludes="${excludes}"
includes="${includes}"/>
            <filterset filtersfile="${filtersfile}" />
        </copy>
    </target>

    <target name="-OtherDeploy-copy">
        <delete dir="${dest}"/>
        <copy todir="${dest}">
            <fileset dir="skeleton/A" excludes="${excludes}"
includes="${includes}"/>
            <fileset dir="skeleton/B" excludes="${excludes}"
includes="${includes}"/>
            <fileset dir="skeleton/D" excludes="${excludes}"
includes="${includes}"/>
            <filterset filtersfile="${filtersfile}" />
        </copy>
    </target>

    <target name="OneDeploy">
        <antcall target="deploy">
            <param name="target" value="One"/>
        </antcall>
    </target>

    <target name="OtherDeploy">
        <antcall target="deploy">
            <param name="target" value="Other"/>
        </antcall>
    </target>

    <target name="deploy">
        <fail unless="target"/>
        <property name="binaryFiles"
value="**/*.class,**/*.jar,**/*.exe,**/*.dll,**/*.gif,**/*.jpg" />
        <antcall target="-${target}-copy">
            <param name="dest" location="${target}Dir"/>
            <param name="includes" value="**/*"/>
            <param name="excludes" value="${binaryFiles}"/>
            <param name="filtersfile" location="${target}Filter.properties"/>
        </antcall>
        <antcall target="-${target}-copy">
            <param name="dest" location="${target}Dir"/>
            <param name="includes" value="${binaryFiles}"/>
            <param name="excludes" value="**/*"/>
        </antcall>
    </target>

Here's another option that let's you put the variable information in files:


<project name="deploy" basedir=".">

    <target name="OneDeploy">
        <antcall target="deploy">
            <param name="target" value="One"/>
        </antcall>
    </target>

    <target name="OtherDeploy">
        <antcall target="deploy">
            <param name="target" value="Other"/>
        </antcall>
    </target>

    <target name="deploy">
        <fail unless="target"/>
        <!-- get a property with the form **/*.dll,**/*.gif -->
        <loadfile property="binaryIncludePattern" srcfile="binary-extensions.txt">
            <filterchain>
                <prefixlines prefix="**/*."/>
                <tokenfilter delimoutput=","/>
            </filterchain>
        </loadfile>
        <!-- get a property with the form \1/**/*.dll,\1/**/*.gif -->
        <loadfile property="binaryRegex" srcfile="binary-extensions.txt">
            <filterchain>
                <prefixlines prefix="\1/**/*."/>
                <tokenfilter delimoutput=","/>
            </filterchain>
        </loadfile>
        <!-- get a property with the form A/**/*,B/**/* -->
        <loadfile property="textfiles" srcfile="${target}.files">
            <filterchain>
                <replaceregex pattern="$$" replace="/**/*"/>
                <tokenfilter delimoutput=","/>
            </filterchain>
        </loadfile>
        <!-- get a property with the form
            A/**/*.dll,A/**/*.gif,B/**/*.dll,B/**/*.gif -->
        <loadfile property="binaries" srcfile="${target}.files">
            <filterchain>
                <replaceregex pattern="(.+)" replace="${binaryRegex}"/>
                <tokenfilter delimoutput=","/>
            </filterchain>
        </loadfile>
        <property name="regexp.removeTopDir"
                value="^[^\${file.separator}]+\${file.separator}(.+)$$"/>
        <delete dir="${target}Dir"/>
        <!-- copy non-binary files -->
        <copy todir="${target}Dir">
            <fileset dir="skeleton" includes="${textfiles}"
                    excludes="${binaryIncludePattern}"/>
            <mapper type="regexp" from="${regexp.removeTopDir}" to="\1"/>
            <filterset filtersfile="${target}Filter.properties"/>
        </copy>
        <!-- copy binary files -->
        <copy todir="${target}Dir">
            <fileset dir="skeleton" includes="${binaries}"/>
            <mapper type="regexp" from="${regexp.removeTopDir}" to="\1"/>
        </copy>
    </target>
</project>


Then have a file binary-extensions.txt containing the text:


class
exe
jar
dll
gif
jpg

and a file One.files containing

A
B
C

Other.files:

A
B
D

and then you've separated the logic from the data. Putting the data
in files also makes it a little easier to manipulate with Ant.

Notes:

1. The regular expression regexp.removeTopDir is to change paths in the
form A/B/C to B/C, removing the top level directory.
2. You'd probably want to add <trim/> and <ignoreblank/> to all the
filterchains.

Regards,

Daniel


Kovács István wrote:

> Hi,
>
> I need to copy files from multiple directories to a single deployment
> target directory. In the process, I need to replace a few strings. A
> filter embedded in the copy task allows me to do that, but it would
> corrupt the binary files, so I copy text files with the filter, binary
> files without it. I'll have several similar targets that copy files
> from different, sometimes overlapping sets of directories, and I'd
> like to eliminate as much duplication as possible.
> Here's what I currently do:
>
> <project name="myProjectr" default="compileAll" basedir=".">
>     <property name="binaryFiles"
> value="**/*.class,**/*.jar,**/*.exe,**/*.dll,**/*.gif,**/*.jpg" />
> [... stuff omitted ...]
>     <target name="OneDeploy">
>         <property name="dest" value="OneDir" />
>         <delete dir="${dest}"  />
>         <copy todir="${dest}">
>             <fileset dir="skeleton/A" excludes="${binaryFiles}" />
>             <fileset dir="skeleton/B" excludes="${binaryFiles}" />
>             <fileset dir="skeleton/C" excludes="${binaryFiles}" />
>             <filterset filtersfile="OneFilter.properties" />
>         </copy>
>         <copy todir="${dest}">
>             <fileset dir="skeleton/A" includes="${binaryFiles}" />
>             <fileset dir="skeleton/B" includes="${binaryFiles}" />
>             <fileset dir="skeleton/C" includes="${binaryFiles}" />
>         </copy>
>     </target>
>
>     <target name="OtherDeploy">
>         <property name="dest" value="OtherDir" />
>         <delete dir="${dest}"  />
>         <copy todir="${dest}">
>             <fileset dir="skeleton/A" excludes="${binaryFiles}" />
>             <fileset dir="skeleton/B" excludes="${binaryFiles}" />
>             <fileset dir="skeleton/D" excludes="${binaryFiles}" />
>             <filterset filtersfile="OtherFilter.properties" />
>         </copy>
>         <copy todir="${dest}">
>             <fileset dir="skeleton/A" includes="${binaryFiles}" />
>             <fileset dir="skeleton/B" includes="${binaryFiles}" />
>             <fileset dir="skeleton/D" includes="${binaryFiles}" />
>         </copy>
>     </target>
> </project>
>
> Q1: Is there a simpler way to define something like "all files from
> skeleton/A, B and C that are not binary" (avoiding the need to repeat
> the excludes attribute for each)?
>
> Q2: Since the copying is identical (except for the name and number of
> source directories involved and the destination directory), it'd be
> nice to define the 'dest' property depending on the target, and define
> the source directories there as well. I've thought of defining a
> doCopy target, setting the destination directory as a property and
> invoking it with antCall, but how do I take care of the sources?
>
> TIA,
> Kofa
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Mime
View raw message