Classpath for taskdef? - ant

I am defining a new task in Ant.
I exported it as a jar and added to my buildfile:
<taskdef classname="X" classpath="Y.jar"/>
The problem is that this fails at runtime.
It tells me it didn't find the class.
By adding the jar to the classpath, it is corrected.
My question is: Is there a way that I can refer to my jar from the Ant buildfile, without changing the classpath?

If you know the path of your jar, inside ant script you can define the classpath for your own task.
<taskdef name="myTaskName" classname="com.myorg.myclass">
<classpath>
<pathelement location="pathToMyJar.jar"/>
</classpath>
</taskdef>

Yes. I'm assuming that you looked at the doc for taskdef, which just shows the task name and implementing class. However, taskdef subclasses typedef, and if you look at the doc for the latter you'll see that there's also a classpath attribute.
HOWEVER, using this attribute means that your ant scripts are tied to a particular environment; they aren't very portable. A far better approach is to pass the classpath into Ant, using the -lib invocation option.

Related

Using JDK 6 style classpath with javac ANT compile task

Does anybody have an example of using JDK 6 style - only defining the directory and not list of each and every jar in the directory - classpath with an ANT javac compile task?
We tried it like this
compile.classpath=c:....\WEB-INF\lib*;...websphere...\a.jar;...websphere...\b.jar; etc. and used this classpath in the javac task, but the libraries from WEB-INF\lib were not being recognised.
So, I am wondering if it is possible to use this type of construct with ANT. The ANT version is 1.8.4.
Typically we use path like structures .
<path id="project.classpath">
<fileset dir="${libdirectory}">
<include name="*.jar"/>
</fileset>
</path>
The the project.classpath is used in javac task. See more at ant examples
I do not yet know, why this works vs. why it doesn't work without these attributes, but I was able to get the wildcard part to work by using the fork="yes" and executable="path-to-my-executable" attributes in the javac task.

Including maven convention folders to classpath in IVY

The reason we are using IVY is that we already have ANT scripts. Meaning, there is no maven compatible folders by default.
We had to create such convention.
Now, I already implemented the IVYing. After resolve, I do the
<ivy:cachepath pathid="ivy.test.classpath" conf="test" settingsref="ivy.ref" />
The problem with this, it doesn't include the
src/test/java , src/test/resources
What is the best way of including those.
What I'm currently doing is creating another classpath (postfixed with .extended) that includes the original classpath along with the new folders.
Is there an 'IVYier' way to do it? maybe add something in the dependency file ivy.xml or a way to tell it to act maven.
Thanks in advance
Why do you need to add the source dirs to the classpath?
I typically only need to add the compiled classes directory into a classpath and that is typically easier to do directly in the few tasks like Junit that needs to be configured in this manner.
For example:
<junit ....>
<classpath>
<path refid="runtime.path"/>
<pathelement path="${classes.dir}"/>
</classpath>
..
</junit>

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.

Specifying an alternate name for junit.jar for the ant junit task

By default JUnit jars have the version number in the file name, eg junit-4.8.2.jar. The JUnit Ant Task only looks for junit.jar. Is it possible to specify a name based on the standard naming convention. I'd like to do so because I want to reference libs in my local maven repository.
Actually, JUnit task documentation gives you a few alternatives:
Note: You must have junit.jar available. You can do one of:
Put both junit.jar and ant-junit.jar in ANT_HOME/lib.
Do not put either in ANT_HOME/lib, and instead include their locations in your CLASSPATH environment variable.
Add both JARs to your classpath using -lib.
Specify the locations of both JARs using a <classpath> element in a <taskdef> in the build file.
Leave ant-junit.jar in its default location in ANT_HOME/lib but include junit.jar in the <classpath> passed to <junit>. (since Ant 1.7)
EDIT
This is what you need.
<junit
...
>
<classpath>
<pathelement location="/path/to/your/custom/junit-4.8.2.jar""/>
<path refid="your.other.classpath.dependencies" />
</classpath>
...
</junit>

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.

Resources