Ant Copy任務

此任務用於將檔或資源複製到新位置,它僅在原始檔案比目標檔更新時才複製。也可以使用overwrite屬性顯式覆蓋它。

todir屬性用於設置目標路徑。 此任務使用下麵給出的各種屬性。

1. Apache Ant Copy任務屬性

屬性 描述 必需
file 要複製的檔
preservelastmodified 保留上次修改的名稱
tofile 要複製到目標檔的檔 如果還指定了file屬性,則只允許使用todir
todir 目標目錄名稱 如果還指定了file屬性,則只允許使用todir
overwrite 即使目標檔較新,也會覆蓋現有檔
force 覆蓋只讀目標檔
filtering 在複製過程中過濾
flatten 通過忽略原始檔案的目錄結構來複製檔
includeEmptyDirs 複製空目錄
failonerror 如果複製失敗,則顯示此錯誤消息。
quiet 如果為truefailonerrorfalse,則不記錄警告消息。
verbose 它記錄正在複製的檔
encoding 用於複製檔的編碼
outputencoding 顯示要使用的編碼

2. Apache Ant複製任務示例

下麵來看一個例子,在這個示例中將數據從一個檔複製到另一個檔。 請參閱下麵的示例。

複製單個檔

檔:build.xml

<project name = "java-ant project" default = "copy-file">
    <target name="copy-file">
        <copy file = "abc.txt" tofile = "xyz.txt"></copy>
    </target>
</project>

將檔複製到目錄

<project name = "java-ant project" default = "copy-file">
    <target name="copy-file">
        <copy file="abc.txt" todir="../someother/dir"/>
    </target>
</project>

將目錄複製到另一個目錄

<project name = "java-ant project" default = "copy-file">
    <target name="copy-file">
        <copy todir="../new/dir"><fileset dir="src_dir"/></copy>
    </target>
</project>

上一篇: Ant Condition任務 下一篇: Ant CVS任務