ANT - How to copy a group of files maintaining the Linux permissions - ant

I need to copy a group of files with ant.
Unfortunately I can't use the target "copy" because it loses the Linux file permissions. So must use the target "execute" "cp" .
How can I pass a group of file to the execute cp target? I know that I have to use a fileset but in which manner I can pass a fileset as argument of the execute cp target?

You can't pass filesets to operating system commands. The best you can do is use the apply task to invoke the "cp" command on each file as follows:
<apply executable="cp">
<srcfile/>
<targetfile/>
<fileset dir="src" includes="*.txt"/>
<globmapper from="*.txt" to="output/*.txt"/>
</apply>
But, I don't really understand why you couldn't combine the copy task with chmod, it would be most efficient:
<copy todir="output">
<fileset dir="src" includes="*.txt"/>
</copy>
<chmod perm="700">
<fileset dir="output" includes="*.txt"/>
</chmod>

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.

How To copy a file in ant irrespective of its location

I want to copy a jar file from ../source,but I am not sure where it exactly is,it can be anywhere in any subdirectory but it is there. I am writing this code
<target name="CopyingFromSource" depends="clean">
<copy file="../source/org.springframework-spring-support-2.0.8.jar" todir="../result"/>
</target>
but is showing me can't find org.springframework-spring-support-2.0.8.jar Can you please tell me how to copy it?
It is present anywhere either immediately in /source or in any subdirectory.
Use copy task with nested fileset like that :
<project>
<copy todir="../result">
<fileset dir="/home/gilreb/temp" includes="**/org.springframework-spring-support-2.0.8.jar" id="foobar"/>
</copy>
</project>
see Ant manual copy task and Ant manual fileset for turther details.. i.e. if you need to copy the flat file only and not it's whole path you need to use :
<copy todir="../result" flatten="true">
...
</copy>

ant target : copying directories and the respective files that are in it

Suppose I have a directory structure like the following
/a/b/testB.xml
/a/c/testC.xml
/a/testD.xml
and I want to copy everything inside /a to /build
so that I will have
/build/b/testB.xml
/build/c/testC.xml
/build/testD.xml
What ant command should I use? I have tried using fileset and it looks like that it only copies the files that are specified in the includes to the todir directory.
Try this:
<copy todir="build">
<fileset dir="a">
<include name="**/*"/>
</fileset>
</copy>
The docs on FileSet are here.

fileset doesn't support the "erroronmissingdir" attribute

I am using ant 1.7, getting following error:
build.xml:55: fileset doesn't support the "erroronmissingdir" attribute
what is the alternative attribute for erroronmissingdir( it is in 1.8) in 1.7
The fileset erroronmissingdir attribute is available since Ant 1.7.1. You must be using an earlier release of 1.7.
The attribute is used to tell the build to silently ignore filesets for which the base dir does not exist at execution time:
<copy todir="tmp">
<fileset dir="foo" erroronmissingdir="false">
<include name="**/*"/>
</fileset>
</copy>
If you do not specify erroronmissingdir="false" (or cannot, because your version of Ant does not support it), then the default result is build failure if the dir foo does not exist.
If you need your build to succeed whether or not the dir exists, and you cannot use the erroronmissingdir attribute, you have some options.
For example, you could specify the base dir of the fileset to be a known-to-exist parent of your target dir, something like this:
<copy todir="tmp">
<fileset dir=".">
<include name="foo/**/*"/>
</fileset>
</copy>
(Note in this case, the copy will now create dir foo in the todir of the copy. You could strip that using a glob mapper.)
Another alternative would be to execute your conditionally available fileset operations in targets, guarded by a condition, e.g.
<available property="foo.available" file="foo"/>
<target name="test" if="foo.available">
<copy todir="tmp">
<fileset dir="foo">
<include name="**/*"/>
</fileset>
</copy>
</target>
Output with ant -v will show:
[available] Unable to find foo to set property foo.available
test: Skipped because property 'foo.available' not set.
BUILD SUCCESSFUL Total time: 0 seconds

How to create directories specified by a mapper in Ant

Given a fileset
<fileset id="myFiles" dir=".">
<include name="**/*.file"/>
</fileset>
How do I create a sub-directory at each file in the set, named after the filename without the extension?
For example, given the files folderA/X.file and folderA/folderB/Y.file, I want to create the directories folderA/X and folderA/folderB/Y
The ant touch task supports creating files, parent dirs and filename mapping, so can be used to achieve this:
<target name="mkdirs">
<touch mkdirs="true">
<fileset dir="the_dir"/>
<mapper type="glob" from="*.file" to="the_dir/*/.tmp" />
</touch>
<delete>
<fileset dir="the_dir" includes="**/.tmp"/>
</delete>
</target>
This creates temporary files in target dirs (creating the dirs if the don't exist) then deletes the temporary files leaving the dirs you wanted.
You would be using for task to iterate on your file list. But I have not come across any substring type of utility in Ant which you can use to strip the extension and create the directory. Do search for this utility, if its not there then you need to implement an Ant task to do that.
Sorry to answer my own question. Unless someone knows otherwise, there appears to be no way for out-of-the-box ANT to create directories (e.g. using mkdir) relative to entries in a fileset.
Ant-Contrib contains useful for loop tasks, as Bhushan suggests, which could possibly perform this sort of task.
Had some better things to be getting on with, so in the end, I just wrote a batch file called by an ANT task (apply tasks can iterate over filesets).
<apply executable="cmd" failonerror="1">
<arg value="/c"/>
<arg line="build\tools\makeRelDir.bat"/>
<fileset dir=".">
<include name="**/*.file"/>
</fileset>
</apply>
where the batch file does this:
mkdir %~dp1%~n1
(Why is it so hard to do something some simple in ANT? Am I missing something?)

Resources