I need to run an ANT build using the build.xml file that is stored inside the jar file. This jar file is available on the classpath. Is it possible to do it, without exploding the jar file and saving the build.xml to local directory? If so how can I do it.
Update:
The jar file is a ant build project where a build.xml in classpath is run via a java program using ANT libraries. The front end is a web application, which loads this build project jar as a dependency. A user will click a Build App button to run the ANT build. So I need to run the build.xml file in a jar when user clicks Build App.
Code for reading the build.xml file
URL preBuildFileUrl = getClass().getClassLoader().getResource("build.xml");
Project p = new Project();
p.setUserProperty("ant.file", preBuildFileUrl.toURI().getPath());
p.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
p.addReference("ant.projectHelper", helper);
helper.parse(p, new File(preBuildFileUrl.toURI().getPath()));
p.executeTarget(p.getDefaultTarget());
I think it's not possible. The API for the ProjectHelper#parse method suggests that it is possible for a Project helper to support any kind of sources including
InputStream
running a modifiyed sample of you code
gives the following Exception:
Unfortunatly after looking into the source code of the latest apache ant version (1.9.4 at posting time) the Built-In
ProjectHelper
implementation does only supporteven though an
InputStream
is created a few lines later :/The apache ant manual suggests that it is possible to implemt an own
ProjectHelper
and configure ant to use it using the system propertyorg.apache.tools.ant.ProjectHelper
.But all the methods that need to be implemented seem to work only with
java.io.File
orjava.net.URL
.So I think your best bet is to extrat the
build.xml
from the jar (as shown in my comment) and run ant against it.It is possible by using the Java URL syntax for jars. If you obtained your build.xml with getResource() the URL external form looks like this:
So calling with the URL should be fine: