my dog learned polymorphism*
The moose likes Ant, Maven and Other Build Tools and the fly likes Checking for .jar in classpath with ANT Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "Checking for .jar in classpath with ANT " Watch "Checking for .jar in classpath with ANT " New topic
Author

Checking for .jar in classpath with ANT

Andrew Mcmurray
Ranch Hand

Joined: Sep 24, 2005
Posts: 188
Hi all

I only want to allow a certain target to run if a particular .jar is in the classpath. Is there a way to check this? I saw a depend attribute, but I wasn't sure if this was what I was looking for.

Thanks,

AMD
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30795
    
157

Andrew,
The "depend" attribute runs another one before the current one. What you want is the "if" attribute. In an earlier target, use the condition tag to set a property if that jar is in the classpath. Then for your target that needs this jar run it if that property is set.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Andrew Mcmurray
Ranch Hand

Joined: Sep 24, 2005
Posts: 188
Hi Jeanne,

Thanks for the reply. So probably have some init target that set a flag if the .jar is in the classpath. Then check if that flag has been set in the other target. I think I saw that ANT has a built in classpath var right?

Thanks,

AMD
Andrew Mcmurray
Ranch Hand

Joined: Sep 24, 2005
Posts: 188
Hi all

I can get it to check if the .jar exists in the dir using



and then using the if attribute on the target, but I need to check to see if the lib.jar is on the classpath.

Any thoughts on how to check for this?

Thanks,

AMD
Ryan Zezeski
Greenhorn

Joined: Mar 13, 2005
Posts: 29
First you need to make a script to do the searching for you, like so:



Then, you need to call this script from your ant script:



I used tools.jar to test this myself, replace it with the jar you need to search for. This will put either TRUE or FALSE in search.result. If you want to be hardcore you don't even need the if/else case in the shell script, just the first line and then echo $?. Then search.result will hold the exit status (0 for found, 1 for not found).

Either that or you could extend ant with your own class that searches the classpath.

Also, if your on windows then the script will have to be different or you could run cygwin.


Java has 99 problems but a pointer ain't one
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30795
    
157

Andrew,
I was so focused on the depends/if, I missed the classpath part of your question!

The available task has a classname attribute. If you can pick a class in your jar that isn't likely to be in other jars, you can do it this way. As most jars contain many unique classes, this is a fairly good proposition.

From the Ant docs:


This is preferable to writing a UNIX script for three reasons:
1) It's shorter
2) It includes classes Ant loaded, not just those in the system classpath
3) It doesn't tie you to a particular OS
Ryan Zezeski
Greenhorn

Joined: Mar 13, 2005
Posts: 29
Originally posted by Jeanne Boyarsky:

This is preferable to writing a UNIX script for three reasons:
1) It's shorter
2) It includes classes Ant loaded, not just those in the system classpath
3) It doesn't tie you to a particular OS


1) Shorter is not always better.
2) I'm not sure why you would want to do this? I would imagine you want to keep your testing/build enviornment as segregated as possible from your working environment. So why is your test/build using classes ant loaded? I would be using a forked process with it's own classpath.
3)Got me there, but in most cases your either programming in Windows or Unix/Linux, and writing a script file isn't rocket science (although Windows scripting sucks IMO).


However, I do agree that your solution is a better fit. I guess the only thing to watch for is searching for a common class that multiple jar files share.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30795
    
157

Originally posted by Ryan Zezeski:
1) Shorter is not always better.

Right. That's why this one got a smiley. Shorter is better if it means not reinventing the wheel of course.


2) I'm not sure why you would want to do this? I would imagine you want to keep your testing/build enviornment as segregated as possible from your working environment. So why is your test/build using classes ant loaded? I would be using a forked process with it's own classpath.

I use some task defs which are loaded by my build script not the system classpath. Someone else controls the classpath, so the build should take care of loading those dependencies on it's own. Andrew didn't specify whether that was the case for him or not.


3)Got me there, but in most cases your either programming in Windows or Unix/Linux, and writing a script file isn't rocket science (although Windows scripting sucks IMO)

Agreed. I'm a UNIX person myself and I've had to train myself to let Ant do things it is good at rather than keep delegating to UNIX. And we may want to run the build on Windows at some point, so it would be good to minimize the OS dependencies.
Bejoy Kunjumon
Greenhorn

Joined: Feb 20, 2006
Posts: 1
Hi

Is there any way to check for existence of multiple files

For example in my test.dir I have to check wether jars bigining with Session is present. available task only help in single file right?

Thanks in Advance
Bejoy
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Checking for .jar in classpath with ANT