Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ant: Conditional Copy

I want to overwrite the hosts file on the Windows machine if the user allows it:

<input message="Do you want to overwrite the HOSTS file?"
       addproperty="overwrite.hosts" validargs="yes,no" />

<copy tofile="${env.WINDIR}/system32/drivers/etc/hosts.backup">
    <fileset file="${env.WINDIR}/system32/drivers/etc/hosts" />
</copy>

<copy todir="${env.WINDIR}/system32/drivers/etc">
    <fileset file="${trainer.dir}/hosts" />
</copy>

How do I do the copies only if the user says yes?

EDIT:

I tried this:

    <input message="Do you want to overwrite the HOSTS file?" addproperty="overwrite.hosts" validargs="yes,no" />

    <if>
        <equals arg1="${overwrite.hosts}" arg2="yes" />
        <then>
            <copy tofile="${env.windir}/system32/drivers/etc/hosts.backup">
                <fileset file="${env.windir}/system32/drivers/etc/hosts">
                </fileset>
            </copy>

            <copy todir="${env.windir}/system32/drivers/etc">
                <fileset file="${trainer.dir}/hosts">
                </fileset>
            </copy>
        </then>
    </if>

and I get this output:

C:\trainer\build.xml:16: Problem: failed to create task or type if
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.

I'm an ant rookie... What do I need to do?

like image 975
mainstringargs Avatar asked Jan 27 '11 17:01

mainstringargs


People also ask

What is FileSet in Ant?

A FileSet is a group of files. These files can be found in a directory tree starting in a base directory and are matched by patterns taken from a number of PatternSets and Selectors. PatternSets can be specified as nested <patternset> elements.

What is PathElement in ant?

This object represents a path as used by CLASSPATH or PATH environment variable. A path might also be described as a collection of unique filesystem resources. and PathElement: Helper class, holds the nested <pathelement> values.

How do I use Ant properties file?

It allows you to reuse the same build file, with different property settings for different execution environment. For example, build properties file can be maintained separately for DEV, TEST, and PROD environments. It is useful, when you do not know the values for a property (in a particular environment) up-front.


1 Answers

You could use a condition or a if task for that. (The latter is part of the ant-contrib project.)

like image 152
miku Avatar answered Sep 23 '22 11:09

miku