Use ant's copy

Source: Internet
Author: User

The ant version is 1.6.5.

1. Copy a single file to the specified directory.
Example: <copy todir = "$ {basedir}/New" file = "$ {basedir}/old/old1.txt1">
Copy the $ {basedir}/old/old.txt file to $ {basedir}/New.

2. Copy a batch of files to the specified directory
Example: <copy todir = "$ {basedir}/New">
<Fileset dir = "$ {basedir}/old">
<Include name = "old1.txt"/>
<Include name = "old2.txt"/>
<Exclude name = "old8.txt"/>
</Fileset>
</Copy>
Here, fileset defines the composition form of the original file. <include/> sub-attributes are included, and <exclude/> sub-attributes are excluded. It is very easy to filter multiple files by combining them, of course, this example is silly. For example
<Include name = "appgen/**"/>
<Include name = "ibatis/**"/>

<Include name = "ibatis/**, **"/> <! -- All files in the whole ibatis include subdirectories -->
<Exclude name = "**/*. log"/>
Copy all the files and subdirectories except. log files under the appget directory and ibatis directory.

<Fileset/> can be abbreviated as <fileset dir = "$ {basedir}/old" primary des = "old1.txt,old2.txt"/>. The primary des can be interpreted as the plural form of include, multiple files are separated by commas (,). The same applies to excludes.

3. Copy a directory to the specified directory
Example: <copy todir = "$ {basedir}/New">
<Fileset dir = "$ {basedir}/old">
<Include name = "appgen"/>
<Include name = "appgen/"/>
<Include name = appgen/** "/>
<Include name = "appgen/***"/>
</Fileset>
</Copy>
Similarly, the <fileset/> attribute is used to specify the directory name. However, the <include/> sub-attribute and <include/> sub-attribute are used in two cases.
If you use <include/>, there are three more cases:
For "appgen", only the empty directory named appgen will be copied, and the files and subdirectories in it will not be copied.
If it is "appgen/" or "appgen/**", the entire appgen directory will be copied, including the files and subdirectories.
If it is "appgen/*", it only copies the contents of the Directory and the first-level sub-directories in the directory, instead of the second-level and second-level sub-directories. Note: "appgen/*" here is a * number. If the * number is greater than two, it works the same as the * number. For example, "appgen/*" and "appgen/***" only copy the first level subdirectory under the appgen directory.

Note: If the appeng directory is an empty directory, the empty directory will not be copied no matter how it is written. That is to say, the copy operation will not create an empty directory. To create an empty directory, only mkdir is used.

If you do not use any <include> attribute, as shown in figure
<Fileset dir = "$ {basedir}/old">
</Fileset>
All files and subdirectories under $ {basedir}/old are copied.

Note: When you use <exclude/> to exclude a directory, the directory name must be in the form of "appgen/" or "appgen/**", otherwise it will not take effect.

These are the three types of copies to the directory. Note that if todir is not specified in the computer, ant will automatically create this path.

4. Copy a single object:
<Copy tofile = "old.txt" file = "new.txt"/>.
Of course, it can also be written
<Copy tofile = "$ {basedir}/New/new.txt">
<Fileset dir = "$ {basedir}/old" includes = "old.txt"/>
</Copy>
Here, des can only write one file, but cannot write multiple files. Because it cannot copy multiple files to one file, it makes no sense to write this troublesome way.

Copying must involve overwriting of the same name. Ant explains in the copy class API: files are only copied if the source file is newer than the destination file, newer indicates the file modification time. Even if the file content does not change during modification, the modification time changes, and ant overwrites the file with the same name, that is, ant does not check the file content.

For directory replication, ant checks the modification time of files in the directory to determine whether to overwrite the directories. If the modification time of a file in the directory changes, the file is overwritten, rather than the entire directory.

If you want to forcibly overwrite the data, <copy/> has an overwrite attribute. The default value is false. Change it to true.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.