Working with ANT and facing an issue - java

<target name="init">
<mkdir dir="${build.dir}" />
<if>
<available file="../war" type="dir"/>
<then></then>
<else>
<mkdir dir="../war" />
</else>
</if>
</target>
This is the code i am using to check if a folder exists, but getting the following error:
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any / declarations have taken place.
I have copied ant-contrib.jar in ANT_HOME/lib. where am i goin wrong?

Given the example above, you can greatly simplify it:
<target name="init">
<mkdir dir="${build.dir}" />
<mkdir dir="../war" />
</target>
...since the mkdir task does nothing if the folder exists (see documentation).
If you're asking how to use if and then in ant, I recommend picking another example since each action in Ant tends to have its own conditionals built in.

Related

Problems with Ant build.xml configuration to work with external Libraries and Java property files

I have a problem with Ant Build Tool.
First, below you can see my project structure:
and the content of my build.xml file is:
<?xml version="1.0" encoding="UTF-8"?>
<project name="addonGenerator" default="main" basedir=".">
<property name="projectName" value="addonGenerator"/>
<property name="src.dir" location="src"/>
<property name="build.dir" location="bin"/>
<property name="dist.dir" location="dist"/>
<target name="compile" description="compile the source ">
<mkdir dir="${build.dir}"/>
<javac srcdir="${src.dir}" destdir="${build.dir}">
<classpath>
<pathelement path="lib/velocity-1.7.jar"/>
<pathelement path="lib/log4j-1.2.16.jar"/>
</classpath>
</javac>
</target>
<target name="dist" description="package, output to JAR">
<mkdir dir="${dist.dir}"/>
<jar jarfile="${dist.dir}/${projectName}.jar" basedir="${build.dir}">
<zipgroupfileset dir="lib" includes="velocity-1.7.jar" />
<zipgroupfileset dir="lib" includes="log4j-1.2.16.jar" />
<manifest>
<attribute name="${projectName}" value="main"/>
<attribute name="Main-Class" value="main.java.AddonGenerator"/>
</manifest>
</jar>
</target>
<target name="clean" description="clean up">
<delete dir="${build.dir}"/>
<delete dir="${dist.dir}"/>
</target>
<target name="main" depends="clean, compile, dist"/>
</project>
I don't know how setup the Ant build.xml to build and run my project with external libraries and the java property file generator.properties
To include your generator.properties file in the .jar file, add your resources directory when building the .jar:
<jar jarfile="${dist.dir}/${projectName}.jar" basedir="${build.dir}">
<fileset dir="src/main/java/resources"/>
Since you are currently building a “fat jar” (by directly including the contents of your library .jars in your application .jar), you can run by simply invoking your .jar file. Such a target obviously requires the .jar file to be built, so it makes sense to depend on the "dist" target:
<target name="run" depends="dist">
<java jar="${dist.dir}/${projectName}.jar"/>
</target>
On another note, I don’t think you want to pass src as your source directory, unless your classes actually declare themselves with ‘package main.java;’ (which they shouldn’t). You should pass the actual root of your packages to the javac task:
<property name="src.dir" location="src/main/java"/>
You should also make the "dist" target depend on "compile", since, well, it depends on having compiled classes available.
I also would suggest that your default target, "main", avoid calling the "clean" target. You should not clean before every single build; that defeats one of the most useful benefits of Ant, namely the ability to update only the things that need to be updated. You should only clean when you need to, with a command like ant clean compile or simply ant clean.
Note that once "dist" depends on "compile", and once "main" no longer calls "clean", you can simply remove the "main" target and change your project’s default target to "dist". When you think about it, this makes sense: the default action is to build and package the application.

Using Ant condition tag

I want to do something like this
<target name="init" description="Create build directories.
>
<mkdir dir="bin" />
<mkdir dir="dist" />
<!-- Check for the presence of dependent JARs -->
<condition property="dependent.files.available">
<and>
<available file="../../xx.jar" />
<available file="../../yy.jar" />
</and>
</condition>
</target>
<!--- Compile -->
<target name="compile" depends="init" description="Compile java sources
and create classes">
<if>
<isset property="dependent.files.available"/>
<then>
<javac srcdir="src" destdir="bin" classpathref="ext_jar_classpath"/>
</then>
<else>
<echo message="Either xx.jar or yy.jar not found"/>
</else>
</if>
</target>
When I tried to compile the code it gave me the following error
Problem: failed to create task or type if
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place
It it the correct way of doing it?
You need to have the ant-contrib jar visible at runtime, or configure it correctly as described in the link.
The thing boils down to have Ant load the task definition, so if you put ant-contrib in ant/lib, you just need
<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
Just a suggestion. You can do the same thing with ant 1.9.1 onwards using if/unless without ant-contrib (http://ant.apache.org/manual/targets.html)
Your targets would be
<target name="init" description="Create build directories.
>
<mkdir dir="bin" />
<mkdir dir="dist" />
<!-- Check for the presence of dependent JARs -->
<condition property="dependent.files.available">
<and>
<available file="../../xx.jar" />
<available file="../../yy.jar" />
</and>
</condition>
</target>
<!--- Compile -->
<target name="compile" depends="init" description="Compile java sources
and create classes" if="dependent.files.available">
<javac srcdir="src" destdir="bin" classpathref="ext_jar_classpath"/>
</target>

Ant: How to know if at least one copy task from ten copy tasks actually copied a file

I have a target which has several copy tasks; It basically copies the common jars to our set of applications from a centralized location to the lib folder of the application.
Seeing as this is a regular copy task a jar will be copied only if it is newer than the one currently in the lib folder.
This is the relevant part in the build.xml:
<target name="libs"/>
<copy file=... />
<copy file=... />
<copy file=... />
<antcall target="clean_compiled_classes"/>
</target>
<target name="clean_compiled_classes" if="anyOfTheLibsWereCopied">
<delete .../>
</target>
I'm looking for a way to set the anyOfTheLibsWereCopied property before the ant call in the libs target based on whether or not any of the files has been actually changed.
Thanks,
Ittai
I would advise having a look at the Uptodate task. I have never used it before but I guess what you are trying to do will be implemented along the following lines:
<target name="libs"/>
<uptodate property="isUpToDate">
<srcfiles dir="${source.dir}" includes="**/*.jar"/>
<globmapper from="${source.dir}/*.jar" to="${destination.dir}/*.jar"/>
</uptodate>
<!-- tasks below will only be executed if
there were libs that needed an update -->
<antcall target="copy_libs"/>
<antcall target="clean_compiled_classes"/>
</target>
<target name="copy_libs" unless="isUpToDate">
<copy file=... />
<copy file=... />
<copy file=... />
</target>
<target name="clean_compiled_classes" unless="isUpToDate">
<delete .../>
</target>
Your other option would be to implement your own ant task that does what you want. This would require a bit more work though.

Conditionally Delete in Ant

I want to delete the directory if the property "delete-compiled-dir" is set to true. If the property is false then do not execute it. Right now I have
<target name="deleted-after-compilation" depends="compile,jar">
<condition property="${delete-compiled-dir}" value="true">
<delete dir="${compilation-dir}" />
</condition>
<echo> Deleting Compiled Directory Classes </echo>
</target>
I get the error message :
condition doesn't support the nested "delete" element.
You can add the condition to your target using if (see manual).
The target will only be executed when the property compilation-dir is set (to any value, e.g. false).
<target name="deleted-after-compilation" depends="compile,jar"
if="${compilation-dir}">
<delete dir="${compilation-dir}" />
<echo> Deleting Compiled Directory Classes </echo>
</target>
To only execute it when a property is set to true, you need to set another property first and check this one in the if. You could add both as dependency the another target:
<target name="set-delete-property">
<condition property="delete-compilation-dir">
<istrue value="${compilation-dir}"/>
</condition>
</target>
<target name="deleted-after-compilation"
depends="compile,jar" if="${compilation-dir}">
....
<target name="some-target"
depends="set-delete-property,deleted-after-compilation">
</target>
There are a few ways to do this:
Use conditions on the target entity
Targets can contain if and unless conditions. The target will execute depending whether or not the property is set. (Not set to true, just set). This is a common way to see if you need to do something or not:
<target name="deleted.after.compilation"
if="delete.compiled.dir"
depends="jar">
<delete dir="${compilation-dir}" />
<echo> Deleting Compiled Directory Classes </echo>
</target>
You can set the property on the command line:
$ ant -Ddelete.compiled.dir all
Note: I use periods as separators for the names of properties and targets. Also note that I only depend upon the target jar since jar is also dependent upon compile, there's no need to have them both.
Use Ant's 1.9.1 conditional clauses
As of Ant 1.9.1, Ant has conditional attributes that can be added to tasks. You need to add a Namepsace declaration in your <project> entity:
<project ...
xmlns:if="ant:if"
xmlns:unless="ant:unless">
<target name="deleted.after.compilation"
depends="jar">
<delete dir="${compilation-dir}" if:true="${delete.compiled.dir}"/>
<echo if:true="${delete.compiled.dir}"> Deleting Compiled Directory Classes </echo>
</target>
Use Ant-Contrib's If Statement
Ha ha, that wacky Ant-Contrib library. No one knows who maintains it, and it hasn't been touched in years, but many people depend so heavily on it.
<project ...>
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<fileset dir="${ivy.dir}/antcontrib">
<include name="ant-contrib*.jar"/>
</fileset>
</classpath>
</taskdef>
<target name="deleted.after.compilation"
depends="jar">
<if>
<istrue value="${delete.compiled.dir}"/>
<then>
<delete dir="${compilation-dir}"/>
<echo>Deleting Compiled Directory Classes </echo>
</then?
</if>
</target>
You can see why Ant-Contrib is popular. It contains a lot of power, and we all know it. Plus, if someone is still using Ant 1.8 or 1.7, this will still work.
if you have get the property ,you can just use it in a target.
<target name="delete" if="${delete-compiled-dir}">
<delete dir="${compilation-dir}" />
</target>
As of Ant 1.9.1, you can use conditionals on any task. Described here.
Add this namespace to your project element:
<project name="yourproject" xmlns:if="ant:if">
Then add this to your delete:
<delete dir="${compilation-dir}" if:true="${delete-compiled-dir}"/>

How do I build a list of file names?

I need to write an Ant target that appends together (comma-delimited) a list of '.jar' file names from a folder into a variable, which is later used as input to an outside utility.
I am running into barriers of scope and immutability. I have access to ant-contrib, but unfortunately the version I am stuck with does not have access to the 'for' task. Here's what I have so far:
<target name="getPrependJars">
<var name="prependJars" value="" />
<foreach param="file" target="appendJarPath">
<path>
<fileset dir="${project.name}/hotfixes">
<include name="*.jar"/>
</fileset>
</path>
</foreach>
<echo message="result ${prependJars}" />
</target>
<target name="appendJarPath">
<if>
<equals arg1="${prependJars}" arg2="" />
<then>
<var name="prependJars" value="-prependJars ${file}" />
</then>
<else>
<var name="prependJars" value="${prependJars},${file}" />
</else>
</if>
</target>
It seems that 'appendJarPath' only modifies 'prependJars' within its own scope. As a test, I tried using 'antcallback' which works for a single target call, but does not help me very much with my list of files.
I realize that I am working somewhat against the grain, and lexical scope is desirable in the vast majority of instances, but i really would like to get this working one way or another. Does anybody have any creative ideas to solve this problem?
You might be able to use the pathconvert task, which allows you to specify the separator character as comma.
<target name="getPrependJars">
<fileset id="appendJars" dir="${project.name}/hotfixes">
<include name="*.jar" />
</fileset>
<pathconvert property="prependJars" refid="appendJars" pathsep="," />
<echo message="prependJars: ${prependJars}" />
</target>
I'd simply write a custom task in Java that (1) takes the folder name, (2) assembles the result string and (3) stores it to the ${prependJars} property.
In ant you just define the task (taskdef) and use like all other tasks afterwards.
I did it once when I was faced with a simliar problem and found that it was very, very easy.
Here's the tutorial.
If a system path format is useful to you, you can use the following:
<target name="getPrependJars">
<path id="prepend.jars.path">
<fileset dir="${project.name}/hotfixes">
<include name="*.jar"/>
</fileset>
</path>
<property name="prependJars" value="${toString:prepend.jars.path}" />
<echo message="result ${prependJars}" />
</target>

Categories