How to add thirdparty jars as Bootstrap entries to jvm in ANT script while building project using bamboo - ant

I have added activation.jar & mail.jar's path under Bootstrap entries of run configuration in eclipse. I have done so because I was facing NoClassDefError for javax.activation.DataSource even though I have added it in my project classpath. Also didn't see any compilation issues for code where I used DataSource and other classes from these jars. I tried all solutions listed suggested on stackoverflow but nothing worked for me and finally I got this Bootstrap entries option which resolved my issue.
Now the problem is I wanted to create build using Bamboo and I have mentioned required jars in launcher ANT script as below:
<target>
<java jvm="${javaHome}/bin/java.exe"
<!--some other parameters ->
>
<classpath>
<fileset dir="${buildDir}/scripts/lib">
<include name="mail.jar"/>
<include name="activation.jar"/>
</fileset>
</classpath>
</java>
</target>
Still I am getting same activation error in generated build application. I am very new to ANT. Please suggest how can resolve above issue.
Thanks!

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.

The archive doesn't exist error when running ant jar task

I'm running an ant build file that creates a Jar. The task looks like this:
<target name="generator-app" depends="clean,compile">
<jar jarfile="${gen.App}">
<manifest>
...
</manifest>
<fileset dir="${classes}">
<include name="com/mypackage/**" />
</fileset>
<zipfileset dir="${jars}" />
</jar>
</target>
The build file runs and creates the file as expected when I run it on Linux, but fails with this error on any other platform:
BUILD FAILED /home/user/build.xml:287: the archive doesn't exist
I tried using destfile instead of jarfile, but the same result occurs. The archive does not exist indeed, but the purpose of the task is to create it.
Is there any limitation on certain platforms or any way to correct this?
Forward slashes don't seem very windowy to me :) Maybe you should convert your slashes based on your os? Do you pass this path somewhere?
I found the problem. I tried using a newer version of ant, and now the error indicates exactly what is missing (a jar to be packaged in the jar to be created).

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.

where to find missing optional ant tasks?

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.

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