0
votes

I am trying to use ant to build an application, run the application's main() method, run junit tests, and package everything in a jar file (source+build+libraries+build.xml). The classes include a runner class with a main() method entry point.

The objective however is to inlcude all libraries used, like junit.jar, and the ant build.xml itself.

I was wondering if there is a way for the executable jar file, to run selected targets of the ant build file itslef, instead of just the main() method. Obviously I wouldn't need to run the compile targets again, but only the main() method (the java element in my run target) and the junit target. Is that possible?

Many thanks in advance for the insight!

1
denchr, did I answer your question? If not, do clarify what are you trying to achieve and I'll edit my reply (or someone else would provide a better one). And if I did, please vote for or accept the answer - that's how SO works and you don't seem to be doing that for most of your questions.ChssPly76
Yes, you actually did. Thanks a bunch. Let me accept your answer, sorry about that.denchr

1 Answers

3
votes

"Executable jar" is not what you think it is. It allows you pack all your classes together (you can add source to it as well though I see little point in that) and declare a main class using Main-Class attribute in the jar manifest. Details are here.

You can then launch that jar using "java -jar my.jar" command line which would invoke main() method of the class you've specified. You can also specify classpath via Class-Path attribute in the manifest file that can point to other jars needed by your application. That DOES NOT MEAN those jars are archived within your jar; in fact if you were to do that, JVM won't be able to load classes from those jars unless you were to take special precautions and write a custom classloader.