Ant - copy files and subdirectories from only one subdirectory in the tree

I would like to copy files and subdirectories using Ant from one subdirectory without copying the rest of the directory structure and contents. For example, I would like to copy dir_3, its files (file_1 in file_n) and subdirectories (dir_4 and dir_5), but not dir_1 and dir_2. Is there a sample that I can use for this?

temp \--dir_1 \--dir_2 | \--dir_3 | \--dir_4 \--dir_5 \-- file_1 | \--file_n 

Thanks.

+6
source share
2 answers

It's not that hard ...

 <copy todir="${copy.dir}"> <fileset dir="temp"> <include name="**/dir3/**"/> </fileset> </copy> 

When you use the include directive, it will only contain files that match the template that you give it. In this case, I copy only those files that have /dir3/ located somewhere along their full path. This includes subdirectories under dir3 and all files under dir3 .

You can use the exclude directive to override include directives:

 <copy todir="${copy.dir}"> <fileset dir="temp"> <include name="**/dir3/**"/> <exclude name="**/dir3/*"/> </fileset> </copy> 

This will copy all the subdirectories and files in these subdirectories, but not the files under dir3 . * matches all files in a directory, and ** matches all files of the entire directory tree.

Note that this will create the temp/dir2/dir3 . If I want temp/dir3 , I have to install my set of files in the parent directory dir3 :

 <copy todir="${copy.dir}"> <fileset dir="temp/dir2"> <include name="dir3/**"/> </fileset> </copy> 

Performing this action:

 <copy todir="${copy.dir}"> <fileset dir="temp/dir2/dir3"/> </copy> 

Creates a temp directory with all files directly under dir3 directly under temp . There will also be a temp/dir4 and temp/dir5 containing all the files (and directory trees) in these directories.

+16
source
 <copy todir="/some/path/foobar" verbose="true"> <fileset dir="/some/path/temp/dir2" includes="**"/> </copy> 

just use a set of files starting with dir2, including all files and files below.
verbose = true to repeat all files copied
Perhaps you need to use overwrite = true also if the directory specified by the todir pointer attribute already exists, otherwise the existing files will not be overwritten by the copy task

+1
source

Source: https://habr.com/ru/post/891043/


All Articles