where to find missing optional ant tasks? - ant

I wanted to have a look which system properties are set here (and to which values), so the easiest way (if not writing a new Java program here) would be adding some lines to my ant build script:
<target name="properties">
<echoproperties/>
</target>
But running ant gives my this error message:
/u/ebermann/projektoj/stackoverflow-examples/build.xml:19: Problem: failed to create task or type echoproperties
Cause: the class org.apache.tools.ant.taskdefs.optional.EchoProperties was not found.
This looks like one of Ant's optional components.
Action: Check that the appropriate optional JAR exists in
-/usr/share/ant/lib
-/u/ebermann/.ant/lib
-a directory added on the command line with the -lib argument
Do not panic, this is a common problem.
The commonest cause is a missing JAR.
This is not a bug; it is a configuration problem
Okay, so I don't panic, but wonder what to do.
I have Ant 1.7.1 here (an OpenSUSE system), and sadly no documentation for this version, and I'm not root to install either a current ant version or the documentation for the old version (I just downloaded it and it still does not say which jar file is needed here). Of the directories listed above, only /usr/share/ant/lib exists, but it contains nothing like optional.
I would want to download the necessary jar file and put it in my home directory, but where to find it? The ant download archive contains nothing like that, and I have no idea where else to search. (I did google a bit, but did not find anything.
So, can someone give me some pointers where to find the right jar file?
(I suppose the solution is quite easy, and something is just blocking my view.)
After vahapt's answer, I downloaded the file from the apache repository, and put it into the directory /u/ebermann/.ant/lib mentioned by the error message. Running ant properties again - the same result as above.
$ jar -tf /u/ebermann/.ant/lib/ant-nodeps-1.7.1.jar | grep 'EchoProperties.class'
org/apache/tools/ant/taskdefs/optional/EchoProperties.class
This looks like it should work - is the error message simply wrong?
If I put it directly into the CLASSPATH, it works:
$ CLASSPATH=/u/ebermann/.ant/lib/ant-nodeps-1.7.1.jar ant properties
Buildfile: build.xml
properties:
[echoproperties] #Ant properties
[echoproperties] #Thu Mar 10 00:46:22 CET 2011
...
[echoproperties] user.name=ebermann
[echoproperties] user.timezone=
BUILD SUCCESSFUL
Total time: 0 seconds
I don't want to change my normal CLASSPATH variable, and it should work by putting it into this directory, or did I understand something wrong?
Any ideas, or is this an ant bug?
(Also, why is this file nowhere mentioned in the ant documentation?)
Edit:
After the answer from vahapt, my ant build-file looks like this:
<project name="stackoverflow-examples" basedir=".">
<target name="echoproperties.prepare">
<available property="echoproperties.works"
classname="org.apache.tools.ant.taskdefs.optional.EchoProperties"
/>
</target>
<target name="echoproperties.init"
depends="echoproperties.prepare"
unless="echoproperties.works">
<taskdef name="echoproperties" classname="org.apache.tools.ant.taskdefs.optional.EchoProperties">
<classpath>
<fileset dir="${user.home}/.ant/lib">
<include name="ant-nodeps.jar" />
</fileset>
</classpath>
</taskdef>
</target>
<target name="properties" depends="echoproperties.init">
<echoproperties/>
</target>
</project>
This re-registers the task only if it is not already in the ant classpath. (Thus it should also work for complete ant installations which do not have this file in the home directory).
I would still say that This is not a bug; it is a configuration problem is not totally right, even more as putting the file in the indicated directory does not help.
One more interesting observation: The nodeps.jar in ${user.home}/.ant/lib (i.e. now /u/ebermann/.ant/lib/ant-nodeps.jar) is already in the class path (the one shown by ${java.class.path}, but this seems not to help for <echoproperties> to work without this taskdef.
So, this works too:
<target name="echoproperties.init"
depends="echoproperties.prepare"
unless="echoproperties.works">
<taskdef name="echoproperties"
classname="org.apache.tools.ant.taskdefs.optional.EchoProperties"
classpath="${java.class.path}" />
</target>

When you make a google search, results point to ant-nodeps-1.7.1.jar
Make sure that jar exists and you've added it into the classpath
For the second part of your question:
SOLUTION 1. You do not need to modify your CLASSPATH variable. Instead you might add it by adding the parameter -cp [JAR FILE LOCATION] (-cp is for "java" executable)
SOLUTION 2. Jar files are simply zip files, open ant-nodeps.jar copy its content to ant.jar throw away ant-nodeps.jar
SOLUTION 3. See the sample below. taskdef is a ant feature that loads a jar or a class into ClassLoader hierarchy. (You load the class before using it, works like a charm)
<?xml version="1.0" encoding="ASCII"?>
<project name="Test" default="properties" basedir=".">
<target name="properties" depends="init">
<echoproperties/>
</target>
<target name="init">
<taskdef name="echoproperties" classname="org.apache.tools.ant.taskdefs.optional.EchoProperties">
<classpath>
<fileset dir="${ant.library.dir}">
<include name="ant-nodeps.jar" />
</fileset>
</classpath>
</taskdef>
</target>
</project>

I downloaded Ant 1.7.1 and looked in the documentation that came with it. There it described echoproperties as an optional task, but didn't mention where to get the jarfile for this optional task.
Looking inside the lib folder, I discovered the ant-nodeps.jar. Apparently, it was included with Ant 1.7.1.
I would recommend that you download and install Ant 1.8. Since Ant is a Java jar file, it's not really all that difficult to install the latest and greatest version.
I looked on my Mac, and /usr/bin/ant is a link to /usr/share/ant/bin and /usr/share/ant/ is a link to /usr/share/java/ant-1.8.2. So, all I have to do is point /usr/share/ant/bin/ to the correct version of Ant.
In Ant 1.8.2, echoproperties is now a standard task.

Related

Add classpath of third party jar in ant build file

I want to use a third party jar file to be used in my ant project.
I guess that I should add something in build.xml file.
Actually I want to add Jsoup dependency in my build file.
I have read somethings after google search but i could not find out.
I already have tried
<classloader loader="system">
<classpath>
<fileset dir="${basedir}" includes="jsoup-1.6.2.jar"/>
</classpath>
</classloader>
Can someone tell me how can I do this ?
Here is one way to add a jar to your javac classpath.
<javac srcdir="${src}" destdir="${classes}">
<classpath>
<pathelement location="jsoup-1.6.2.jar"/>
</classpath>
</javac>
You could also define the classpath outside the scope of javac (e.g. at top-level of your build file) and then refer to it by id in your javac task.
Thanks for your reply. But later on I managed to solve the problem.
Since this is ant project. There is an ivy.xml so just adding ant dependency of jsoup in ivy.xml works fine.

Ant: "Duplicated project name in import" with imported build file

I have several build files which all import the same base build file, like this:
base.xml:
<project name="base">
<!-- does not define a 'build' target -->
</project>
buildA.xml:
<project name="buildA">
<import file="base.xml" />
<target name="build">
<ant antfile="buildB.xml" target="build"
inheritall="false" inheritrefs="false" />
</target>
</project>
buildB.xml:
<project name="buildB">
<import file="base.xml" />
<target name="build">
...snip...
</target>
</project>
(Module A depends on module B.)
Now, the above calling of B's build target from buildA.xml gives the following error:
Duplicated project name in import. Project base defined first in buildA.xml and again in buildB.xml
Since both buildA.xml and buildB.xml inherit the same base.xml, this seems unavoidable.
How could I get rid of this error?
Based on sudocode's answer, I solved the problem. Because the absolute path to base.xml is different in both cases, Ant does not recognize it as the same file. Even though inheritAll is set to false, the context of the calling task is preserved and this causes the name clash.
To solve this, one can omit the name attribute from base.xml. Since Ant 1.8, the import task has an attribute as, which can be used to reference base targets when the base project is nameless. If you don't override any targets, you can use include instead of import. I'm on 1.7, so that does not help me.
For previous versions of Ant, you can go through an exec call to prevent proliferation of the Ant context entirely (then you get two running Ant instances). Better yet, find a way to import the exact same base.xml (with the same absolute path) in both files.
Are you using Ant 1.6? This resolved Ant bug looks like the same issue.
EDIT
I tried to reproduce the dir structure you refer to in your recent comment.
./base.xml
./buildA
./buildA/buildA.xml
./buildB
./buildB/buildB.xml
And amended the build files accordingly, e.g.
<project name="buildA">
<import file="../base.xml"/>
<target name="build">
<ant antfile="../buildB/buildB.xml" target="build" inheritall="false" inheritrefs="false"/>
</target>
</project>
I still get no build error for the following with ant 1.8.2 or 1.7.1:
ant -f buildA/buildA.xml build

How to solve "Cause: the class org.apache.tools.ant.taskdefs.optional.junit.JUnitTask was not found." while running "ant test"?

I have a target named test and I want to do some tests.
I put here the important parts in build.xml. It includes:
<property name='lib.dir' value='lib' />
<path id='classpath'>
<fileset dir="${lib.dir}" includes="**/*.jar" />
</path>
And I have put the junit.jar and ant-junit.jar(is it a must?) in the lib directory.
However, if I run
ant test.
The output error is:
test:
BUILD FAILED
/home/xiaohan/EclipseWorkSpace/AntTest/build.xml:82: Problem: failed to create task or type junit
Cause: the class org.apache.tools.ant.taskdefs.optional.junit.JUnitTask was not found.
This looks like one of Ant's optional components.
Action: Check that the appropriate optional JAR exists in
-/usr/share/ant/lib
-/home/xiaohan/.ant/lib
-a directory added on the command line with the -lib argument
Do not panic, this is a common problem.
The commonest cause is a missing JAR.
This is not a bug; it is a configuration problem
Additionally, if I put the two jar files in /usr/share/ant/lib with the $ANT_HOME set, it still does not work.
Really thanks for any hints
In my case (using Mint, based on Debian)
sudo apt-get install ant-optional
was the only thing that worked.
The issue was solved on RHEL-based systems once ant-junit was installed:
$ sudo yum install ant-junit
<property name='lib.dir' value='lib' />
<path id='classpath'>
<fileset dir="${lib.dir}" includes="**/*.jar" />
</path>
this has nothing to do with Ant classpath itself. It is properties you can use in your tasks. You have to put jars to the suggested dirs or add command line argument.
Try running it like this:
ant -lib /path/to/the/ant-junit.jar/ test
I was seeing this message because I had missed to include the ant-junit.jar from my IDE's Classpath, e.g. in Eclipse > Right click on your project > Run as > Run Configurations.. > Classpath (tab) > Ensure the ant-junit.jar is there.
When you run JUnit tasks, you must make sure both your classpath that was used for your builds, and the path for the junit jars are added to your classpath.
I prefer to put the junit jars inside my project, so others don't have to install them in their machines for my build to work. I would install them in ${basedir}/antlib/junit
I use ${basedir}/antlib to store all the various Ant build related jars such as the Ant-Contrib jars, JaCoCo, Findbugs, etc. Since this is inside my project, a checkout will always include these jars and the build will work for everyone:
<classpath id="junit.path">
<fileset dir="${basedir}/antlib/junit"/>
</classpath>
This will create a path that contains your JUnit jars. Now to compile your junit tests:
<javac dest="${target.dir}/test-classes"
src=${src.test.java}">
<classpath refid="javac.classpath"/> <!-- Your main build classpath -->
<classpath refid="junit.path"/> <!-- Your JUnit classpath -->
<classpath path="${main.destdir}"/> <!-- These the classes you've built-->
Note that placing the JUnit jars in $ANT_HOME/lib main not work because it depends whether <javac> has includeAntRuntime set or not. It's always highly recommended not to have this set in order to make your build more platform independent.
Also remember to include that junit.path in your <junit> task too.

How can I best share Ant targets between projects?

Is there a well-established way to share Ant targets between projects? I have a solution currently, but it's a bit inelegant. Here's what I'm doing so far.
I've got a file called ivy-tasks.xml hosted on a server on our network. This file contains, among other targets, boilerplate tasks for managing project dependencies with Ivy. For example:
<project name="ant-ivy-tasks" default="init-ivy"
xmlns:ivy="antlib:org.apache.ivy.ant">
...
<target name="ivy-download" unless="skip.ivy.download">
<mkdir dir="${ivy.jar.dir}"/>
<echo message="Installing ivy..."/>
<get src="http://repo1.maven.org/maven2/org/apache/ivy/ivy/${ivy.install.version}/ivy-${ivy.install.version}.jar"
dest="${ivy.jar.file}" usetimestamp="true"/>
</target>
<target name="ivy-init" depends="ivy-download"
description="-> Defines ivy tasks and loads global settings">
<path id="ivy.lib.path">
<fileset dir="${ivy.jar.dir}" includes="*.jar"/>
</path>
<taskdef resource="org/apache/ivy/ant/antlib.xml"
uri="antlib:org.apache.ivy.ant"
classpathref="ivy.lib.path"/>
<ivy:settings url="http://myserver/ivy/settings/ivysettings-user.xml"/>
</target>
...
</project>
The reason this file is hosted is because I don't want to:
Check the file into every project that needs it - this will result in duplication, making maintaining the targets harder.
Have my build.xml depend on checking out a project from source control - this will make the build have more XML at the top-level just to access the file.
What I do with this file in my projects' build.xmls is along the lines of:
<property name="download.dir" location="download"/>
<mkdir dir="${download.dir}"/>
<echo message="Downloading import files to ${download.dir}"/>
<get src="http://myserver/ivy/ivy-tasks.xml" dest="${download.dir}/ivy-tasks.xml" usetimestamp="true"/>
<import file="${download.dir}/ivy-tasks.xml"/>
The "dirty" part about this is that I have to do the above steps outside of a target, because the import task must be at the top-level. Plus, I still have to include this XML in all of the build.xml files that need it (i.e. there's still some amount of duplication).
On top of that, there might be additional situations where I might have common (non-Ivy) tasks that I'd like imported. If I were to provide these tasks using Ivy's dependency management I'd still have problems, since by the time I'd have resolved the dependencies I would have to be inside of a target in my build.xml, and unable to import (due to the constraint mentioned above).
Is there a better solution for what I'm trying to accomplish?
If you are using ANT 1.8+, then you could just import the build.xml directly from the hosted location.
http://ant.apache.org/manual/Tasks/import.html
Since Ant 1.8.0 the task can also
import resources from URLs or
classpath resources (which are URLs,
really). If you need to know whether
the current build file's source has
been a file or an URL you can consult
the property ant.file.type.projectname
(using the same example as above
ant.file.type.builddocs) which either
have the value "file" or "url".
<!-- importing.xml -->
<project name="importing" basedir="." default="...">
<import file="http://myserver/ivy/ivy-tasks.xml"/>
</project>
If you use Antlibs you can package them all inside a JAR file. Then simply copy this file into the ${ANT_HOME}/lib directory to use them.
After some additional searching, a possible solution would be to use SVN externals to check out specific required files that may be needed by the build.xml.
However, this would only work for users who are using Subversion as source control. It would still be nice to have a SCM-agnostic solution for users who aren't using Subversion, or another SCM that supports similar functionality.
What we've done is to create a project called 'bootstrap' which contains the various xml-files needed for the other projects at our office.
So to set up your development environment you run build.xml in bootstrap which copies the xml-files (like your ivy-stuff, and other targets) to a known location, and then your build files include these like this:
<import file="${ant.bootstrap.dir}/ant-commons.xml" />
<import file="${ant.bootstrap.dir}/ant-commons-ear.xml" />
Our bootstrap build.xml contains this:
<target name="install">
<fail unless="ant.bootstrap.dir" message="ant.bootstrap.dir ${missing.property.message}"/>
<copy todir = "${ant.bootstrap.dir}">
<fileset dir = "src/xml"/>
</copy>
</target>

Where do I get the Antlr Ant task?

I'm trying to call an Antlr task in my Ant build.xml as follows:
<path id="classpath.build">
<fileset dir="${dir.lib.build}" includes="**/*.jar" />
</path>
...
<target name="generate-lexer" depends="init">
<antlr target="${file.antlr.lexer}">
<classpath refid="classpath.build"/>
</antlr>
</target>
But Ant can't find the task definition. I've put all of the following in that dir.lib.build:
antlr-3.1.jar
antlr-2.7.7.jar
antlr-runtime-3.1.jar
stringtemplate-3.2.jar
But none of those seems to have the task definition. (I've also tried putting those jars in my Ant classpath; same problem.)
The current Antlr-task jar is available at http://www.antlr.org/share/1169924912745/antlr3-task.zip
It can be found on the antlr.org website under the "File Sharing" heading.
You should use the antlrall.jar jar. You can go ahead and just drop it into your Ant installation but that does mean that it will only work for that one install. We check the jar in and use taskdef to load the jar file so that it doesn't become another step for developers when they start on the team or move to a new computer.
Antlr http://ant.apache.org/manual/Tasks/antlr.html
Using taskdef http://www.onjava.com/pub/a/onjava/2004/06/02/anttask.html
I just got this working for myself. Took me an hour. ugh. anyway,
Step 1: download ant-antlr3 task from
http://www.antlr.org/share/1169924912745/antlr3-task.zip
Step 2: copy to where ant can see it. My mac:
sudo cp /usr/local/lib/ant-antlr3.jar /usr/share/ant/lib/
my linux box:
sudo cp /tmp/ant-antlr3.jar /usr/local/apache-ant-1.8.1/lib/
Step 3: make sure antlr2, antlr3, ST are in classpath. All in one is here:
http://antlr.org/download/antlr-3.3-complete.jar
Step 4: use in build.xml
<path id="classpath">
<pathelement location="${antlr3.jar}"/>
<pathelement location="${ant-antlr3.jar}"/>
</path>
<target name="antlr" depends="init">
<antlr:ant-antlr3 xmlns:antlr="antlib:org/apache/tools/ant/antlr"
target="src/T.g"
outputdirectory="build">
<classpath refid="classpath"/>
</antlr:ant-antlr3>
</target>
Just added a faq entry:
http://www.antlr.org/wiki/pages/viewpage.action?pageId=24805671
The most basic way to run Antlr is to execute the Antlr JAR:
<project default="antlr">
<target name="antlr">
<java jar="antlr-4.1-complete.jar" fork="true">
<arg value="grammar.g4"/>
</java>
</target>
</project>
This is a bit slower, because it forks the JVM and it runs Antlr even if the grammar did not change. But it works in the same way with every Antlr version and does not need any special targets.
On Ubuntu this should make it available:
sudo apt-get install ant-optional
Additional info on top of what everybody else contributed so far:
The ant-optional package in Ubuntu includes the task shipped with Ant 1.8.2 which is a task for ANTLR 2.7.2 so this will fail with an error as described in this post. The method described by Terence is the best way to use the ANTLR3 task.
If you do not have root access on a Linux machine, you can install the ant-antlr3.jar file in the Ant user directory: ~/.ant/lib. Check with ant -diagnostics whether ant-antlr3.jar is visible to Ant, as explained in this other post.
If you are using Eclipse, you will need to restart the IDE before it recognises the new task and you will also need to include antlr3.jar and stringtemplate.jar in your classpath (but ant-antlr3.jar is not necessary).

Resources