Apache Ant Tasks 简明教程

Apache Ant Tasks - Copy

Description

Copy 任务将文件/资源集合复制到新文件或目录中。仅当源文件比目标文件新或目标文件不存在时,才复制文件。

Copy task copies a file/resource collection to a new file or directory. Files are only copied if the source file is newer than the destination file, or when the destination file does not exist.

Properties

Sr.No

Attributes & Description

1

File The file to copy. (Mandatory)

2

Preservelastmodified Give the copied files the same last modified time as the original source files.

3

Tofile The file to copy to.

4

Todir The directory to copy to.

5

Overwrite Overwrite existing files even if the destination files are newer.

6

Force Overwrite read-only destination files.

7

Filtering Indicates whether token filtering using the global build-file filters should take place during the copy.

8

Flatten Ignore the directory structure of the source files, and copy all files into the directory specified by the todir attribute.

9

includeEmptyDirs Copy any empty directories included in the FileSet(s).

10

Failonerror If false, log a warning message, but do not stop the build, when the file to copy does not exist or one of the nested filesets points to a directory that doesn’t exist or an error occurs while copying.

11

Quiet If true and failonerror is false, then do not log a warning message when the file to copy does not exist or one of the nested filesets points to a directory that doesn’t exist or an error occurs while copying.

12

Verbose Log the files that are being copied.

13

Encoding The encoding to assume when filter-copying the files.

14

Outputencoding The encoding to use when writing the files.

15

Enablemultiplemappings If true the task will process to all the mappings for a given source path. If false the task will only process the first file or directory. This attribute is only relevant if there is a mapper subelement.

16

Granularity The number of milliseconds leeway to give before deciding a file is out of date. This is needed because not every file system supports tracking the last modified time to the millisecond level. This can also be useful if source and target files live on separate machines with clocks being out of sync.

Example

Usage

使用以下内容创建 build.xml −

Create build.xml with the following content −

<?xml version="1.0"?>
<project name="TutorialPoint" default="info">
   <target name="info">
      <copy file="text.txt" tofile="textcopy.txt"></copy>
   </target>
</project>

上面的脚本会在当前目录中将一个名为 text.txt 的文件复制为 textcopy.txt

Above script will copy a file say text.txt in the current directory as textcopy.txt.

Output

在上述构建文件上运行 Ant 会生成以下输出:

Running Ant on the above build file produces the following output −

F:\tutorialspoint\ant>ant
Buildfile: F:\tutorialspoint\ant\build.xml

info:
   [echo] Copying 1 file to F:\tutorialspoint\ant

BUILD SUCCESSFUL
Total time: 1 second