How to create new folder named dd.MM.yyyy with ant? - ant

My ant script needs to create a folder that is named
with actual datetime and datetimepattern dd.MM.yyyy

Use tstamp and mkdir task like that :
<project>
<tstamp>
<format property="datetime" pattern="dd.MM.yyyy"/>
</tstamp>
<mkdir dir="C:/whatever/${datetime}">
</project>

Related

How can I extract version from a file name in Ant without using ant contrib?

I am trying to scan a folder in ant script and extract the version out of it, suppose file name is abc-1.0.0.exe/dmg. I want to get the 1.0.0 in Ant. Is there any way I can do without using ant contrib? I only found help with ant contrib which I don't want to use.
You might be able to use something like this, based on the <pathconvert> task with a regexmapper.
Here's the directory structure in this example:
$ find folder
folder
folder/abc-1.0.0.exe
folder/abc-1.0.0.exe/dmg
Here's the Ant buildfile:
<fileset dir="folder" id="folder"/>
<echo message="file is: ${toString:folder}" />
<pathconvert property="version">
<path path="${toString:folder}" />
<regexpmapper from=".*-(.*).exe.*" to="\1" />
</pathconvert>
<echo message="version is: ${version}" />
Output:
[echo] file is: abc-1.0.0.exe/dmg
[echo] version is: 1.0.0

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

Ignore full path for specific files when running 'tar' task in ant

I have my proj structure like this
myproj
-bin/myproj-run
-bin/myproj-cli
-lib/x.jar
-target/myproj.jar
I want to create a tar.gz that looks like
myproj
-bin/myproj-run
-bin/myproj-cli
-lib/x.jar
-myproj.jar
How do I do it using ant tar task ?
This one should work (I've tested this on my computer and it works):
<project name="tar.test" default="tartest">
<dirname property="basedir" file="${ant.file.tar.test}" />
<target name="tartest">
<tar destfile="${basedir}/files.tar">
<tarfileset dir="${basedir}/myproj">
<exclude name="target/**" />
</tarfileset>
<tarfileset dir="${basedir}/myproj/target"/>
</tar>
</target>
</project>
BTW, you should read tar manual on apache page, you will find there few examples which shoud help you.
I would first <copy> the files to some other directory (e.g., one called 'package') with the folder structure that you want. Then, run <tar> and <gzip>:
<tar destfile="myTar.tar" basedir="package" />
<gzip src="myTar.tar" destfile="myTar.tar.gz"/>

How to create a Java file dynamically into certain package using Ant concat task?

I have an Ant script file in which I use concat task to create a Java cource file in the specified package which is defined in a properties file.
For example, I define the package name:
ma.package=com.my.package
In Ant script, I call:
<concat destfile="./${prject.root}/${ma.package}/MyClass.java">
However, MyClass.java was created in a subfolder com.my.package, instead of folder structure com\my\package. How to fix it?
I use Eclipse Helios under Windows XP.
You can use a PathConvert with a nested UnpackageMapper to convert the package name to a path. For example:
<project default="test">
<property name="ma.package" value="com.my.package"/>
<target name="test">
<pathconvert property="ma.package.dir">
<path path="${ma.package}"/>
<unpackagemapper from="*" to="*"/>
</pathconvert>
<echo message="ma.package : ${ma.package}"/>
<echo message="ma.package.dir : ${ma.package.dir}"/>
</target>
</project>
The output is:
Buildfile: C:\tmp\ant\build.xml
test:
[echo] ma.package : com.my.package
[echo] ma.package.dir : C:\tmp\ant\com\my\package
So you could use the converted property value in your concat:
<concat destfile="${ma.package.dir}/MyClass.java">

How to turn directories to .jar files with Ant

How to turn a list of directories into a list of .jar files ?
It seems that using Copy Task and Mappers is the right way to do this but I did not find any nested element creating a jar file from a directory.
Of course, the list of directories would be computed and not hard coded in the ant script.
For instance, a foo directory contains bar1, bar2 and bar3 directories. The script would create bar1.jar from the bar1 dir, bar2.jar from bar2 dir and bar3.jar from bar3 dir
The script would create a bar4.jar from a newly added bar4 directory without any change in the script (no hardcoded list).
Thanks ahead for your help
You can use the subant task to do this. For example:
<project name="jars" default="jar" basedir=".">
<property name="dir.build" value="${basedir}/build"/>
<property name="dir.root" value="${basedir}/foo"/>
<target name="init">
<tstamp/>
<mkdir dir="${dir.build}"/>
</target>
<target name="jar" depends="init">
<!-- ${ant.file} is the name of the current build file -->
<subant genericantfile="${ant.file}" target="do-jar">
<!-- Pass the needed properties to the subant call. You could also use
the inheritall attribute on the subant element above to pass all
properties. -->
<propertyset>
<propertyref name="dir.build"/>
</propertyset>
<!-- subant will call the "do-jar" target for every directory in the
${dir.root} directory, making the subdirectory the basedir. -->
<dirset dir="${dir.root}" includes="*"/>
</subant>
</target>
<target name="do-jar">
<!-- Get the basename of the basedir (bar1, bar2, etc.) -->
<basename file="${basedir}" property="jarname"/>
<jar jarfile="${dir.build}/${jarname}.jar" basedir="${basedir}"/>
</target>
</project>
You could use the appropriate Jar task, along with the foreach ant-contrib extension task.
A sample:
<foreach list="${hmProjectsList}" delimiter="/,"
target="cvsCheckOut" param="hmProjectDir" inheritall="true" />

Resources