Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I make the Ant copy task OS-specific?

Tags:

copy

ant

I have an Ant script that performs a copy operation using the 'copy' task. It was written for Windows, and has a hardcoded C:\ path as the 'todir' argument. I see the 'exec' task has an OS argument, is there a similar way to branch a copy based on OS?

like image 684
timbonicus Avatar asked Sep 17 '08 19:09

timbonicus


5 Answers

I would recommend putting the path in a property, then setting the property conditionally based on the current OS.

<condition property="foo.path" value="C:\Foo\Dir">
   <os family="windows"/>
</condition>
<condition property="foo.path" value="/home/foo/dir">
   <os family="unix"/>
</condition>

<fail unless="foo.path">No foo.path set for this OS!</fail>

As a side benefit, once it is in a property you can override it without editing the Ant script.

like image 192
benzado Avatar answered Nov 05 '22 22:11

benzado


The previously posted suggestions of an OS specific variable will work, but many times you can simply omit the "C:" prefix and use forward slashes (Unix style) file paths and it will work on both Windows and Unix systems.

So, if you want to copy files to "C:/tmp" on Windows and "/tmp" on Unix, you could use something like:

<copy todir="/tmp" overwrite="true" >
         <fileset dir="${lib.dir}">
             <include name="*.jar" />
         </fileset>
</copy>

If you do want/need to set a conditional path based on OS, it can be simplified as:

    <condition property="root.drive" value="C:/" else="/">
        <os family="windows" />
    </condition>
    <copy todir="${root.drive}tmp" overwrite="true" >
             <fileset dir="${lib.dir}">
                 <include name="*.jar" />
             </fileset>
    </copy>
like image 38
Mads Hansen Avatar answered Nov 05 '22 21:11

Mads Hansen


You could use the condition task to branch to different copy tasks... from the ant manual:

<condition property="isMacOsButNotMacOsX">
<and>
  <os family="mac"/>

  <not>
    <os family="unix"/>

  </not>
</and>

like image 35
jsight Avatar answered Nov 05 '22 22:11

jsight


Declare a variable that is the root folder of your operation. Prefix your folders with that variable, including in the copy task.

Set the variable based on the OS using a conditional, or pass it as an argument to the Ant script.

like image 27
easeout Avatar answered Nov 05 '22 22:11

easeout


You can't use a variable and assign it depending on the type? You could put it in a build.properties file. Or you could assign it using a condition.

like image 1
sblundy Avatar answered Nov 05 '22 20:11

sblundy