ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ste...@apache.org
Subject svn commit: r421588 - in /ant/core/trunk/src/main/org/apache/tools/ant: Main.java launch/Launcher.java
Date Thu, 13 Jul 2006 11:11:22 GMT
Author: stevel
Date: Thu Jul 13 04:11:21 2006
New Revision: 421588

URL: http://svn.apache.org/viewvc?rev=421588&view=rev
Log:
This is a little bit of fun; something asked for on the mail list. You can now use -main to
specify a new entry point for ant, so if you override ant.Main with a subclass, you can switch
to that.

It has a side effect of making ant a generic launcher of things; anything that implements
AntMain.startAnt.

Modified:
    ant/core/trunk/src/main/org/apache/tools/ant/Main.java
    ant/core/trunk/src/main/org/apache/tools/ant/launch/Launcher.java

Modified: ant/core/trunk/src/main/org/apache/tools/ant/Main.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/Main.java?rev=421588&r1=421587&r2=421588&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/Main.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/Main.java Thu Jul 13 04:11:21 2006
@@ -26,6 +26,8 @@
 import java.util.Enumeration;
 import java.util.Properties;
 import java.util.Vector;
+import java.util.HashMap;
+
 import org.apache.tools.ant.input.DefaultInputHandler;
 import org.apache.tools.ant.input.InputHandler;
 import org.apache.tools.ant.launch.AntMain;
@@ -271,7 +273,7 @@
 
     /**
      * Process command line arguments.
-     * When ant is started from Launcher, the -lib argument does not get
+     * When ant is started from Launcher, launcher-only arguments doe not get
      * passed through to this routine.
      *
      * @param args the command line arguments.
@@ -282,6 +284,15 @@
         String searchForThis = null;
         PrintStream logTo = null;
 
+        //this is hte list of lu
+        HashMap launchCommands =new HashMap();
+        launchCommands.put("-lib","");
+        launchCommands.put("-cp", "");
+        launchCommands.put("-noclasspath", "");
+        launchCommands.put("--noclasspath", "");
+        launchCommands.put("-nouserlib", "");
+        launchCommands.put("--nouserlib", "");
+        launchCommands.put("-main", "");
         // cycle through given args
 
         for (int i = 0; i < args.length; i++) {
@@ -431,7 +442,7 @@
                     throw new BuildException(
                             "Niceness value is out of the range 1-10");
                 }
-            } else if (arg.equals("-cp") || arg.equals("-lib")) {
+            } else if (launchCommands.get(arg)!=null) {
                 //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
@@ -445,7 +456,7 @@
             } else if (arg.startsWith("-")) {
                 // we don't have any more args to recognize!
                 String msg = "Unknown argument: " + arg;
-                System.out.println(msg);
+                System.err.println(msg);
                 printUsage();
                 throw new BuildException("");
             } else {
@@ -835,7 +846,9 @@
         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("  -noproxy               Java 1.5 only: do not use the OS proxies");
+        msg.append("  -noproxy               Java 1.5 only: do not use the OS proxies" +
+                lSep);
+        msg.append("  -main <class>          override Ant's normal entry point");
         System.out.println(msg.toString());
     }
 

Modified: ant/core/trunk/src/main/org/apache/tools/ant/launch/Launcher.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/launch/Launcher.java?rev=421588&r1=421587&r2=421588&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/launch/Launcher.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/launch/Launcher.java Thu Jul 13 04:11:21
2006
@@ -95,16 +95,24 @@
      * @param  args commandline arguments
      */
     public static void main(String[] args) {
+        int exitCode;
         try {
             Launcher launcher = new Launcher();
-            launcher.run(args);
+            exitCode=launcher.run(args);
         } catch (LaunchException e) {
+            exitCode=-1;
             System.err.println(e.getMessage());
         } catch (Throwable t) {
-            t.printStackTrace();
+            exitCode=-1;
+            t.printStackTrace(System.err);
+        }
+        if(exitCode!=0) {
+            System.exit(exitCode);
         }
     }
 
+    
+    
     /**
      * Add a CLASSPATH or -lib to lib path urls.
      *
@@ -138,17 +146,19 @@
      * Run the launcher to launch Ant.
      *
      * @param args the command line arguments
-     *
+     * @return an exit code. As the normal ant main calls exit when it ends,
+     *         this is for handling failures at bind-time
      * @exception MalformedURLException if the URLs required for the classloader
      *            cannot be created.
      */
-    private void run(String[] args)
+    private int run(String[] args)
             throws LaunchException, MalformedURLException {
         String antHomeProperty = System.getProperty(MagicNames.ANT_HOME);
         File antHome = null;
 
         File sourceJar = Locator.getClassSource(getClass());
         File jarDir = sourceJar.getParentFile();
+        String mainClassname = MAIN_CLASS;
 
         if (antHomeProperty != null) {
             antHome = new File(antHomeProperty);
@@ -192,6 +202,12 @@
                 noUserLib = true;
             } else if (args[i].equals("--noclasspath") || args[i].equals("-noclasspath"))
{
                 noClassPath = true;
+            } else if (args[i].equals("-main")) {
+                if (i == args.length - 1) {
+                    throw new LaunchException("The -main argument must "
+                            + "be followed by a library location");
+                }
+                mainClassname = args[++i];
             } else {
                 argList.add(args[i]);
             }
@@ -273,19 +289,24 @@
         URLClassLoader loader = new URLClassLoader(jars);
         Thread.currentThread().setContextClassLoader(loader);
         Class mainClass = null;
+        int exitCode=0;
         try {
-            mainClass = loader.loadClass(MAIN_CLASS);
+            mainClass = loader.loadClass(mainClassname);
             AntMain main = (AntMain) mainClass.newInstance();
             main.startAnt(newArgs, null, null);
         } catch (InstantiationException ex) {
-            System.out.println(
-                "Incompatible version of org.apache.tools.ant detected");
+            System.err.println(
+                "Incompatible version of "+mainClassname+" detected");
             File mainJar = Locator.getClassSource(mainClass);
-            System.out.println(
+            System.err.println(
                 "Location of this class " + mainJar);
+            exitCode=-1;
         } catch (Throwable t) {
-            t.printStackTrace();
+            t.printStackTrace(System.err);
+            exitCode=-1;
         }
+        return exitCode;
+        
     }
 
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Mime
View raw message