Can I make the Ant copy task OS-specific?

故事扮演 提交于 2019-12-03 15:14:32

问题


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:


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.




回答2:


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>



回答3:


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>




回答4:


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.




回答5:


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.




回答6:


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.



来源:https://stackoverflow.com/questions/86526/can-i-make-the-ant-copy-task-os-specific

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!