Using Ant to delete a file based on existence of another file - ant

I need to write an ant task to selectively delete files.
In a directory, I have the following jars:
acme.jar
acme-201105251330.jar
I want to delete acme.jar because acme-*.jar exists.
Here's what I've tried:
<target name="-check-use-file">
<available property="file.exists">
<filepath> <fileset dir=".">
<include name="./test-*.jar"/> </fileset>
</filepath>
</available>
</target>
<target name="use-file" depends="-check-use-file" if="file.exists">
<!-- do something requiring that file... -->
</target>
Thanks

Have a look at If/Unless Attributes, examples given there seem to be exactly what you are looking for.

Related

Delete a folder using starts with in ANT

I am comparing 2 folders A and B, and wanted to delete folders and jars present in B which are not present in folder A.
I have written the logic to get needed files to delete, but i do not wanted to delete the directory and jars starting with "com.ibm".
For that I have written delete task as below:
<delete>
<dirset dir="D://mypath/plugins<Filename to delete> excludes="**/com.ibm.*/**" />
</delete>
I have tried the excludes with the scenarios like:
excludes="**/com.ibm.*/**"
excludes="**/com.ibm.*"
excludes="com.ibm.*"
excludes="com.ibm.*/**"
But nothing works for me (It is not deleting any folders/files). Any help would be highly appreciated. Thanks !
you should use fileset instead and specify includeemptydirs="true" of delete.
<project default="init" name="My Project">
<target name="init">
<delete verbose="true" includeemptydirs="true">
<fileset dir="/home/guest/Desktop/plugins" defaultexcludes="no">
<exclude name="com.ibm.*"/>
<exclude name="com.ibm.*/**"/>
</fileset>
</delete>
</target>
</project>

Ant do not delete file with no name like .gitignore .git

I want to use git to delete a folder,the folder contains some file with no name and only have extension,such as .gitignore,.log
I did it as below,and I also tried many other ways,but still can not delete them,can anyone help me?Thanks in advance!
<?xml version="1.0" encoding="UTF-8"?>
<project name="test" default="clean">
<target name="clean">
<delete>
<fileset dir="E:\test\tmt"/>
</delete>
<echo message="Finished delete file"/>
</target>
</project>
In the following snippet...
<delete>
<fileset dir="E:\test\tmt"/>
</delete>
....gitignore won't be deleted because .gitignore is in Ant's Default Exclude Set.
You can configure Ant to ignore the Default Exclude Set with the defaultexcludes attribute...
<delete>
<fileset dir="E:\test\tmt" defaultexcludes="false"/>
</delete>
...or even better, use the following instead...
<delete dir="E:\test\tmt"/>
defaultexcludes isn't needed in this case because the Default Exclude Set only applies to pattern sets such as <fileset>.
It is deprecated so I would go with Chad's answer, but ant delete also has an excludes property which takes in a comma-separated list of patterns (or direct file names). Just FYI.

Execute ant task over sons of parent folder

I was trying to execute a task over a fileset defined using something like this:
<target name="copyToTarget">
<copy todir="${target.folder}/" >
<fileset dir="../**/folder" />
</copy>
</target>
Is there anyway to achieve this?
EDIT:
I saw by myself I can use something like:
<fileset dir="../" includes="*/folder/** />
but the problem is that this copies the whole structure since the parent folder and I only need it contents (e.g., with this I obtain
/target/son1/folder/contents
and I'm looking for
/target/contents
EDIT 2:
Using How to strip one folder during Ant copy I managed how to get the result I need:
<target name="copyFolderToTarget">
<copy todir="${target.folder}" >
<fileset dir="../" includes="*/src_folder/**" />
<cutdirsmapper dirs="2"/>
</copy>
</target>

Ant using Fileset in jar task and renaming files

I have the following piece of code:
<jar destfile="${jar.file}">
<fileset dir="${basedir}/resources"/>
<include name="META-INF/ejb-jar.xml"/>
<include name="META-INF/persistence-prod.xml"/>
</fileset>
[...]
</jar>
The problem is that persistence-prod.xm1 should be persistence.xml when placed in the jar.
I know I could create a working directory and layout my whole jar there, and then jar that up. I know I can copy that one file elsewhere and rename it while copying. If I had a whole bunch of files named *-prod.xml to be renamed *.xml, I can use a file mapper inside the copy task. However, I want to be able to rename the file right in the <jar> task. I tried adding <globmapper> to the jar task, but I got the error message: jar doesn't support the nested "globmapper" element.
Any idea how this rename can take place while jaring the file?
Of course, the minute I asked the question, I figure out the answer:
I can't put <globmapper> directly into a <jar> task, but I can include <mappedresources> into the <jar> task, and place my <globmapper> in there:
Wrong:
<jar destfile="${jar.file}">
<fileset dir="${basedir}/resources"/>
<include name="META-INF/ejb-jar.xml"/>
<include name="META-INF/persistence-prod.xml"/>
</fileset>
<globmapper from="*-prod.xml" to="*.xml"/>
[...]
</jar>
Right:
<jar destfile="${jar.file}">
<fileset dir="${basedir}/resources"/>
<include name="META-INF/ejb-jar.xml"/>
</fileset>
<mappedresources>
<fileset dir="${basedir}/resources">
<include name="META-INF/persistence-prod.xml"/>
</fileset>
<globmapper from="*-prod.xml" to="*.xml"/>
</mappedresources>
[...]
</jar>
I guess this makes sense since it limits my file mapping to just the <mappedresources> and not to all <fileset> of the <jar> task.

Loop through directory structure in ant

We want to loop through directory structure in ant without using foreach .
Is there any elegant way to do the same ?
The apply task can iterate over a set of directories or files
<target name="run-apply">
<apply executable="echo">
<dirset dir="src"/>
</apply>
</target>
I personally like the groovy ANT task
<target name="run-groovy">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/>
<dirset id="dirs" dir="src"/>
<groovy>
project.references.dirs.each {
ant.echo it
}
</groovy>
</target>
The installation of the task jar is easily automated:
<target name="install-groovy">
<mkdir dir="${user.home}/.ant/lib"/>
<get dest="${user.home}/.ant/lib/groovy-all.jar" src="http://search.maven.org/remotecontent?filepath=org/codehaus/groovy/groovy-all/2.1.1/groovy-all-2.1.1.jar"/>
</target>
Finally if you're iterating thru other build files, the subant task is very useful:
<target name="run-subant">
<subant>
<fileset dir="src" includes="**/build.xml"/>
</subant>
</target>
Short answer: Not really. There are ways around this, but I prefer the ant-contrib <for/> task for clarity and simplicity. With the <local/> task, you can now localize values of variables. Before, you sometimes had to use ant-contrib's <var/> task to reset the values, so you could loop through them over and over.
<for param="directory">
<fileset dir="${some.dir}"/>
<sequential>
<local name="foo"/>
<local name="bar"/> <!-- Properties that may change with each iteration -->
<!-- Here be dragons -->
</sequential>
</for>
It's clean, simple, and easy to understand. The big issue many people have with Ant Contrib is that not everyone may have it installed in their $ANT_HOME/lib directory. Far enough. So, if you use ant-contrib, put it as part of your project.
I'll put the ant-contrib jar in ${basedir}/antlib/antcontrib and then put this in my program:
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<fileset dir="${basedir}/antlib/antcontrib"/>
</classpath>
</taskdef>
Now, when someone checks out my project, they have ant-contrib already installed (since it's inside my project) and accessible (since I point my <taskdef> task at the location of ant-contrib.jar in my project).

Resources