Can I make the Ant copy task OS-specific?

后端 未结 6 1693
醉梦人生
醉梦人生 2021-02-07 05:03

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 \'exe

相关标签:
6条回答
  • 2021-02-07 05:19

    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>
    
    0 讨论(0)
  • 2021-02-07 05:21

    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.

    0 讨论(0)
  • 2021-02-07 05:22

    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.

    0 讨论(0)
  • 2021-02-07 05:28

    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.

    0 讨论(0)
  • 2021-02-07 05:36

    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.

    0 讨论(0)
  • 2021-02-07 05:37

    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>
    

    0 讨论(0)
提交回复
热议问题