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>