How to copy a directory using Ant

2019-01-22 01:08发布

I have used copydir to copy a directory tree but it is deprecated. My directory contains some sub-directories, and some of those contain files and others contain more sub-directories.

How can I copy the entire tree?

标签: java ant
11条回答
可以哭但决不认输i
2楼-- · 2019-01-22 01:10

This code should copy the folder as well as its contents. It also uses the basename task to avoid having to do any manual path parsing.

<project name="Build" default="doCopy">
  <property name="source.dir" value="SourceDirPathGoesHere"/>
  <property name="dest.dir" value="DestinationDirPathGoesHere"/>
  <target name="doCopy">
    <basename property="source.dir.base.name" file="${source.dir}"/>
    <copy todir="${dest.dir}">
      <fileset dir="${source.dir}/.." includes="${source.dir.base.name}/**"/>
    </copy>
  </target>
</project>
查看更多
Fickle 薄情
3楼-- · 2019-01-22 01:12

You should only have to specify the directory (sans the includes property):

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

See the manual for more details and examples.

查看更多
成全新的幸福
4楼-- · 2019-01-22 01:12

Another ant task is Copydir. The key part here is to include the name of the directory you want to copy after the dest directory. The sub-directories and files will be copied automatically.

<target name="-post-jar">
    <copydir src="config" dest="${dist.dir}/config/"/>
</target>
查看更多
地球回转人心会变
5楼-- · 2019-01-22 01:18

Copy contents including the directory itself.

<copy todir="${dest.dir}" >  
    <fileset dir="${src.dir.parent}">  
        <include name="${src.dir}/**"/>
    </fileset>
</copy>
查看更多
爷的心禁止访问
6楼-- · 2019-01-22 01:18

Copy contents including the directory itself.

<copy todir="${dest.dir}" >  
  <fileset dir="${src.dir.parent}" includes="${src.dir}/**"/>
</copy>
查看更多
我命由我不由天
7楼-- · 2019-01-22 01:19

I finally copied using following code

<copy todir="${root.dir}/dist/src">  
                <fileset dir="${root.dir}/build/src" includes="**"/>  
            </copy>

This will copy the src folder from dist to build.

Hope this helps someone.

查看更多
登录 后发表回答