I have some servlet and I am trying to call an Ant script within the class
to compile a "project". All of the references in the build script are not
relative, therefore some of the classpath references are not correct. I
know I can change the properties, etc to fix this. However, due to the
nature of our environment we do not want to do this. With all that being
said, I do know that all the classpath entries that I need to compile the
needed classes are within my current servlet container (in my case Tomcat).
What can I do retrieve the correct classloader so that all the classes are
visible to the Ant script. Below is a snippet of code I am using. In
comments, you will see the various ways I have tried to call the
setCoreLoader method.
Thank you,
File buildfile = null;
try {
buildfile = new File(this.workingDirectory.getPath() + "/build.xml");
Project project = new Project();
project.setBaseDir(this.workingDirectory);
project.setName(this.project);
project.setUserProperty("ant.file", buildfile.getPath());
project.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
project.addReference("ant.projectHelper", helper);
helper.parse(project, buildfile);
Properties sysProps = System.getProperties();
for (Iterator it = sysProps.entrySet().iterator();it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
project.setProperty(entry.getKey().toString(), entry.getValue
().toString());
}
project.setProperty("basedir", project.getBaseDir().getPath());
project.setProperty("ant.project.name", project.getName());
project.setProperty("ant.file", buildfile.getPath());
project.setCoreLoader(Thread.currentThread().getContextClassLoader());
// project.setCoreLoader(null);
// project.setCoreLoader(this.getClass().getClassLoader());
// project.setCoreLoader(Thread.currentThread
().getContextClassLoader().getParent());
project.execute("dist");
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
project = null;
buildfile = null;
}
--
Marc Farrow