Java 如何使用 Ant 复制目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1685442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:34:50  来源:igfitidea点击:

How to copy a directory using Ant

javaant

提问by Sunil Kumar Sahoo

I have used copydirto 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.

我曾经copydir复制过目录树,但它已被弃用。我的目录包含一些子目录,其中一些包含文件,其他包含更多子目录。

How can I copy the entire tree?

如何复制整个树?

采纳答案by Omnipresent

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

believe that will do what you want... (Recursive copy done)

相信会做你想做的......(递归复制完成)

回答by s1n

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

您应该只需要指定目录(没有包含属性):

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

See the manualfor more details and examples.

有关更多详细信息和示例,请参阅手册

回答by NawaMan

From the example here, you can write a simple Ant file using copy task.

这里的示例中,您可以使用复制任务编写一个简单的 Ant 文件。

<project name="MyProject" default="copy" basedir=".">
    <target name="copy">
        <copy todir="./new/dir">
           <fileset dir="src_dir"/>
        </copy>
    </target>
</project>

This should copy everything inside src_dir(excluding it) to new/dir.

这应该将里面的所有内容src_dir(不包括它)复制到new/dir.

回答by ery

Copy contents including the directory itself.

复制内容,包括目录本身。

<copy todir="${dest.dir}" >  
    <fileset dir="${src.dir.parent}">  
        <include name="${src.dir}/**"/>
    </fileset>
</copy>

Note: ${src.dir} is relative to ${src.dir.parent}, and not a full path

注意:${src.dir} 相对于 ${src.dir.parent},而不是完整路径

回答by Jess

A fine point: ant will only copy the sub-directories if the source files are newer than the destination files. [1]In my case, the sub-dirs were not being copied (I am using verbose="true"), since there were no changes and they were already in the destination. You can use "overwrite" to force it, or touch some of the files in your source sub-dirs.

一个很好的点:如果源文件比目标文件新,ant 只会复制子目录。 [1]就我而言,子目录没有被复制(我使用的是verbose="true"),因为没有更改并且它们已经在目标中。您可以使用“覆盖”来强制它,或触摸源子目录中的某些文件。

回答by cmcginty

Copy contents including the directory itself.

复制内容,包括目录本身。

<copy todir="${dest.dir}" >  
  <fileset dir="${src.dir.parent}" includes="${src.dir}/**"/>
</copy>

回答by user506069

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.

此代码应复制文件夹及其内容。它还使用 basename 任务来避免进行任何手动路径解析。

<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>

回答by Dilip Rajkumar

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.

这会将 src 文件夹从 dist 复制到 build。

Hope this helps someone.

希望这可以帮助某人。

回答by sartoris

I'm adding a more generic pattern to copy all subfolders.

我正在添加一个更通用的模式来复制所有子文件夹。

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

See Patternsfor details.

有关详细信息,请参阅模式

回答by Raman B

I used include tags as shown in below code snippet in my build.xml file to copy individul jar files inside a directory.

我在 build.xml 文件中使用了如下代码片段所示的包含标签来复制目录中的单个 jar 文件。

<target name="devInstall" depends="generateXsl" description="testing">
<copy flatten="true" todir="${test}/WEB-INF/lib" overwrite="${overwrite}">
                <fileset refid="buildJars"/>
                <fileset dir="lib">
                    <include name="commons-collections-*.jar"/>
                    <include name="commons-io-*.jar"/>              
                    <include name="kodo/*.jar"/>
                    <include name="mail*.jar"/>    
                    <include name="activation*.jar"/>               
                    <include name="guava*.jar"/>
                    <include name="jna*.jar"/>                          
                </fileset>          
            </copy>
</target>