ant copy copy file usage

ant copy copy files

When deploying with Ant , it appears: "Warning: Unmappable characters encoding GBK", I found it online, mainly because of the encoding compatibility problem, I need a javac specified encoding, so add the following instructions to build.xml:

<target name="compile" depends="prepare" description="compiler">
   <javac srcdir="${src.dir}" destdir="${build.classes.dir}" debug="on" deprecation= "on" optimize="off" includes="**">
    <!--Specify encoding to compiler to prevent: "Warning: Unmappable characters encoding GBK"-->
  <compilerarg line="-encoding UTF- 8 "/>  
    <classpath refid="build.classpath" />
   </javac>
</target>

 

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 of the original file, the <include/> sub-attribute means include, < The exclude/> sub-attribute indicates exclusion. It is very simple to filter multiple files through their combination. Of course, I used this example stupidly. For example
                <include name="appgen/**"/>
                <include name="



       <fileset/> can be abbreviated as <fileset dir="${basedir}/old" includes="old1.txt,old2.txt" />, includes can be understood as the plural form of include, and commas are used when including multiple files Separated, excludes the same.

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>
       also use the <fileset/> attribute, name specifies the directory name , but there are two cases here, with <include/> sub-attributes and without <include/> sub-attributes.
      If you use <include/>
          , and it is divided into three cases. If it is "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 inside.
          If it is "appgen/*", it will only copy everything in the directory and the first-level subdirectories under the directory, but will not copy the second level and below. Note: "appgen/*" here is a *, if there are more than two *, it will have the same effect as a *. For example, "appgen/*" and "appgen/****" only copy the first-level subdirectory under the appgen directory.

Note: If the append directory itself is an empty directory, no matter how it is written, the empty directory will not be copied. That is to say, the copy operation will not have the effect of creating an empty directory. To create an empty directory, you can only use mkdir.

       If you do not use any <include> attribute, such as
           <fileset dir="${basedir}/old">
           </fileset>
       will copy all files and subdirectories under ${basedir}/old.

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

       The above are three types of copying to the directory. Note that if there is no path specified by todir in the computer, ant will automatically create this path.

4. Copy a single file: 
<copy tofile="old.txt" file="new.txt" /> is as simple as that.
Of course, it can also be written as
<copy tofile="${basedir}/new/new.txt">
      <fileset dir="${basedir}/old" includes="old.

       Here includes can only write one file, not multiple files, because multiple files cannot be copied into one file, so such a troublesome writing method is meaningless.

Properties and functions of Copy Task

The Copy task has the following properties:

(1) file: used to specify the source file to be copied.

(2) preservelastmodified: The function is to make the last modification time of the copied file and the source file the same. Defaults to false.

(3) tofile: used to specify the file to be copied to.

(4) todir: used to specify the target directory to be copied to. todir and tofile can only use one of these attributes.

(5) overwrite: used to specify whether to overwrite directories and files, regardless of whether the file is newer than the source file, it will be overwritten. Defaults to false.

(6) filtering: It is used to specify whether to use the global filter of the component file to filter the file when copying. Defaults to false.

(7) flatten: used to specify whether the directory needs to be copied. If true, it means that all files are copied to the directory set by the todir attribute. Defaults to false, copy directories.

(8) includeEmptyDirs: used to specify whether to copy empty directories. Defaults to true.

(9) failonerror: used to specify whether to stop execution when an error is encountered. Defaults to true.

(10) verbose: Used to specify whether to record log information when copying files.

(11) encoding: used to set the encoding when copying files or the encoding method used by the file filter. By default, the encoding method of the Java virtual machine is used.

(12) outputencoding: Specifies the encoding method when writing files. By default, the encoding method of the Java virtual machine is used.

(13) enablemultiplemappings: used to set whether to allow multiple mappings. Defaults to false.

(14) granularity: The allowable error of millisecond data used to specify the modification time of a file. Because not all filesystem modification times are accurate to milliseconds. It is 0 by default, and 2 if it is a DOS system.

 

Example of using file filtering when executing Copy Task

Here's an example of copying a file while replacing special symbols in a file:

<copy todir="../backup/dir">

    <fileset dir="src_dir"/>

    <filterset>

      <filter token="TITLE" value="Foo Bar"/>

    </filterset>

</copy>

What this example does is copy all files in the src_dir directory to the ../backup/dir directory, and find and replace @TITLE@ with Foo Bar in all files. When a new product is to be released, the version information and time information in the file need to be replaced.

Description: The Ant tool also provides two specific tasks, copydir and copyfile, which are used to copy directories and files respectively. However, since the Copy task was created, these two tasks have expired and are no longer recommended. The Copy task should be used uniformly.

Small example: copy from workspace to Tomcat

<?xml version="1.0" encoding="UTF-8"?>
<project name="" default="lch" basedir=".">
    <property name="db_file_dir" value="C:/Users/Administrator/workspace/gdagri/WebRoot/WEB-INF/classes/gznxt/database/"/>
    <property name="row_file_dir" value="C:/Users/Administrator/workspace/gdagri/WebRoot/WEB-INF/classes/gznxt/entity/"/>
    <property name="to_db_dir"  value="D:/tool/apache-tomcat-5.5.26/webapps/gdnyw/WEB-INF/classes/gznxt/database/"/>
    <property name="to_row_dir" value="D:/tool/apache-tomcat-5.5.26/webapps/gdnyw/WEB-INF/classes/gznxt/entity/"/>

 
    <!-- Don't forget to restart the server Tomcat every time you compile it locally into the server. The following is the laefage of Agricultural Express -->
    

<target name="lch_1" >
            <copy todir="${to_row_dir}">
                <fileset dir="${row_file_dir}">
                    <include name="TransportGoodsRow.class"/>
                    <include name="TransportCartRow.class"/>
                </fileset>
            </copy>
        </target>
    
    <target name="lch" >
        <copy todir="${to_db_dir}" overwrite="true">
            <fileset dir="${db_file_dir}">
                <include name="TransportCartDB.class"/>
                <include name="TransportGoodsDB.class"/>
            </fileset>
        </copy>
    </target>
</project>

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326876687&siteId=291194637