此任務用於將檔案或資源複製到新位置,它僅在原始檔比目標檔案更新時才複製。也可以使用overwrite
屬性顯式覆蓋它。
todir
屬性用於設定目標路徑。 此任務使用下面給出的各種屬性。
屬性 | 描述 | 必需 |
---|---|---|
file |
要複製的檔案 | 是 |
preservelastmodified |
保留上次修改的名稱 | 否 |
tofile |
要複製到目標檔案的檔案 | 如果還指定了file 屬性,則只允許使用todir 。 |
todir |
目標目錄名稱 | 如果還指定了file 屬性,則只允許使用todir 。 |
overwrite |
即使目標檔案較新,也會覆蓋現有檔案 | 否 |
force |
覆蓋唯讀目標檔案 | 否 |
filtering |
在複製過程中過濾 | 否 |
flatten |
通過忽略原始檔的目錄結構來複製檔案 | 否 |
includeEmptyDirs |
複製空目錄 | 否 |
failonerror |
如果複製失敗,則顯示此錯誤訊息。 | 否 |
quiet |
如果為true 且failonerror 為false ,則不記錄警告訊息。 |
否 |
verbose |
它記錄正在複製的檔案 | 否 |
encoding |
用於複製檔案的編碼 | 否 |
outputencoding |
顯示要使用的編碼 | 否 |
下面來看一個例子,在這個範例中將資料從一個檔案複製到另一個檔案。 請參閱下面的範例。
複製單個檔案
檔案: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>