Browse Source

Cleanup of Main.java

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271753 13f79535-47bb-0310-9956-ffa450edef68
master
Conor MacNeill 23 years ago
parent
commit
9ec33bdb83
1 changed files with 59 additions and 39 deletions
  1. +59
    -39
      src/main/org/apache/tools/ant/Main.java

+ 59
- 39
src/main/org/apache/tools/ant/Main.java View File

@@ -161,13 +161,14 @@ public class Main {


try { try {
m = new Main(args); m = new Main(args);
} catch(Throwable exc) {
} catch (Throwable exc) {
printMessage(exc); printMessage(exc);
System.exit(1); System.exit(1);
} }


if (additionalUserProperties != null) { if (additionalUserProperties != null) {
for (Enumeration e = additionalUserProperties.keys(); e.hasMoreElements(); ) {
for (Enumeration e = additionalUserProperties.keys();
e.hasMoreElements(); ) {
String key = (String) e.nextElement(); String key = (String) e.nextElement();
String property = additionalUserProperties.getProperty(key); String property = additionalUserProperties.getProperty(key);
m.definedProps.put(key, property); m.definedProps.put(key, property);
@@ -182,7 +183,7 @@ public class Main {
printMessage(be); printMessage(be);
} }
System.exit(1); System.exit(1);
} catch(Throwable exc) {
} catch (Throwable exc) {
exc.printStackTrace(); exc.printStackTrace();
printMessage(exc); printMessage(exc);
System.exit(1); System.exit(1);
@@ -238,15 +239,16 @@ public class Main {
msgOutputLevel = Project.MSG_DEBUG; msgOutputLevel = Project.MSG_DEBUG;
} else if (arg.equals("-logfile") || arg.equals("-l")) { } else if (arg.equals("-logfile") || arg.equals("-l")) {
try { try {
File logFile = new File(args[i+1]);
File logFile = new File(args[i + 1]);
i++; i++;
out = new PrintStream(new FileOutputStream(logFile)); out = new PrintStream(new FileOutputStream(logFile));
err = out; err = out;
System.setOut(out); System.setOut(out);
System.setErr(out); System.setErr(out);
} catch (IOException ioe) { } catch (IOException ioe) {
String msg = "Cannot write on the specified log file. " +
"Make sure the path exists and you have write permissions.";
String msg = "Cannot write on the specified log file. "
+ "Make sure the path exists and you have write "
+ "permissions.";
System.out.println(msg); System.out.println(msg);
return; return;
} catch (ArrayIndexOutOfBoundsException aioobe) { } catch (ArrayIndexOutOfBoundsException aioobe) {
@@ -255,9 +257,10 @@ public class Main {
System.out.println(msg); System.out.println(msg);
return; return;
} }
} else if (arg.equals("-buildfile") || arg.equals("-file") || arg.equals("-f")) {
} else if (arg.equals("-buildfile") || arg.equals("-file")
|| arg.equals("-f")) {
try { try {
buildFile = new File(args[i+1]);
buildFile = new File(args[i + 1]);
i++; i++;
} catch (ArrayIndexOutOfBoundsException aioobe) { } catch (ArrayIndexOutOfBoundsException aioobe) {
String msg = "You must specify a buildfile when " + String msg = "You must specify a buildfile when " +
@@ -267,7 +270,7 @@ public class Main {
} }
} else if (arg.equals("-listener")) { } else if (arg.equals("-listener")) {
try { try {
listeners.addElement(args[i+1]);
listeners.addElement(args[i + 1]);
i++; i++;
} catch (ArrayIndexOutOfBoundsException aioobe) { } catch (ArrayIndexOutOfBoundsException aioobe) {
String msg = "You must specify a classname when " + String msg = "You must specify a classname when " +
@@ -292,16 +295,17 @@ public class Main {
String value = null; String value = null;
int posEq = name.indexOf("="); int posEq = name.indexOf("=");
if (posEq > 0) { if (posEq > 0) {
value = name.substring(posEq+1);
value = name.substring(posEq + 1);
name = name.substring(0, posEq); name = name.substring(0, posEq);
} else if (i < args.length-1) {
} else if (i < args.length - 1) {
value = args[++i]; value = args[++i];
} }


definedProps.put(name, value); definedProps.put(name, value);
} else if (arg.equals("-logger")) { } else if (arg.equals("-logger")) {
if (loggerClassname != null) { if (loggerClassname != null) {
System.out.println("Only one logger class may be specified.");
System.out.println("Only one logger class may "
+ " be specified.");
return; return;
} }
try { try {
@@ -319,14 +323,14 @@ public class Main {
projectHelp = true; projectHelp = true;
} else if (arg.equals("-find")) { } else if (arg.equals("-find")) {
// eat up next arg if present, default to build.xml // eat up next arg if present, default to build.xml
if (i < args.length-1) {
if (i < args.length - 1) {
searchForThis = args[++i]; searchForThis = args[++i];
} else { } else {
searchForThis = DEFAULT_BUILD_FILENAME; searchForThis = DEFAULT_BUILD_FILENAME;
} }
} else if (arg.startsWith("-propertyfile")) { } else if (arg.startsWith("-propertyfile")) {
try { try {
propertyFiles.addElement(args[i+1]);
propertyFiles.addElement(args[i + 1]);
i++; i++;
} catch (ArrayIndexOutOfBoundsException aioobe) { } catch (ArrayIndexOutOfBoundsException aioobe) {
String msg = "You must specify a property filename when " + String msg = "You must specify a property filename when " +
@@ -372,10 +376,11 @@ public class Main {
} }


// Load the property files specified by -propertyfile // Load the property files specified by -propertyfile
for (int propertyFileIndex=0;
for (int propertyFileIndex = 0;
propertyFileIndex < propertyFiles.size(); propertyFileIndex < propertyFiles.size();
propertyFileIndex++) { propertyFileIndex++) {
String filename = (String) propertyFiles.elementAt(propertyFileIndex);
String filename
= (String) propertyFiles.elementAt(propertyFileIndex);
Properties props = new Properties(); Properties props = new Properties();
FileInputStream fis = null; FileInputStream fis = null;
try { try {
@@ -421,7 +426,7 @@ public class Main {
filename = file.getParent(); filename = file.getParent();


if (filename != null && msgOutputLevel >= Project.MSG_VERBOSE) { if (filename != null && msgOutputLevel >= Project.MSG_VERBOSE) {
System.out.println("Searching in "+filename);
System.out.println("Searching in " + filename);
} }


return (filename == null) ? null : new File(filename); return (filename == null) ? null : new File(filename);
@@ -444,7 +449,8 @@ public class Main {
* *
* @exception BuildException if no build file is found * @exception BuildException if no build file is found
*/ */
private File findBuildFile(String start, String suffix) throws BuildException {
private File findBuildFile(String start, String suffix)
throws BuildException {
if (msgOutputLevel >= Project.MSG_INFO) { if (msgOutputLevel >= Project.MSG_INFO) {
System.out.println("Searching for " + suffix + " ..."); System.out.println("Searching for " + suffix + " ...");
} }
@@ -534,12 +540,14 @@ public class Main {
project.setUserProperty(arg, value); project.setUserProperty(arg, value);
} }
project.setUserProperty("ant.file" , buildFile.getAbsolutePath() );
project.setUserProperty("ant.file",
buildFile.getAbsolutePath() );
// first use the ProjectHelper to create the project object // first use the ProjectHelper to create the project object
// from the given build file. // from the given build file.
String noParserMessage =
"No JAXP compliant XML parser found. Please visit http://xml.apache.org for a suitable parser";
String noParserMessage = "No JAXP compliant XML parser found. "
+ "Please visit http://xml.apache.org "
+ "for a suitable parser";
try { try {
Class.forName("javax.xml.parsers.SAXParserFactory"); Class.forName("javax.xml.parsers.SAXParserFactory");
ProjectHelper.configureProject(project, buildFile); ProjectHelper.configureProject(project, buildFile);
@@ -575,11 +583,11 @@ public class Main {
System.setErr(err); System.setErr(err);
} }
} }
catch(RuntimeException exc) {
catch (RuntimeException exc) {
error = exc; error = exc;
throw exc; throw exc;
} }
catch(Error err) {
catch (Error err) {
error = err; error = err;
throw err; throw err;
} }
@@ -609,18 +617,22 @@ public class Main {
(BuildListener) Class.forName(className).newInstance(); (BuildListener) Class.forName(className).newInstance();
project.addBuildListener(listener); project.addBuildListener(listener);
} }
catch(Throwable exc) {
throw new BuildException("Unable to instantiate listener " + className, exc);
catch (Throwable exc) {
throw new BuildException("Unable to instantiate listener "
+ className, exc);
} }
} }
} }


// XXX: (Jon Skeet) Any reason for writing a message and then using a bare // XXX: (Jon Skeet) Any reason for writing a message and then using a bare
// RuntimeException rather than just using a BuildException here? Is it // RuntimeException rather than just using a BuildException here? Is it
// in case the message could end up being written to no loggers (as the loggers
// could have failed to be created due to this failure)?
// in case the message could end up being written to no loggers (as the
// loggers could have failed to be created due to this failure)?
/** /**
* Creates the default build logger for sending build events to the ant log.
* Creates the default build logger for sending build events to the ant
* log.
*
* @return the logger instance for this build.
*/ */
private BuildLogger createLogger() { private BuildLogger createLogger() {
BuildLogger logger = null; BuildLogger logger = null;
@@ -629,13 +641,14 @@ public class Main {
logger = (BuildLogger)(Class.forName(loggerClassname).newInstance()); logger = (BuildLogger)(Class.forName(loggerClassname).newInstance());
} }
catch (ClassCastException e) { catch (ClassCastException e) {
System.err.println("The specified logger class " + loggerClassname +
" does not implement the BuildLogger interface");
System.err.println("The specified logger class "
+ loggerClassname
+ " does not implement the BuildLogger interface");
throw new RuntimeException(); throw new RuntimeException();
} }
catch (Exception e) { catch (Exception e) {
System.err.println("Unable to instantiate specified logger class " +
loggerClassname + " : " + e.getClass().getName());
System.err.println("Unable to instantiate specified logger "
+ "class " + loggerClassname + " : " + e.getClass().getName());
throw new RuntimeException(); throw new RuntimeException();
} }
} }
@@ -783,13 +796,14 @@ public class Main {


printTargets(topNames, topDescriptions, "Main targets:", maxLength); printTargets(topNames, topDescriptions, "Main targets:", maxLength);
if( printSubTargets ) {
if (printSubTargets) {
printTargets(subNames, null, "Subtargets:", 0); printTargets(subNames, null, "Subtargets:", 0);
} }


String defaultTarget = project.getDefaultTarget(); String defaultTarget = project.getDefaultTarget();
if (defaultTarget != null && !"".equals(defaultTarget)) { // shouldn't need to check but...
System.out.println( "Default target: " + defaultTarget );
if (defaultTarget != null && !"".equals(defaultTarget)) {
// shouldn't need to check but...
System.out.println( "Default target: " + defaultTarget);
} }
} }


@@ -805,7 +819,7 @@ public class Main {
*/ */
private static int findTargetPosition(Vector names, String name) { private static int findTargetPosition(Vector names, String name) {
int res = names.size(); int res = names.size();
for (int i=0; i<names.size() && res == names.size(); i++) {
for (int i = 0; i < names.size() && res == names.size(); i++) {
if (name.compareTo((String)names.elementAt(i)) < 0) { if (name.compareTo((String)names.elementAt(i)) < 0) {
res = i; res = i;
} }
@@ -816,18 +830,24 @@ public class Main {
/** /**
* Writes a formatted list of target names to <code>System.out</code> * Writes a formatted list of target names to <code>System.out</code>
* with an optional description * with an optional description
*
* @param names the names to be printed.
* @param descriptions the associated target descriptions.
* @param heading the heading for the print.
* @param maxlen the maximum length of the names of the targets.
*/ */
private static void printTargets(Vector names, Vector descriptions, String heading, int maxlen) {
private static void printTargets(Vector names, Vector descriptions,
String heading, int maxlen) {
// now, start printing the targets and their descriptions // now, start printing the targets and their descriptions
String lSep = System.getProperty("line.separator"); String lSep = System.getProperty("line.separator");
// got a bit annoyed that I couldn't find a pad function // got a bit annoyed that I couldn't find a pad function
String spaces = " "; String spaces = " ";
while (spaces.length()<maxlen) {
while (spaces.length() < maxlen) {
spaces += spaces; spaces += spaces;
} }
StringBuffer msg = new StringBuffer(); StringBuffer msg = new StringBuffer();
msg.append(heading + lSep + lSep); msg.append(heading + lSep + lSep);
for (int i=0; i<names.size(); i++) {
for (int i = 0; i < names.size(); i++) {
msg.append(" "); msg.append(" ");
msg.append(names.elementAt(i)); msg.append(names.elementAt(i));
if (descriptions != null) { if (descriptions != null) {


Loading…
Cancel
Save