if ant else

Here is what I think is the best way to perform one or the other targets in an ant file depending on if a property is set or not.
Let us pretend that if the mainclass property has a value we want to create an executable JAR-file, if not set we will create a plain JAR-file.

<!-- =================================================================
Create a jar-file of the projects classes
================================================================== -->
depends="plain-jar, executable-jar"
description="--> Create a JAR-file of the project classes."

<target name="executable-jar" if="mainclass">

<target name="plain-jar" unless="mainclass">

It is dead simple but still, I've seen a lot of strange antcall with conditions and stuff in the wild to acheive the same thing.

Another little smippet that took a while to get right is to create a class-path in the MANIFEST.MF based on an ANT-path.
This is the best way I have found so far, the silly thing is that it is different if building on windows or on a real operating system.

<pathconvert dirsep="/" pathsep=" " property="manifest-classpath" refid="build.classpath">
The regexp when building on unix is:
from="\\([^\\]*.jar)\z" to="lib/\1"

This will set the manifest-classpath property with a value that is suitable for writing in the Manfest attribute Class-Path. In this example it will prefix all path-entries with a lib/ but you can take it from here yourselves :-)

Blog status
It is a sad state of affairs; My friend still has not started a blog.

No comments:

Post a Comment