0

Is it possible to run an ant script from within a jar file?

At the moment, I've got a build.xml which calls some java code which is in a jar file. It then does some further ant processing.

When I export the jar file from Eclipse, I can choose to add the build.xml file to the jar, so I'm wondering whether I could run the ant script directly from the jar somehow so that I only have to give people the jar file, not the ant script as well.

bodger
  • 1,112
  • 6
  • 24

1 Answers1

0

The answer is cyclic. Are you trying to add the build.xml to execute A.jar into A.jar itself? If so, you want to execute the jar with the parameters mentioned in the build.xml. If this is the case, you might as well write a main method which runs with those specific set of parameters.

In any case, there should always be a way to run your jar with some parameters which can be accomplished without the build.xml.

If you really need to do this via ant, use this Starting a process in Java?

Update:

See this link in stackoverflow. -> Run ant from Java

I have not tried this personally but this is one way to go provided you take care of the cyclic nature of your problem

Community
  • 1
  • 1
guruprasath
  • 277
  • 3
  • 17
  • What I need to be able to do is for the user to give the process a location of an ant properties file and for that file to determine the arguments that are passed to the java process. Once that java process is complete, the ant script then does some more processing. – bodger Mar 20 '13 at 12:07
  • So, I am assuming a B.jar which takes in the location of the ant "build" file and executes a specific target which in turn executes A.jar's Main method with some parameters and does some additional processing. If this is the case, this is no different from using an ant command itself directly instead of B.jar. But since you do not want to distribute the build.xml because of some reasons, you want it to be a part of the B.jar which can be executed using the jar command. Is this your scenario? – guruprasath Mar 20 '13 at 12:16
  • No, one jar, which contains the ant build file. I want the user to be able to run the ant script from that jar file. – bodger Mar 20 '13 at 14:46
  • Then you can use the link I have pasted above to achieve your objective. Have a main class which runs an Ant Task from within java and give the embedded build.xml as a resource to that. You will have to experiment a bit with the url of the buid.xml http://ant.apache.org/manual/running.html – guruprasath Mar 22 '13 at 07:08