Need pattern matching in Ant for copying only parent folders - ant

I want to copy only the parent folders names from source and then create the folders with those names in a destination directory.
Ex:
source
- folder_1
- sub_1
- folder_2
- sub_2
- folder_3
- sub_3
destination
- folder_1
- folder_2
- folder_3
Could you let me know the pattern matching for this requirement in ANT.
Regards,
Satya

The way to do what you ask will depend on how you intend to use the directory list once you have it,
but it sounds like you need a dirset. Here's an example build file snippet:
<property name="src.dir" value="src" />
<dirset id="my.dirset" dir="${src.dir}" includes="*"/>
<echo message="${toString:my.dirset}" />
Specifying * for the includes attribute will pick up only directories at the top-level under ${src.dir}. Then, for example, the copy task can then be used to copy those (empty) directories somewhere:
<copy todir="${dest.dir}" >
<dirset refid="my.dirset" />
</copy>

Related

Update destination directory with new files (if modified) using ant

I'm facing a problem with ant "copy". Here is my requirement:
I want to sync 2 dirs(dir1 and dir2) but i want to keep the extra files/dirs present in dir2. My aim is i want to copy dir1 contents(if modified ) to dir2 but want to keep any additional files/dirs present in dir2.
I tried ant's sync task, but it is trying to keep both dirs in sync ie., it is deleting extra contents present in dir2. I don't find any flag to disable this feature:
<sync todir="dir2" failonerror="true" verbose="true">
<fileset dir="dir1" excludes="*.svn" />
</sync>
I tried ant's copy with "modified" selector, but its also doing the same: :(
<copy todir="dir2" failonerror="true">
<fileset dir="dir1" excludes="*.svn" >
<modified/> <!-- Copies only modified files -->
</fileset>
Can any one suggest, how can i achieve my requirement with ant?
By default, the ANT copy task does not overwrite files:
<copy todir="target/dir2" verbose="true" overwrite="false">
<fileset dir="src/dir1"/>
</copy>
The copy task will also detect if the file has changed.
The copy task will not delete files, so I don't understand why your second example (using a modified selector) did not work.

Ant: Zip the contents of a directory, without the top-level directory itself

How can I use Ant to create zip containing all the files and nested directories inside some root directory, but exclude the top-level directory itself from the zip.
For example, say I have this file structure:
/foo
splat.js
/bar
wheee.css
I want the zip to contain splat, and wheee inside /bar, but I don't want all that to be contained inside a 'foo' directory. In other words, unzipping this into /honk should not create a foo directory; splat and bar should end up at the root of /honk.
I'm currently using this (extraneous details removed):
<zip destfile="${zipfile}" basedir="" includes="${distRoot}/**/*.*" />
What kind of fileset select can replace that 'includes' spec to achieve this?
It's not dynamic, but using the fullpath attribute allows you to define the path struture of the zip file. See ant documentation: http://ant.apache.org/manual/Types/zipfileset.html
<zip destfile="YourZipName.zip">
<zipfileset fullpath="splat.js" dir="foo" includes="splat.js"/>
<zipfileset fullpath="bar/wheee.css" dir="foo/bar" includes="wheee.css"/>
</zip>
This may get you want you want dynamically. It should pull in everything in the foo dir then zip it up. It's just a few extra steps.
<mkdir dir="DirToBeZipped"/>
<copy todir="DirToBeZipped">
<fileset dir="foo" includes="*"/>
</copy>
<zip destfile="YourZipName.zip" basedir="DirToBeZipped"/>

How to check if files in one directory exist in another directory?

I am looking for a means to have Ant check a target directory for the existence of files in a list/set defined by a source directory.
The Ant target only needs to determine if the files in a source directory also currently exist in the target directory. No copying/moving/deleting necessary.
Can this be done in Ant?
Take a look at the Ant resource collection set operators. For example:
<fileset id="source" dir="source_dir" />
<fileset id="target" dir="target_dir" />
<difference id="difference">
<resources refid="source"/>
<resources refid="target"/>
</difference>
<echo message="${toString:difference}" />
You can use a dirset if you're only interested in directories.

Apache Ant: Selecting files with fileset?

It's really easy to select a file with a specific filename, or filetype using fileset in ANT, however I have yet not figured out how to write a fileset that remove all files with a filename beginning with a dot, such as .builtpath, and .hgignore, but excluding .htaccess;
Here's my current file:
<delete includeemptydirs="true">
<fileset dir="${temp.dir}/fromRepo">
<exclude name=".htaccess"/>
<include name="**/*" /> <!-- How to select files starting with .?!-->
</fileset>
</delete>
Suggest you try:
<delete includeemptydirs="true">
<fileset dir="${temp.dir}/fromRepo">
<exclude name="**/.htaccess"/>
</fileset>
</delete>
If you don't specify any wildcard - as in ".htaccess" then that rule will only match the exact file name, i.e., '.htaccess' in the top-level directory of the fileset. Prepending the directory wildcard ** to .htaccess will tell Ant to exclude from the delete all files called '.htaccess' found under the directory hierarchy of the fileset.
There's an implicit include of all files if you don't specify any include rule - so no need to specify the 'global' include.
One thing to watch out for - setting includeemptydirs true will remove any empty directories when using a fileset with the delete task. A directory will only be considered empty if it doesn't contain any files. In other words: directories containing a file called '.htaccess' will not be deleted, but those with a '.htaccess' file will not be deleted - hope that's what you need.

Copy content of subfolders with Ant

How can I copy content of all subfolders of given folder using Ant?
i.e. I have such folder structure
folder/
folder/sub1/1.txt
folder/sub1/f1/1.txt
folder/sub2/2.txt
...
I don't know exact names of subfolders. And I need to copy content from all of them into one folder (keeping the structure of content, i.e. copying all files into one dir using flatten isn't a solution). I need to get
newfolder/1.txt
newfolder/1/1.txt
newfolder/2.txt
...
Does fileset allows to group subfolders in such a way?
** stands for zero or more directories, and usage of * as directory name is disallowed, i.e. <fileset dir="${dir}/*/" /> isn't acceptable.
Thanks in advance, Yury
<copy toDir="newfolder">
<fileset dir="folder">
<include name="*/**"/>
<exclude name="*"/>
</fileset>
<regexpmapper from="^[^/]*/(.*)$$" to="\1" handledirsep="true"/>
</copy>
You only need to specify handledirsep if you ever intend to run this script in Windows.

Resources