`
feishihai
  • 浏览: 1019 次
  • 性别: Icon_minigender_1
  • 来自: 成都
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

Ant Copy Task (转)

阅读更多
转自:http://ant.apache.org/manual/Tasks/copy.html

很详细的讲解ant copy task资料,很不错。

Description

Copies a file or resource collection to a new file or directory. By default, files are only copied if the source file is newer than the destination file, or when the destination file does not exist. However, you can explicitly overwrite files with the overwrite attribute.

Resource Collections are used to select a group of files to copy. To use a resource collection, the todir attribute must be set. Note that some resources (for example the file resource) return absolute paths as names and the result of using them without using a nested mapper (or the flatten attribute) may not be what you expect.

Note: If you employ filters in your copy operation, you should limit the copy to text files. Binary files will be corrupted by the copy operation. This applies whether the filters are implicitly defined by the filter task or explicitly provided to the copy operation as filtersets. See encoding note.
Parameters
Attribute Description Required
file The file to copy. Yes, unless a nested resource collection element is used.
preservelastmodified Give the copied files the same last modified time as the original source files. No; defaults to false.
tofile The file to copy to. With the file attribute, either tofile or todir can be used.
With nested resource collection elements, if the number of included resources is greater than 1, or if only the dir attribute is specified in the <fileset>, or if the file attribute is also specified, then only todir is allowed.
Prior to Apache Ant 1.8.2 the tofile attribute only supported filesystem resources top copy from.
todir The directory to copy to.
overwrite Overwrite existing files even if the destination files are newer. No; defaults to false.
force Overwrite read-only destination files. since Ant 1.8.2 No; defaults to false.
filtering Indicates whether token filtering using the global build-file filters should take place during the copy. Note: Nested <filterset> elements will always be used, even if this attribute is not specified, or its value is false (no, or off). No; defaults to false.
flatten Ignore the directory structure of the source files, and copy all files into the directory specified by the todir attribute. Note that you can achieve the same effect by using a flatten mapper. No; defaults to false.
includeEmptyDirs Copy any empty directories included in the FileSet(s). No; defaults to true.
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. No; defaults to true.
verbose Log the files that are being copied. No; defaults to false.
encoding The encoding to assume when filter-copying the files. since Ant 1.5. No - defaults to default JVM encoding
outputencoding The encoding to use when writing the files. since Ant 1.6. No - defaults to the value of the encoding attribute if given or the default JVM encoding otherwise.
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. since Ant 1.6. No - defaults to false.
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. Default is 1 second, or 2 seconds on DOS systems. This can also be useful if source and target files live on separate machines with clocks being out of sync. since Ant 1.6.2. No
Parameters specified as nested elements
fileset or any other resource collection

Resource Collections are used to select groups of files to copy. To use a resource collection, the todir attribute must be set.

Prior to Ant 1.7 only <fileset> has been supported as a nested element.
mapper

You can define filename transformations by using a nested mapper element. The default mapper used by <copy> is the identity mapper.

Since Ant 1.6.3, one can use a filenamemapper type in place of the mapper element.

Note that the source name handed to the mapper depends on the resource collection you use. If you use <fileset> or any other collection that provides a base directory, the name passed to the mapper will be a relative filename, relative to the base directory. In any other case the absolute filename of the source will be used.
filterset

FilterSets are used to replace tokens in files that are copied. To use a FilterSet, use the nested <filterset> element.

It is possible to use more than one filterset.
filterchain

The Copy task supports nested FilterChains.

If <filterset> and <filterchain> elements are used inside the same <copy> task, all <filterchain> elements are processed first followed by <filterset> elements.
Examples

Copy a single file

  <copy file="myfile.txt" tofile="mycopy.txt"/>

Copy a single file to a directory

  <copy file="myfile.txt" todir="../some/other/dir"/>

Copy a directory to another directory

  <copy todir="../new/dir">
    <fileset dir="src_dir"/>
  </copy>

Copy a set of files to a directory

  <copy todir="../dest/dir">
    <fileset dir="src_dir">
      <exclude name="**/*.java"/>
    </fileset>
  </copy>

  <copy todir="../dest/dir">
    <fileset dir="src_dir" excludes="**/*.java"/>
  </copy>

Copy a set of files to a directory, appending .bak to the file name on the fly

  <copy todir="../backup/dir">
    <fileset dir="src_dir"/>
    <globmapper from="*" to="*.bak"/>
  </copy>

Copy a set of files to a directory, replacing @TITLE@ with Foo Bar in all files.

  <copy todir="../backup/dir">
    <fileset dir="src_dir"/>
    <filterset>
      <filter token="TITLE" value="Foo Bar"/>
    </filterset>
  </copy>

Collect all items from the current CLASSPATH setting into a destination directory, flattening the directory structure.

  <copy todir="dest" flatten="true">
    <path>
      <pathelement path="${java.class.path}"/>
    </path>
  </copy>

Copies some resources to a given directory.

  <copy todir="dest" flatten="true">
    <resources>
      <file file="src_dir/file1.txt"/>
      <url url="http://ant.apache.org/index.html"/>
    </resources>
  </copy>

If the example above didn't use the flatten attribute, the <file> resource would have returned its full path as source and target name and would not have been copied at all. In general it is a good practice to use an explicit mapper together with resources that use an absolute path as their names.

Copies the two newest resources into a destination directory.

  <copy todir="dest" flatten="true">
    <first count="2">
      <sort>
        <date xmlns="antlib:org.apache.tools.ant.types.resources.comparators"/>
        <resources>
          <file file="src_dir/file1.txt"/>
          <file file="src_dir/file2.txt"/>
          <file file="src_dir/file3.txt"/>
          <url url="http://ant.apache.org/index.html"/>
        </resources>
      </sort>
    </first>
  </copy>

The paragraph following the previous example applies to this example as well.

Unix Note: File permissions are not retained when files are copied; they end up with the default UMASK permissions instead. This is caused by the lack of any means to query or set file permissions in the current Java runtimes. If you need a permission-preserving copy function, use <exec executable="cp" ... > instead.

Windows Note: If you copy a file to a directory where that file already exists, but with different casing, the copied file takes on the case of the original. The workaround is to delete the file in the destination directory before you copy it.

Important Encoding Note: The reason that binary files when filtered get corrupted is that filtering involves reading in the file using a Reader class. This has an encoding specifing how files are encoded. There are a number of different types of encoding - UTF-8, UTF-16, Cp1252, ISO-8859-1, US-ASCII and (lots) others. On Windows the default character encoding is Cp1252, on Unix it is usually UTF-8. For both of these encoding there are illegal byte sequences (more in UTF-8 than for Cp1252).

How the Reader class deals with these illegal sequences is up to the implementation of the character decoder. The current Sun Java implemenation is to map them to legal characters. Previous Sun Java (1.3 and lower) threw a MalformedInputException. IBM Java 1.4 also thows this exception. It is the mapping of the characters that cause the corruption.

On Unix, where the default is normally UTF-8, this is a big problem, as it is easy to edit a file to contain non US Ascii characters from ISO-8859-1, for example the Danish oe character. When this is copied (with filtering) by Ant, the character get converted to a question mark (or some such thing).

There is not much that Ant can do. It cannot figure out which files are binary - a UTF-8 version of Korean will have lots of bytes with the top bit set. It is not informed about illegal character sequences by current Sun Java implementions.

One trick for filtering containing only US-ASCII is to use the ISO-8859-1 encoding. This does not seem to contain illegal character sequences, and the lower 7 bits are US-ASCII. Another trick is to change the LANG environment variable from something like "us.utf8" to "us".
分享到:
评论

相关推荐

    替换文本文件中Ant表达式的Ant扩展任务

    &lt;taskdef name="copy-with-replace" classpath="${basedir}/release/ant-task-ext.jar;${basedir}/lib/ant-1.9.1.jar;${basedir}/lib/commons-lang-2.6.jar" classname="kingspider.tools.ant.task....

    ant-deb-task.jar.zip

    标题中的"ant-deb-task.jar.zip"是一个压缩文件,它包含了一个名为"ant-deb-task.jar"的Java可执行文件(JAR包)以及一个"ant.license.txt"的文本文件,很可能包含了Apache Ant项目的授权信息。Apache Ant是Java开发...

    ant.jar下载

    org.apache.tools.ant.Task org.apache.tools.bzip2.CRC org.apache.tools.ant.Target org.apache.tools.ant.Project org.apache.tools.zip.ZipFile org.apache.tools.zip.ZipLong org.apache.tools.ant....

    org.apache.tools.*需要的ant.jar包

    - **任务(Task)**:Ant通过各种预定义的任务来完成构建过程,如`&lt;javac&gt;`用于编译Java源代码,`&lt;jar&gt;`用于打包JAR,`&lt;copy&gt;`用于复制文件等。 - **属性(Property)**:Ant允许定义属性来存储可配置的值,这些值...

    Ant & SVN task script

    ### 关于Ant与SVN任务脚本的知识点 #### 一、引言 在软件开发过程中,版本控制是一项至关重要的技术。它不仅帮助团队管理代码变更,还支持并行开发,确保不同开发者能够同时在同一个项目上工作而不会发生冲突。...

    ant脚本资料ant

    - **任务(Task)**:是Ant的基本工作单元,比如编译源代码、复制文件、运行测试等,每个任务都有其特定的用途和属性。 2. **Ant脚本编写** - **XML结构**:Ant脚本以XML格式编写,遵循特定的命名空间和元素结构...

    ant 1.10.3 源码和bin

    这些任务可以是内置的,如编译Java代码(javac task)、复制文件(copy task)或运行测试(junit task),也可以是用户自定义的任务。Ant的XML配置文件,通常命名为build.xml,定义了整个构建过程的步骤和依赖关系。...

    ant工具和ant教程

    Ant的构建文件是基于XML的,其基本结构包括项目(project)、目标(target)和任务(task)。项目元素定义了整个构建的根,通常包括版本信息和属性(property)定义。属性可以在整个构建文件中引用,允许动态配置。...

    Ant入门-ant入门pdf

    Ant的核心概念是基于任务(task)的,这些任务可以是编译源代码、打包、测试、部署等任何构建过程中的步骤。在《Ant入门》这本书中,读者会了解到如何设置Ant环境,包括下载和安装Ant,以及配置环境变量。 书中的内容...

    ant介绍Ant是什么

    除了内置任务,开发者可以通过实现 `Task` 接口创建自己的任务,这使得 Ant 能够处理几乎任何类型的构建需求。XML 配置文件的结构清晰,使得多人协作变得容易,也方便自动化工具的集成。 总之,Ant 是 Java 开发中...

    Ant参考教程,Ant使用指南,Ant全攻略

    任务(task)是Ant中最基本的工作单元,例如`javac`用于编译Java源码,`copy`用于复制文件,`jar`用于创建JAR文件等。 "ant+初步.doc"可能会涵盖Ant的安装、环境配置以及第一个简单的构建文件的创建。一个基本的...

    ant 中文手册2.0

    在Ant中,XML标签用于定义这些任务,比如`&lt;javac&gt;`用于编译Java源代码,`&lt;jar&gt;`用于创建JAR文件,`&lt;copy&gt;`用于文件复制,以及`&lt;mkdir&gt;`用于创建目录等。 在Ant中,属性(property)用于存储可配置的值,可以通过...

    Ant权威指南(ant 学习)

    一个基本的Ant脚本包含项目(Project)、目标(Target)和任务(Task)元素。项目定义了整个构建的上下文,目标是一系列任务的集合,任务则是实际执行的动作。 四、Ant的依赖关系 在Ant中,目标之间可以设置依赖...

    ant-1.8.0 apache-ant-1.8.0

    5. **文件操作**:Ant包含了丰富的文件操作任务,如`copy`、`move`、`delete`和`mkdir`,使得在构建过程中对文件和目录的操作变得简单。 6. **国际化支持**:Ant 1.8.0对多语言环境提供了支持,允许构建脚本处理...

    apache ANT 源代码

    Ant内置了许多任务,如`javac`用于编译Java源码,`jar`用于创建JAR文件,`copy`用于复制文件等。同时,用户也可以自定义任务来扩展Ant的功能。 3. **Property**:属性是Ant中用来存储值的变量,它们可以在整个构建...

    ant 开发及整合 源码 实例

    3. **任务(Task)**:Ant提供了许多内置任务,如javac(用于Java编译)、copy(文件复制)、jar(创建JAR文件)和delete(删除文件或目录)。通过实例学习如何使用这些任务来实现特定的构建步骤。 4. **自定义任务...

    Ant使用详细介绍(中文版)

    这取决于 Ant 的任务(Task),Ant 有哪些任务,就能完成哪些工作! Ant 的基本概念 Ant 的运行,靠的是一个构建脚本,默认的名称叫 build.xml,如果你在任何目录下运行 ant 命令,Ant 将自动查找当前目录下有没有 ...

    ANT初步运行例子,ANT学习

    2. **任务(Task)**: Ant通过任务来执行具体的动作,如`&lt;javac&gt;`用于编译Java源代码,`&lt;copy&gt;`用于复制文件,`&lt;mkdir&gt;`用于创建目录等。在"hello world"示例中,可能只有一个或两个任务来展示基础操作。 3. **属性...

    ant.rar java的架包ant

    3. 任务(Task):任务是Ant的最小执行单元,如`&lt;javac&gt;`用于编译Java源码,`&lt;copy&gt;`用于复制文件,`&lt;jar&gt;`用于打包成JAR文件等。 二、Ant的主要功能 1. 编译:Ant可以自动编译Java源代码,并且支持多种编译器,如...

    ant+video.rar

    视频资源“Ant 入门讲解视频(22分03秒).doc”可能包含以下内容:首先,可能会介绍Ant的基本概念,如构建文件build.xml的结构和元素,如`&lt;project&gt;`、`&lt;target&gt;`、`&lt;task&gt;`等。其次,会详细解释如何设置属性...

Global site tag (gtag.js) - Google Analytics