I have a build script (which just happens to be maintained by NetBeans) that
I am trying to execute targets on in java programmatically. One of the
targets is called "dist" which will build a web project and create the
distributable jar. I have code that programmatically retrieves all
dependent projects and jar files. I have this listing of jars and I want to
add them to the "classpath" for the build. However, I am unable to do so.
Can somone review the code below to make sure I am at least properly adding
the jars to ant's classpath. The fact that the target may not be using the
classpath is ok. I will try to resolve that separately as it would not be a
concern for ant user group.
String buildfile = null;
Project project;
Path path;
FileList filelist;
ProjectHelper helper;
try {
buildfile = "build.xml";
project = new Project();
project.init();
project.setBaseDir(this.workingDirectory);
project.setName(this.projectName);
project.setUserProperty("ant.file", buildfile);
helper = ProjectHelper.getProjectHelper();
project.addReference("ant.projectHelper",helper);
path = new Path(project);
addPathsFromClasspath(path);
for (String pn : path.list()) {
System.out.println("path-->" + pn);
}
helper.parse(project, new File(this.workingDirectory.getPath() +
"/" + 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());
}
// can above code be replaced with project.setSystemProporties();
findAndExecuteBuildTarget(project);
} catch (Exception e) {
if (debug) {
e.printStackTrace();
}
if (e instanceof BuildException && e.getMessage().indexOf("
java.io.FileNotFoundException") > -1) {
System.err.println("WARNING!!! Unable to build project
because build.xml was not found for " + this.projectName);
} else {
throw new RuntimeException(e);
}
} finally {
path = null;
filelist = null;
project = null;
buildfile = null;
helper = null;
}
}
private void addPathsFromClasspath(Path path) {
List<FileSet> classpath = parseClasspath();
for (FileSet fs : classpath) {
path.addFileset(fs);
}
}
private List<FileSet> parseClasspath() {
List<FileSet> arList = new ArrayList<FileSet>();
Set<String> set = new HashSet<String>();
for (File file : getClasspath()) {
set.add(file.getParentFile().getAbsolutePath());
}
for (String parent : set) {
FileSet fs = new FileSet();
fs.setDir(new File(parent));
fs.setIncludes("**/*.jar");
arList.add(fs);
}
return arList;
}
private List<File> getClasspath() {
.... // code to retrieve List of file references to classpath entries
}
--
Marc Farrow