Creating fileset from subfolder with dynamic foldername - ant

i want to create a fileset for deployment via SCP. So far, so good, files are being deployed, BUT:
I only want to deploy files and subfolders of a folder with a dynamic name.
My directory tree is:
Source Files
|_ _target
|_ <timestamp>-package
|_ css
|_ img
|_ js
|_ index.php
|_ css
|_ scss
|_ img
|_ config.rb
[...]
I only want to deploy the files and subfolders of
_target | <timestamp>-package
so I declared the fileset like
<fileset id="fileset:deployment" dir="${basedir}/_target">
<include name="**/**" />
</fileset>
As you see, the subfolder of "_target" has a dynamic name, because it contains a timestamp (it gets created with a different ant target).
SCP now deploys the whole folder "timestamp-package", but I want to deploy ONLY the files and subfolders IN there. Is there any way to achieve that?

There's probably several ways to do this. I'm assuming you have only one <timestamp>-package directory present at build time, as otherwise you would have ambiguity.
Here's one method. Use a <dirset> task to identify the correct parent directory for your <fileset>:
<dirset id="src.dir" dir="_target">
<include name="*-package" />
</dirset>
Then use that as part of the dir attribute of your <fileset>:
<fileset id="fileset:deployment" dir="_target/${ant.refid:src.dir}">
<include name="**/**" />
</fileset>

Related

Copying only files which have another file as a sibling [based on extension]?

I am trying to use ANT to copy some files of a C++ build into another directory.
The output of the build looks like this:
/Build
/LibA
LibA.lib
LibA.pdb
/LibB
LibB.lib
LibB.pdb
/ProjA
myexe.exe
myexe.pdb
/Foo
foo.exe
foo.pdb
/...
Now, I would like to copy all *.exe files and their *.pdb files to another directory (but not the *.pdb files of the libs).
I tried with:
<copy todir="outdir">
<fileset dir="Build">
<include name="**/*.exe" />
<include name="**/*.pdb" />
</fileset>
</copy>
However, then I will also get the *.pdb files inside the LibA, LibB, ... folders.
Is there any way I can only copy the pdb-files which have an *.exe-file in the same directory as their sibling?
Unfortunately, the folders are not named in any way that would allow using wildcards based on the folder name.
Of course, I could list each file individually, such as:
<include name="ProjA/*.pdb" />
<include name="Foo/*.pdb" />
<!-- ... -->
However, I am thinking that maybe there is an elegant way where I can specify "copy all *.exe files and all *.pdb files which have an *.exe file next to them".
You could use the <present> selector to find the "sibling" files, something like this:
<copy todir="outdir">
<fileset dir="Build" includes="**/*.pdb">
<present targetdir="Build">
<mapper type="glob" from="*.pdb" to="*.exe" />
</present>
</fileset>
<fileset dir="Build" includes="**/*.exe" />
<flattenmapper />
</copy>
This will only copy the .pdb files with matching .exe files.
To keep it simple, use a separate fileset for the exe files.
The flatten mapper is only needed if you want to copy just the files ignoring the directory structure in the source.

Copying multiple sub directory content from a parent directory into a new directory

I have a directory called config under each of the subfolders of a parent directory lets x. I want copy all the config directories from each of the sub directories of x into a new directory. I want to do through Ant since i dont want to introduce a new technology into build script. Looks foreach and for does the operation at the file level but not the directory level. Any help is appreciated.
If I understood correctly, using copy task should solve your problem:
<copy todir="${dst.dir}">
<fileset dir="${src.dir}">
<include name="**/config/**"/>
</fileset>
</copy>

adding the specified folders in the jar created using ant

I have a folder structures
build/classes/com/vaannila/domain/.class files
build/classes/com/vaannila/service/.class files
build/classes/com/vaannila/web/.class files
My requirement is to create a jar file using ant, how can I include the specified folders in the jar,say I want include only domain and service (or) domain and web in the jar, like the way I create the jar using Eclipse(Export-->archive file).
Can anyone help me with this please?
You can use include and exclude tags in the fileset element, and choose specific files...
<jar destfile="${jar.dir}/${jar.file.name}" index="true"
manifest="${manif.dir}/${manif.file.name}">
<fileset dir="${build.classes.dir}" >
<include/>
<exclude/>
</fileset>
</jar>
<fileset dir="${build.classes.dir}">
<exclude name="**/property/*"/>
<include name="**/abcd/*"/>
</fileset>
This way you can remove/include respective directories.

Ant -- copying files and subdirectories from only one subdirectory on a tree

I'd like to copy files and subdirectories using Ant from a single subdirectory without copying the rest of the directory structure and contents. For example, I'd like to copy dir_3, its files (file_1 to file_n) and subdirectories (dir_4 and dir_5), but not dir_1 nor dir_2. Is there a pattern that I can use to do this?
temp
\--dir_1
\--dir_2
|
\--dir_3
|
\--dir_4
\--dir_5
\-- file_1
|
\--file_n
Thanks.
<copy todir="${copy.dir}">
<fileset dir="temp">
<include name="**/dir3/**"/>
</fileset>
</copy>
When you use the include directive, it will only include the files that match the pattern you give it. In this case, I'm copying only those files that have /dir3/ somewhere in their full path name. This includes sub-directories under dir3 and all files under dir3.
You can use the exclude directive to override the include directives:
<copy todir="${copy.dir}">
<fileset dir="temp">
<include name="**/dir3/**"/>
<exclude name="**/dir3/*"/>
</fileset>
</copy>
This will copy all sub-directories and files in those sub directories, but not the files under dir3 itself. The * matches all files in the directory while ** matches the all the files in the entire directory tree.
Notice this will create a directory temp/dir2/dir3. If I want temp/dir3, I have to set my fileset to the parent directory of dir3:
<copy todir="${copy.dir}">
<fileset dir="temp/dir2">
<include name="dir3/**"/>
</fileset>
</copy>
Doing this:
<copy todir="${copy.dir}">
<fileset dir="temp/dir2/dir3"/>
</copy>
Will create a directory temp with all the files directly under dir3 directly under temp. There will also be a temp/dir4 and temp/dir5 directory containing all the files (and directory trees) under those directories.
<copy todir="/some/path/foobar" verbose="true">
<fileset dir="/some/path/temp/dir2" includes="**"/>
</copy>
just use a fileset starting from dir2 including all dirs and files below..
verbose = true to echo all the files copied
May be you need to use overwrite = true also if the dir that is specified by todir
attribute already exists, otherwise existing files won't be overwritten by copy task

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