tags:

views:

4323

answers:

6

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?

+1  A: 

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.

sblundy
+1  A: 

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>

jsight
+1  A: 

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.

Kevin Conner
+9  A: 

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.

benzado
Isn't there a way to declare a path in Ant that is treated equally under Windows and Linux? I'm just curious.
ivan_ivanovich_ivanoff
+4  A: 

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>
Mads Hansen
A: 

Ant-contrib has the <osfamily /> task. This will expose the family of the os to a property (that you specify the name of). This could be of some benefit.

Glenn2041