• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Checking for .jar in classpath with ANT

 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Andrew Mcmurray
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic