Ant archiving subfolders - ant

I have next trouble. I have one folder("FirstFolder") and 3 subfolders("1", "2", "3"). I want to archive this folders in next - 1.acp, 2.acp, 3.acp and placed in other folder. It's important - I do not know what are called sub-folders and their number!!! I don't found solutions for this and write simple ant task-
<target name="start">
<foreach target="zipAcp" param="Files">
<path>
<dirset dir="src/main/bootstrap"/>
</path>
</foreach>
</target>
<target name="zipAcp">
<zip destfile="target/classes/alfresco/extension/agilent/${Files}.acp" basedir="src/main/bootstrap"/>
</target>
But i don't know how get directory name(1), but not full path as now(D:\test\1).

You can use the basename task for getting directory name.

Related

How to compile each jasper file to its own directory?

I'm trying to create a ant script to compile my jasper files, but I have many "srcdir" and "destdir":
<target name="all">
<jrc
srcdir="many..."
destdir="many..."
tempdir="any"
xmlvalidation="true">
<classpath refid="classpath"/>
<include name="**/*.jrxml"/>
</jrc>
</target>
...and I would like it to compile each file to it's own dir. For every ".jrxml" file.
Is there a way?
You can use ant-contrib foreach task to loop over each jrxml file and call the jrc task for each of those. If you don't have it, you'll need to install ant-contrib by copying its JAR file to the lib directory of your Ant installation (if you're using Eclipse, you can add it by going to "Window > Preferences > Ant > Runtime" and adding the JAR into "Ant Home Entries").
The following defines a target "all" that will select all the jrxml files under the current directory. For each of those file, the "jrc" target will be called and the corresponding file will be referenced by the property jrxml.file.
Inside this task, the directory where the jrxml file is located is retrieved with the dirname task and the name of the jrxml file is retrieved with the basename task. The built .jasper file will be created under a folder having the same name as the jrxml file. (It needs to be created first with the mkdir task).
<taskdef resource="net/sf/antcontrib/antcontrib.properties" />
<target name="all">
<foreach target="jrc" param="jrxml.file">
<path>
<fileset dir=".">
<include name="**/*.jrxml"/>
</fileset>
</path>
</foreach>
</target>
<target name="jrc">
<dirname property="jrxml.dir" file="${jrxml.file}"/>
<basename property="jrxml.filename" file="${jrxml.file}" suffix="jrxml"/>
<mkdir dir="${jrxml.dir}/${jrxml.filename}"/>
<jrc srcdir="${jrxml.dir}"
destdir="${jrxml.dir}/${jrxml.filename}"
tempdir="${jrxml.dir}/${jrxml.filename}"
xmlvalidation="true">
<classpath refid="classpath"/>
<include name="${jrxml.filename}.jrxml"/>
</jrc>
</target>
As an example, if you have a structure:
+folder
+--jrxml
+----Example1.jrxml
+----Example2.jrxml
the result will be
+folder
+--jrxml
+----Example1.jrxml
+----Example1
+------Example1.jasper
+----Example2.jrxml
+----Example2
+------Example2.jasper

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>

Copy entire directory before creating an ear file

I am trying to build an ear file from an ant script. There are several targets that create .jar and .war (to be contained within the ear) files using different projects and these are building without issue so I will not include them.
Imagine this directory structure:-
Project1/
build/
lib/
META-INF/
build.xml
So when the ant script is called the build directory is deleted and remade, all fairly standard stuff. Then I create the jar's and war's from external projects and store them in build/ - everything is fine.
But I also want to include the directories lib/ and META-INF/ in the ear file. So I try to copy them to the build directory using this target.
<target name="file_cleanup">
<copy todir="${build}">
<fileset dir="lib/"/>
<fileset dir="META-INF/"/>
</copy>
</target>
This file_cleanup target is a dependant of the default build target which creates the ear - shown below:
<target name="ear" depends="initialise, file_cleanup, other targets...">
<ear destfile="My.ear" appxml="META-INF/application.xml">
<fileset dir="${build}" includes="*.jar,*.war"/>
</ear>
</target>
What I want to see when I extract the ear is:
target1.jar
target2.war
lib/
META-INF/
But what I actually get is:
target1.jar
target2.war
and all of the contents of both the lib and META-INF directories...
I was able to resolve this issue by creating additional properties and directories and copying the directory structures to the new directories:
<target name="initialise">
<delete dir="${build}"/>
<mkdir dir="${build}"/>
<mkdir dir="${build}/${lib}"/>
<mkdir dir="${build}/${meta-inf}"/>
</target>
<target name="file_cleanup">
<copy todir="${build}/${lib}">
<fileset dir="lib"/>
</copy>
<copy todir="${build}/${meta-inf}">
<fileset dir="META-INF"/>
</copy>
</target>

Ant build script not seeing refid

I have a build.xml file that includes a common.xml file that defines some refid values. However, my task cannot see the refid value. I have not been able to find a solution on the web and am looking for some help.
I call the genbeans target in the build.xml file. It fails on the xmlbean taskdef with the message Reference my_classpath_jars not found.
build.xml
----------------------------
[includes common.xml]
**my_classpath_jars fails to be seen at this point - defined in common.xml**
<taskdef name="xmlbean" classname="org.apache.xmlbeans.impl.tool.XMLBean">
<classpath refid="my_classpath_jars"/>
</taskdef>
<!-- Generate the XMLBeans java code from our source XSD file(s) -->
<target name="genbeans" description="Generate XML Bean files" depends="build_my_jar_cpath">
<mkdir dir="${lib}"/>
<xmlbean destfile="${lib}/${appname}Beans.jar" failonerror="true">
<classpath refid="my_classpath_jars"/>
<fileset dir="src/XSD Files" includes="*.xsd, *.wsdl"/>
</xmlbean>
</target>
common.xml
-----------------------------
<target name="build_my_jar_cpath">
<path id="my_classpath_jars">
<fileset dir="${jardir}" includes="**/*.jar" />
</path>
<pathconvert pathsep="${path.separator}" property="myjar.clpath" refid="my_classpath_jars"/>
</target>
When in doubt, use the ant -d switch when calling your target. You'll see a ton of output. Save it to a file and parse through it.
Do that, and the first thing you'll notice in the output is that it's defining your taskdefbefore you have defined your my_classpath_jars. That my_classpath_jars refid is only set when you call that greenbeans target. Your <taskdef> is executed before any of your targets are called.
Either take the definition of my_classpath_jars out of the target greenbeans, or put your <taskdef> in there.

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

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.

Resources