// If this class does not exist, then we are not running Java 1.4
}
if (lessThan14)
Thread.currentThread().setContextClassLoader(Launcher.class.getClassLoader());
Project project = new Project();
// Set the project's class loader
project.setCoreLoader(Launcher.class.getClassLoader());
// Initialize the project. Note that we don't invoke the
// Project.init() method directly as this will cause all of
// the myriad of Task subclasses to load which is a big
// performance hit. Instead, we load only the
// Launcher.SUPPORTED_ANT_TASKS and Launcher.SUPPORTED_ANT_TYPES
// into the project that the Launcher supports.
for (int i = 0; i < Launcher.SUPPORTED_ANT_TASKS.length; i++) {
// The even numbered elements should be the task name
String taskName = (String)Launcher.SUPPORTED_ANT_TASKS[i];
// The odd numbered elements should be the task class
Class taskClass = (Class)Launcher.SUPPORTED_ANT_TASKS[++i];
project.addTaskDefinition(taskName, taskClass);
}
for (int i = 0; i < Launcher.SUPPORTED_ANT_TYPES.length; i++) {
// The even numbered elements should be the type name
String typeName = (String)Launcher.SUPPORTED_ANT_TYPES[i];
// The odd numbered elements should be the type class
Class typeClass = (Class)Launcher.SUPPORTED_ANT_TYPES[++i];
project.addDataTypeDefinition(typeName, typeClass);
}
// Add all system properties as project properties
project.setSystemProperties();
// Parse the arguments
int currentArg = 0;
// Set default XML file
File launchFile = new File(Launcher.getBootstrapDir(), Launcher.DEFAULT_XML_FILE_NAME);
// Get standard launcher arguments
for ( ; currentArg < args.length; currentArg++) {
// If we find a "-" argument or an argument without a
// leading "-", there are no more standard launcher arguments
if ("-".equals(args[currentArg])) {
currentArg++;
break;
} else if (args[currentArg].length() > 0 && !"-".equals(args[currentArg].substring(0, 1))) {
break;
} else if ("-help".equals(args[currentArg])) {
throw new IllegalArgumentException();
} else if ("-launchfile".equals(args[currentArg])) {
if (currentArg + 1 < args.length){
String fileArg = args[++currentArg];
launchFile = new File(fileArg);
if (!launchFile.isAbsolute())
launchFile = new File(Launcher.getBootstrapDir(), fileArg);
} else {
throw new IllegalArgumentException(args[currentArg] + " " + Launcher.getLocalizedString("missing.arg"));
}
} else if ("-executablename".equals(args[currentArg])) {
if (currentArg + 1 < args.length)
System.setProperty(ChildMain.EXECUTABLE_PROP_NAME, args[++currentArg]);
else
throw new IllegalArgumentException(args[currentArg] + " " + Launcher.getLocalizedString("missing.arg"));
} else if ("-verbose".equals(args[currentArg])) {
Launcher.setVerbose(true);
} else {
throw new IllegalArgumentException(args[currentArg] + " " + Launcher.getLocalizedString("invalid.arg"));
}
}
// Get target
String target = null;
if (currentArg < args.length)
target = args[currentArg++];
else
throw new IllegalArgumentException(Launcher.getLocalizedString("missing.target"));
// Get user properties
for ( ; currentArg < args.length; currentArg++) {
// If we don't find any more "-" or "-D" arguments, there are no
// more user properties
if ("-".equals(args[currentArg])) {
currentArg++;
break;
} else if (args[currentArg].length() <= 2 || !"-D".equals(args[currentArg].substring(0, 2))) {
break;
}
int delimiter = args[currentArg].indexOf('=', 2);
String key = null;
String value = null;
if (delimiter >= 2) {
key = args[currentArg].substring(2, delimiter);
value = args[currentArg].substring(delimiter + 1);
} else {
// Unfortunately, MS-DOS batch scripts will split an
// "-Dname=value" argument into "-Dname" and "value"
// arguments. So, we need to assume that the next
// argument is the property value unless it appears
// to be a different type of argument.
key = args[currentArg].substring(2);
if (currentArg + 1 < args.length &&
!"-D".equals(args[currentArg + 1].substring(0, 2)))
{
value = args[++currentArg];
} else {
value = "";
}
}
project.setUserProperty(key, value);
}
// Treat all remaining arguments as application arguments
String[] appArgs = new String[args.length - currentArg];
for (int i = 0; i < appArgs.length; i++) {
appArgs[i] = args[i + currentArg];
project.setUserProperty(LaunchTask.ARG_PROP_NAME + Integer.toString(i), appArgs[i]);
}
// Set standard Ant user properties
project.setUserProperty("ant.version", Main.getAntVersion());
project.setUserProperty("ant.file", launchFile.getCanonicalPath());
project.setUserProperty("ant.java.version", System.getProperty("java.specification.version"));
// Set the buildfile
ProjectHelper.configureProject(project, launchFile);
// Check that the target exists
if (!project.getTargets().containsKey(target))
throw new IllegalArgumentException(target + " " + Launcher.getLocalizedString("invalid.target"));
// Execute the target
try {
runtime.addShutdownHook(shutdownHook);
} catch (NoSuchMethodError nsme) {
// Early JVMs do not support this method
}
project.executeTarget(target);
} catch (Throwable t) {
// Log any errors
returnValue = 1;
String message = t.getMessage();