Problem : ant unable to recognize for task - ant

I am trying to iterate over all the files in a ant however ant seems to be unable to recognize for task.
I have installed ant-contrib on a location and have provided the path in the build file.
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="${basedir}\build\ant-contrib.jar"/>
</classpath>
</taskdef>
<for param="file">
<path>
<fileset dir="${basedir}/src/objects"/>
</path>
<sequential>
perform operations here.
</sequential>
</for>
</target>
Problem: failed to create task or type for
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.

First of all, I would suggest avoiding the use of ant-contrib if at all possible. If you have anything resembling a normal build, native Ant can probably handle it better. Feel free to post a higher level description what you're trying to do here and I can help you out with it.
That said, your task isn't recognized because antcontrib.properties doesn't define it, for some strange reason. Instead, try pointing the resource attribute to antlib.xml.
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="${basedir}\build\ant-contrib.jar"/>
</classpath>
</taskdef>

Related

Ant for script how to continue if any iteration fails

I writing an ant build file to check all translate files in a directory.
I want the ant for script to continue checking the rest files if an checking error in any file appear.
My ant task:
<taskdef name="validate" classname="ValidateTranslateFile">
<classpath>
<pathelement location="${ant-libs-dir}/TranslateFileUtilities.jar" />
<pathelement location="../web/WEB-INF/lib/commons-io-2.5.jar" />
<pathelement location="../web/WEB-INF/lib/commons-lang3-3.5.jar" />
</classpath>
</taskdef>
<for param="program">
<path>
<fileset dir="../web/WEB-INF" includes="*.txt" />
</path>
<sequential>
<validate targetFile="#{program}" checkLanguages="true" checkKeysOrder="true" />
</sequential>
</for>
</target>
the Result:
it checks the files till the first error appear, and then the BUILD FAILED.
Could any one help me with that?
The for task is not part of standard ANT. It is a 3rd party extension documented here:
http://ant-contrib.sourceforge.net/tasks/tasks/for.html
The documentation suggests using a "keepgoing" attribute to ignore errors, this might be the answer you're looking for.
If this is a custom ANT task you are writing, perhaps you should consider refactoring the code to operate on a fileset? This would enable you to call task as follows:
<validate checkLanguages="true" checkKeysOrder="true">
<fileset dir="../web/WEB-INF" includes="*.txt" />
</validate>
Simpler and more robust.

Trying to make this target conditional

This is what I'm trying to do:
<target name="example">
<if>
<equals arg1="${var}" arg2="true" />
<then>
<exec executable="/bin/bash">
<arg value="script.sh" />
</exec>
</then>
</if>
</target>
And when I execute the target with ant example I get this 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.
I really need help in here. I would really appreciate it
That is because, if is custom task from ant-contrib library.
Make sure, ant-contrib library in your class path. Binaries can be downloaded from here. Extract the ant-contrib jar file to a location. And specify the same in below taskdef.
Then add the taskdef at the beginning of the build.xml. Refer documentation for more details.
For eg:
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="/absolute/path/to/ant-contrib-1.0b3.jar" />
</classpath>
</taskdef>

Ant Throws Reference Error for Existing Jar File

I want to add task to my build.xml file:
<target name="forbidden-checks" depends="download-forbidden-checks">
<taskdef name="forbidden-apis" classname="de.thetaphi.forbiddenapis.AntTask"
classpathref="lib/forbiddenapis-2.2.jar"/>
<fa:forbiddenapis classpathref="build.classpath" dir="${build.dir}" targetVersion="${jdk.version}">
<bundledsignatures name="jdk-unsafe"/>
<bundledsignatures name="jdk-deprecated"/>
<bundledsignatures name="jdk-non-portable"/>
</fa:forbiddenapis>
</target>
I see that jar is downloaded, there is no problem. However I get an error:
download-forbidden-checks:
setup-maven-url:
download-via-maven:
[get] Getting: https://repo1.maven.org/maven2/de/thetaphi/forbiddenapis/2.2/forbiddenapis-2.2.jar
[get] To: /home/kamaci/pro/lib/forbiddenapis-2.2.jar
forbidden-checks:
BUILD FAILED
/home/kamaci/pro/build.xml:2391: Reference /home/kamaci/pro/lib/forbiddenapis-2.2.jar not found.
I checked if I have a problem with the path of jar. So, I've put forbiddenapis-2.2.jar to another place than the project. I've pointed that jar from my taskef but I got same error.
Any ideas?
EDIT 1:
I've changed it to that:
<target name="forbidden-checks" depends="download-forbidden-checks">
<taskdef name="forbidden-apis" classname="de.thetaphi.forbiddenapis.AntTask">
<classpath>
<pathelement location="/home/kamaci/pro/forbiddenapis-2.2.jar"/>
</classpath>
</taskdef>
<fa:forbiddenapis targetVersion="${jdk.version}">
<classpath>
<pathelement location="lib/forbiddenapis-2.2.jar"/>
</classpath>
<bundledsignatures name="jdk-unsafe"/>
<bundledsignatures name="jdk-deprecated"/>
<bundledsignatures name="jdk-non-portable"/>
</fa:forbiddenapis>
</target>
However, it didn't work:
Problem: failed to create task or type antlib:de.thetaphi.forbiddenapis:forbiddenapis
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.
No types or tasks have been defined in this namespace yet
This appears to be an antlib declaration.
Action: Check that the implementing library exists in one of:
-/usr/share/ant/lib
-/home/kamaci/.ant/lib
-a directory added on the command line with the -lib argument
When I run this:
ant forbidden-checks -lib lib/
it works. My project definition starts with that at build.xml:
Why I should add -lib? Is there any way to avoid it?
Pretty sure the issue is the classpathref as it is using a literal value and not a path element, so the jar may exist just not on the classpath, try creating a path element to use for the taskdef
<path id="lib.path">
<fileset dir="lib" includes="lib/*.jar"/>
</path>
<taskdef name="forbidden-apis" classname="de.thetaphi.forbiddenapis.AntTask"
classpathref="lib.path"/>
or defining the classpath inside the taskdef.
<taskdef name="forbidden-apis" classname="de.thetaphi.forbiddenapis.AntTask">
<classpath>
<pathelement location="lib/forbiddenapis-2.2.jar"/>
</classpath>
</taskdef>

Could not load definitions from resource when using ant task

I'm using ant-contrib in my build script
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="ant/ant-contrib-0.6.jar"/>
</classpath>
</taskdef>
It works. But when I'm calling some target in this script from another ant file using ant task, I'm getting the error.
<ant antfile="build.xml" target="make" dir="${client.project.location}/ant"/>
Please, help me to fix the problem. Thanks
In your code we can see
<classpath>
<pathelement location="ant/ant-contrib-0.6.jar"/>
</classpath>
You need to give path location="/home/[some
path]/ant-contrib-0.6.jar"
you can do one more thing,you need to copy your ant-contrib-0.6.jar
into ANT_HOME/lib folder and remove your tag completely from you code.
Note: Second option is always better.
I had to set usenativebasedir="true" when calling ant task

Apache ant does not recognize 'for' task/macro, although I have added ant-contrib via taskdef

I am getting following while doing ant build:
Build\build.xml:247: Problem: failed to create task or type
for
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.
build.xml line 247 is <for param="file">
Already defined <taskdef resource="net/sf/antcontrib/antcontrib.properties"/>, it didn't work. Then I specifically added following but it is still not working.
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="${env.ANT_HOME}/lib/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
I have ant-contrib-1.0b3.jar at C:\Softwares\apache-ant-1.8.4\lib directory. What is missing here?
If you placed the AntContrib jar in $ANT_HOME/lib directory, all you really need to do is this:
<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
Actually to use the <for/> task, you need to do this:
<taskdef resource="net/sf/antcontrib/antlib.xml"/>
Note you have to use antlib.xml and not antcontrib.properties. Read the Installation directions very carefully. It's easy to miss.
If you are doing this in a group project, I recommend that you put your ant-contrib.jar in your project. THen add them to your project in your version control system. That way, other developers can use your build with the ant-contrib tasks without downloading the ant-contrib jar and installing it in their $ANT_HOME directory themselves.
Let's say you create a directory called ant-contrib.dir and put that in the root of your project, then put the ant-contrib jar in that folder. Just put this in your project:
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<fileset dir="${basedir}/ant-contrib.dir"/>
</classpath>
</taskdef>
Ant needs to be aware of the the dependency. The following is a more succinct version of David W's answer. Add the equivalent of the following to your ant project:
<taskdef resource="net/sf/antcontrib/antcontrib.properties" classpath="relative/path/to/ant-contrib-1.0b3.jar"/>
<taskdef resource="net/sf/antcontrib/antlib.xml" classpath="relative/path/to/ant-contrib-1.0b3.jar"/>

Resources