📄 main.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */package org.apache.tools.ant;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.PrintStream;import java.util.Enumeration;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.Map;import java.util.Properties;import java.util.Set;import java.util.Vector;import org.apache.tools.ant.input.DefaultInputHandler;import org.apache.tools.ant.input.InputHandler;import org.apache.tools.ant.launch.AntMain;import org.apache.tools.ant.util.ClasspathUtils;import org.apache.tools.ant.util.FileUtils;import org.apache.tools.ant.util.ProxySetup;/** * Command line entry point into Ant. This class is entered via the * canonical `public static void main` entry point and reads the * command line arguments. It then assembles and executes an Ant * project. * <p> * If you integrating Ant into some other tool, this is not the class * to use as an entry point. Please see the source code of this * class to see how it manipulates the Ant project classes. * */public class Main implements AntMain { /** * A Set of args are are handled by the launcher and should * not be seen by Main. */ private static final Set LAUNCH_COMMANDS = new HashSet(); static { LAUNCH_COMMANDS.add("-lib"); LAUNCH_COMMANDS.add("-cp"); LAUNCH_COMMANDS.add("-noclasspath"); LAUNCH_COMMANDS.add("--noclasspath"); LAUNCH_COMMANDS.add("-nouserlib"); LAUNCH_COMMANDS.add("-main"); } /** The default build file name. {@value} */ public static final String DEFAULT_BUILD_FILENAME = "build.xml"; /** Our current message output status. Follows Project.MSG_XXX. */ private int msgOutputLevel = Project.MSG_INFO; /** File that we are using for configuration. */ private File buildFile; /* null */ /** Stream to use for logging. */ private static PrintStream out = System.out; /** Stream that we are using for logging error messages. */ private static PrintStream err = System.err; /** The build targets. */ private Vector targets = new Vector(); /** Set of properties that can be used by tasks. */ private Properties definedProps = new Properties(); /** Names of classes to add as listeners to project. */ private Vector listeners = new Vector(1); /** File names of property files to load on startup. */ private Vector propertyFiles = new Vector(1); /** Indicates whether this build is to support interactive input */ private boolean allowInput = true; /** keep going mode */ private boolean keepGoingMode = false; /** * The Ant logger class. There may be only one logger. It will have * the right to use the 'out' PrintStream. The class must implements the * BuildLogger interface. */ private String loggerClassname = null; /** * The Ant InputHandler class. There may be only one input * handler. */ private String inputHandlerClassname = null; /** * Whether or not output to the log is to be unadorned. */ private boolean emacsMode = false; /** * Whether or not this instance has successfully been * constructed and is ready to run. */ private boolean readyToRun = false; /** * Whether or not we should only parse and display the project help * information. */ private boolean projectHelp = false; /** * Whether or not a logfile is being used. This is used to * check if the output streams must be closed. */ private static boolean isLogFileUsed = false; /** * optional thread priority */ private Integer threadPriority = null; /** * proxy flag: default is false */ private boolean proxy = false; /** * Prints the message of the Throwable if it (the message) is not * <code>null</code>. * * @param t Throwable to print the message of. * Must not be <code>null</code>. */ private static void printMessage(Throwable t) { String message = t.getMessage(); if (message != null) { System.err.println(message); } } /** * Creates a new instance of this class using the * arguments specified, gives it any extra user properties which have been * specified, and then runs the build using the classloader provided. * * @param args Command line arguments. Must not be <code>null</code>. * @param additionalUserProperties Any extra properties to use in this * build. May be <code>null</code>, which is the equivalent to * passing in an empty set of properties. * @param coreLoader Classloader used for core classes. May be * <code>null</code> in which case the system classloader is used. */ public static void start(String[] args, Properties additionalUserProperties, ClassLoader coreLoader) { Main m = new Main(); m.startAnt(args, additionalUserProperties, coreLoader); } /** * Start Ant * @param args command line args * @param additionalUserProperties properties to set beyond those that * may be specified on the args list * @param coreLoader - not used * * @since Ant 1.6 */ public void startAnt(String[] args, Properties additionalUserProperties, ClassLoader coreLoader) { try { Diagnostics.validateVersion(); processArgs(args); } catch (Throwable exc) { handleLogfile(); printMessage(exc); exit(1); return; } if (additionalUserProperties != null) { for (Enumeration e = additionalUserProperties.keys(); e.hasMoreElements();) { String key = (String) e.nextElement(); String property = additionalUserProperties.getProperty(key); definedProps.put(key, property); } } // expect the worst int exitCode = 1; try { try { runBuild(coreLoader); exitCode = 0; } catch (ExitStatusException ese) { exitCode = ese.getStatus(); if (exitCode != 0) { throw ese; } } } catch (BuildException be) { if (err != System.err) { printMessage(be); } } catch (Throwable exc) { exc.printStackTrace(); printMessage(exc); } finally { handleLogfile(); } exit(exitCode); } /** * This operation is expected to call {@link System#exit(int)}, which * is what the base version does. * However, it is possible to do something else. * @param exitCode code to exit with */ protected void exit(int exitCode) { System.exit(exitCode); } /** * Close logfiles, if we have been writing to them. * * @since Ant 1.6 */ private static void handleLogfile() { if (isLogFileUsed) { FileUtils.close(out); FileUtils.close(err); } } /** * Command line entry point. This method kicks off the building * of a project object and executes a build using either a given * target or the default target. * * @param args Command line arguments. Must not be <code>null</code>. */ public static void main(String[] args) { start(args, null, null); } /** * Constructor used when creating Main for later arg processing * and startup */ public Main() { } /** * Sole constructor, which parses and deals with command line * arguments. * * @param args Command line arguments. Must not be <code>null</code>. * * @exception BuildException if the specified build file doesn't exist * or is a directory. * * @deprecated since 1.6.x */ protected Main(String[] args) throws BuildException { processArgs(args); } /** * Process command line arguments. * When ant is started from Launcher, launcher-only arguments do not get * passed through to this routine. * * @param args the command line arguments. * * @since Ant 1.6 */ private void processArgs(String[] args) { String searchForThis = null; PrintStream logTo = null; // cycle through given args for (int i = 0; i < args.length; i++) { String arg = args[i]; if (arg.equals("-help") || arg.equals("-h")) { printUsage(); return; } else if (arg.equals("-version")) { printVersion(); return; } else if (arg.equals("-diagnostics")) { Diagnostics.doReport(System.out); return; } else if (arg.equals("-quiet") || arg.equals("-q")) { msgOutputLevel = Project.MSG_WARN; } else if (arg.equals("-verbose") || arg.equals("-v")) { printVersion(); msgOutputLevel = Project.MSG_VERBOSE; } else if (arg.equals("-debug") || arg.equals("-d")) { printVersion(); msgOutputLevel = Project.MSG_DEBUG; } else if (arg.equals("-noinput")) { allowInput = false; } else if (arg.equals("-logfile") || arg.equals("-l")) { try { File logFile = new File(args[i + 1]); i++; logTo = new PrintStream(new FileOutputStream(logFile)); isLogFileUsed = true; } catch (IOException ioe) { String msg = "Cannot write on the specified log file. " + "Make sure the path exists and you have write " + "permissions."; throw new BuildException(msg); } catch (ArrayIndexOutOfBoundsException aioobe) { String msg = "You must specify a log file when " + "using the -log argument"; throw new BuildException(msg); } } else if (arg.equals("-buildfile") || arg.equals("-file") || arg.equals("-f")) { i = handleArgBuildFile(args, i); } else if (arg.equals("-listener")) { i = handleArgListener(args, i); } else if (arg.startsWith("-D")) { i = handleArgDefine(args, i); } else if (arg.equals("-logger")) { i = handleArgLogger(args, i); } else if (arg.equals("-inputhandler")) { i = handleArgInputHandler(args, i); } else if (arg.equals("-emacs") || arg.equals("-e")) { emacsMode = true; } else if (arg.equals("-projecthelp") || arg.equals("-p")) { // set the flag to display the targets and quit projectHelp = true; } else if (arg.equals("-find") || arg.equals("-s")) { // eat up next arg if present, default to build.xml if (i < args.length - 1) { searchForThis = args[++i]; } else { searchForThis = DEFAULT_BUILD_FILENAME; } } else if (arg.startsWith("-propertyfile")) { i = handleArgPropertyFile(args, i); } else if (arg.equals("-k") || arg.equals("-keep-going")) { keepGoingMode = true; } else if (arg.equals("-nice")) { i = handleArgNice(args, i); } else if (LAUNCH_COMMANDS.contains(arg)) { //catch script/ant mismatch with a meaningful message //we could ignore it, but there are likely to be other //version problems, so we stamp down on the configuration now String msg = "Ant's Main method is being handed "
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -