📄 main.java
字号:
} finally { // put back the original security manager //The following will never eval to true. (PD) if (oldsm != null) { System.setSecurityManager(oldsm); } System.setOut(savedOut); System.setErr(savedErr); System.setIn(savedIn); } } catch (RuntimeException exc) { error = exc; throw exc; } catch (Error e) { error = e; throw e; } finally { if (!projectHelp) { project.fireBuildFinished(error); } else if (error != null) { project.log(error.toString(), Project.MSG_ERR); } } } /** * Adds the listeners specified in the command line arguments, * along with the default listener, to the specified project. * * @param project The project to add listeners to. * Must not be <code>null</code>. */ protected void addBuildListeners(Project project) { // Add the default listener project.addBuildListener(createLogger()); for (int i = 0; i < listeners.size(); i++) { String className = (String) listeners.elementAt(i); BuildListener listener = (BuildListener) ClasspathUtils.newInstance(className, Main.class.getClassLoader(), BuildListener.class); project.setProjectReference(listener); project.addBuildListener(listener); } } /** * Creates the InputHandler and adds it to the project. * * @param project the project instance. * * @exception BuildException if a specified InputHandler * implementation could not be loaded. */ private void addInputHandler(Project project) throws BuildException { InputHandler handler = null; if (inputHandlerClassname == null) { handler = new DefaultInputHandler(); } else { handler = (InputHandler) ClasspathUtils.newInstance( inputHandlerClassname, Main.class.getClassLoader(), InputHandler.class); project.setProjectReference(handler); } project.setInputHandler(handler); } // XXX: (Jon Skeet) Any reason for writing a message and then using a bare // 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)? /** * Creates the default build logger for sending build events to the ant * log. * * @return the logger instance for this build. */ private BuildLogger createLogger() { BuildLogger logger = null; if (loggerClassname != null) { try { logger = (BuildLogger) ClasspathUtils.newInstance( loggerClassname, Main.class.getClassLoader(), BuildLogger.class); } catch (BuildException e) { System.err.println("The specified logger class " + loggerClassname + " could not be used because " + e.getMessage()); throw new RuntimeException(); } } else { logger = new DefaultLogger(); } logger.setMessageOutputLevel(msgOutputLevel); logger.setOutputPrintStream(out); logger.setErrorPrintStream(err); logger.setEmacsMode(emacsMode); return logger; } /** * Prints the usage information for this class to <code>System.out</code>. */ private static void printUsage() { String lSep = System.getProperty("line.separator"); StringBuffer msg = new StringBuffer(); msg.append("ant [options] [target [target2 [target3] ...]]" + lSep); msg.append("Options: " + lSep); msg.append(" -help, -h print this message" + lSep); msg.append(" -projecthelp, -p print project help information" + lSep); msg.append(" -version print the version information and exit" + lSep); msg.append(" -diagnostics print information that might be helpful to" + lSep); msg.append(" diagnose or report problems." + lSep); msg.append(" -quiet, -q be extra quiet" + lSep); msg.append(" -verbose, -v be extra verbose" + lSep); msg.append(" -debug, -d print debugging information" + lSep); msg.append(" -emacs, -e produce logging information without adornments" + lSep); msg.append(" -lib <path> specifies a path to search for jars and classes" + lSep); msg.append(" -logfile <file> use given file for log" + lSep); msg.append(" -l <file> ''" + lSep); msg.append(" -logger <classname> the class which is to perform logging" + lSep); msg.append(" -listener <classname> add an instance of class as a project listener" + lSep); msg.append(" -noinput do not allow interactive input" + lSep); msg.append(" -buildfile <file> use given buildfile" + lSep); msg.append(" -file <file> ''" + lSep); msg.append(" -f <file> ''" + lSep); msg.append(" -D<property>=<value> use value for given property" + lSep); msg.append(" -keep-going, -k execute all targets that do not depend" + lSep); msg.append(" on failed target(s)" + lSep); msg.append(" -propertyfile <name> load all properties from file with -D" + lSep); msg.append(" properties taking precedence" + lSep); msg.append(" -inputhandler <class> the class which will handle input requests" + lSep); msg.append(" -find <file> (s)earch for buildfile towards the root of" + lSep); msg.append(" -s <file> the filesystem and use it" + lSep); msg.append(" -nice number A niceness value for the main thread:" + lSep + " 1 (lowest) to 10 (highest); 5 is the default" + lSep); msg.append(" -nouserlib Run ant without using the jar files from" + lSep + " ${user.home}/.ant/lib" + lSep); msg.append(" -noclasspath Run ant without using CLASSPATH" + lSep); msg.append(" -autoproxy Java1.5+: use the OS proxy settings" + lSep); msg.append(" -main <class> override Ant's normal entry point"); System.out.println(msg.toString()); } /** * Prints the Ant version information to <code>System.out</code>. * * @exception BuildException if the version information is unavailable */ private static void printVersion() throws BuildException { System.out.println(getAntVersion()); } /** * Cache of the Ant version information when it has been loaded. */ private static String antVersion = null; /** * Returns the Ant version information, if available. Once the information * has been loaded once, it's cached and returned from the cache on future * calls. * * @return the Ant version information as a String * (always non-<code>null</code>) * * @exception BuildException if the version information is unavailable */ public static synchronized String getAntVersion() throws BuildException { if (antVersion == null) { try { Properties props = new Properties(); InputStream in = Main.class.getResourceAsStream("/org/apache/tools/ant/version.txt"); props.load(in); in.close(); StringBuffer msg = new StringBuffer(); msg.append("Apache Ant version "); msg.append(props.getProperty("VERSION")); msg.append(" compiled on "); msg.append(props.getProperty("DATE")); antVersion = msg.toString(); } catch (IOException ioe) { throw new BuildException("Could not load the version information:" + ioe.getMessage()); } catch (NullPointerException npe) { throw new BuildException("Could not load the version information."); } } return antVersion; } /** * Prints the description of a project (if there is one) to * <code>System.out</code>. * * @param project The project to display a description of. * Must not be <code>null</code>. */ private static void printDescription(Project project) { if (project.getDescription() != null) { project.log(project.getDescription()); } } /** * Targets in imported files with a project name * and not overloaded by the main build file will * be in the target map twice. This method * removes the duplicate target. * @param targets the targets to filter. * @return the filtered targets. */ private static Map removeDuplicateTargets(Map targets) { Map locationMap = new HashMap(); for (Iterator i = targets.entrySet().iterator(); i.hasNext();) { Map.Entry entry = (Map.Entry) i.next(); String name = (String) entry.getKey(); Target target = (Target) entry.getValue(); Target otherTarget = (Target) locationMap.get(target.getLocation()); // Place this entry in the location map if // a) location is not in the map // b) location is in map, but it's name is longer // (an imported target will have a name. prefix) if (otherTarget == null || otherTarget.getName().length() > name.length()) { locationMap.put( target.getLocation(), target); // Smallest name wins } } Map ret = new HashMap(); for (Iterator i = locationMap.values().iterator(); i.hasNext();) { Target target = (Target) i.next(); ret.put(target.getName(), target); } return ret; } /** * Prints a list of all targets in the specified project to * <code>System.out</code>, optionally including subtargets. * * @param project The project to display a description of. * Must not be <code>null</code>. * @param printSubTargets Whether or not subtarget names should also be * printed. */ private static void printTargets(Project project, boolean printSubTargets) { // find the target with the longest name int maxLength = 0; Map ptargets = removeDuplicateTargets(project.getTargets()); String targetName; String targetDescription; Target currentTarget; // split the targets in top-level and sub-targets depending // on the presence of a description Vector topNames = new Vector(); Vector topDescriptions = new Vector(); Vector subNames = new Vector(); for (Iterator i = ptargets.values().iterator(); i.hasNext();) { currentTarget = (Target) i.next(); targetName = currentTarget.getName(); if (targetName.equals("")) { continue; } targetDescription = currentTarget.getDescription(); // maintain a sorted list of targets if (targetDescription == null) { int pos = findTargetPosition(subNames, targetName); subNames.insertElementAt(targetName, pos); } else { int pos = findTargetPosition(topNames, targetName); topNames.insertElementAt(targetName, pos); topDescriptions.insertElementAt(targetDescription, pos); if (targetName.length() > maxLength) { maxLength = targetName.length(); } } } printTargets(project, topNames, topDescriptions, "Main targets:", maxLength); //if there were no main targets, we list all subtargets //as it means nothing has a description if (topNames.size() == 0) { printSubTargets = true; } if (printSubTargets) { printTargets(project, subNames, null, "Other targets:", 0); } String defaultTarget = project.getDefaultTarget(); if (defaultTarget != null && !"".equals(defaultTarget)) { // shouldn't need to check but... project.log("Default target: " + defaultTarget); } } /** * Searches for the correct place to insert a name into a list so as * to keep the list sorted alphabetically. * * @param names The current list of names. Must not be <code>null</code>. * @param name The name to find a place for. * Must not be <code>null</code>. * * @return the correct place in the list for the given name */ private static int findTargetPosition(Vector names, String name) { int res = names.size(); for (int i = 0; i < names.size() && res == names.size(); i++) { if (name.compareTo((String) names.elementAt(i)) < 0) { res = i; } } return res; } /** * Writes a formatted list of target names to <code>System.out</code> * with an optional description. * * * @param project the project instance. * @param names The names to be printed. * Must not be <code>null</code>. * @param descriptions The associated target descriptions. * May be <code>null</code>, in which case * no descriptions are displayed. * If non-<code>null</code>, this should have * as many elements as <code>names</code>. * @param heading The heading to display. * Should not be <code>null</code>. * @param maxlen The maximum length of the names of the targets. * If descriptions are given, they are padded to this * position so they line up (so long as the names really * <i>are</i> shorter than this). */ private static void printTargets(Project project, Vector names, Vector descriptions, String heading, int maxlen) { // now, start printing the targets and their descriptions String lSep = System.getProperty("line.separator"); // got a bit annoyed that I couldn't find a pad function String spaces = " "; while (spaces.length() <= maxlen) { spaces += spaces; } StringBuffer msg = new StringBuffer(); msg.append(heading + lSep + lSep); for (int i = 0; i < names.size(); i++) { msg.append(" "); msg.append(names.elementAt(i)); if (descriptions != null) { msg.append( spaces.substring(0, maxlen - ((String) names.elementAt(i)).length() + 2)); msg.append(descriptions.elementAt(i)); } msg.append(lSep); } project.log(msg.toString(), Project.MSG_WARN); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -