Browse Source

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.

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@421588 13f79535-47bb-0310-9956-ffa450edef68
master
Steve Loughran 19 years ago
parent
commit
91ee1ced95
2 changed files with 47 additions and 13 deletions
  1. +17
    -4
      src/main/org/apache/tools/ant/Main.java
  2. +30
    -9
      src/main/org/apache/tools/ant/launch/Launcher.java

+ 17
- 4
src/main/org/apache/tools/ant/Main.java View File

@@ -26,6 +26,8 @@ import java.io.PrintStream;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.Properties; import java.util.Properties;
import java.util.Vector; import java.util.Vector;
import java.util.HashMap;

import org.apache.tools.ant.input.DefaultInputHandler; import org.apache.tools.ant.input.DefaultInputHandler;
import org.apache.tools.ant.input.InputHandler; import org.apache.tools.ant.input.InputHandler;
import org.apache.tools.ant.launch.AntMain; import org.apache.tools.ant.launch.AntMain;
@@ -271,7 +273,7 @@ public class Main implements AntMain {


/** /**
* Process command line arguments. * 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. * passed through to this routine.
* *
* @param args the command line arguments. * @param args the command line arguments.
@@ -282,6 +284,15 @@ public class Main implements AntMain {
String searchForThis = null; String searchForThis = null;
PrintStream logTo = 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 // cycle through given args


for (int i = 0; i < args.length; i++) { for (int i = 0; i < args.length; i++) {
@@ -431,7 +442,7 @@ public class Main implements AntMain {
throw new BuildException( throw new BuildException(
"Niceness value is out of the range 1-10"); "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 //catch script/ant mismatch with a meaningful message
//we could ignore it, but there are likely to be other //we could ignore it, but there are likely to be other
//version problems, so we stamp down on the configuration now //version problems, so we stamp down on the configuration now
@@ -445,7 +456,7 @@ public class Main implements AntMain {
} else if (arg.startsWith("-")) { } else if (arg.startsWith("-")) {
// we don't have any more args to recognize! // we don't have any more args to recognize!
String msg = "Unknown argument: " + arg; String msg = "Unknown argument: " + arg;
System.out.println(msg);
System.err.println(msg);
printUsage(); printUsage();
throw new BuildException(""); throw new BuildException("");
} else { } else {
@@ -835,7 +846,9 @@ public class Main implements AntMain {
msg.append(" -nouserlib Run ant without using the jar files from" + lSep msg.append(" -nouserlib Run ant without using the jar files from" + lSep
+ " ${user.home}/.ant/lib" + lSep); + " ${user.home}/.ant/lib" + lSep);
msg.append(" -noclasspath Run ant without using CLASSPATH" + 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()); System.out.println(msg.toString());
} }




+ 30
- 9
src/main/org/apache/tools/ant/launch/Launcher.java View File

@@ -95,16 +95,24 @@ public class Launcher {
* @param args commandline arguments * @param args commandline arguments
*/ */
public static void main(String[] args) { public static void main(String[] args) {
int exitCode;
try { try {
Launcher launcher = new Launcher(); Launcher launcher = new Launcher();
launcher.run(args);
exitCode=launcher.run(args);
} catch (LaunchException e) { } catch (LaunchException e) {
exitCode=-1;
System.err.println(e.getMessage()); System.err.println(e.getMessage());
} catch (Throwable t) { } 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. * Add a CLASSPATH or -lib to lib path urls.
* *
@@ -138,17 +146,19 @@ public class Launcher {
* Run the launcher to launch Ant. * Run the launcher to launch Ant.
* *
* @param args the command line arguments * @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 * @exception MalformedURLException if the URLs required for the classloader
* cannot be created. * cannot be created.
*/ */
private void run(String[] args)
private int run(String[] args)
throws LaunchException, MalformedURLException { throws LaunchException, MalformedURLException {
String antHomeProperty = System.getProperty(MagicNames.ANT_HOME); String antHomeProperty = System.getProperty(MagicNames.ANT_HOME);
File antHome = null; File antHome = null;


File sourceJar = Locator.getClassSource(getClass()); File sourceJar = Locator.getClassSource(getClass());
File jarDir = sourceJar.getParentFile(); File jarDir = sourceJar.getParentFile();
String mainClassname = MAIN_CLASS;


if (antHomeProperty != null) { if (antHomeProperty != null) {
antHome = new File(antHomeProperty); antHome = new File(antHomeProperty);
@@ -192,6 +202,12 @@ public class Launcher {
noUserLib = true; noUserLib = true;
} else if (args[i].equals("--noclasspath") || args[i].equals("-noclasspath")) { } else if (args[i].equals("--noclasspath") || args[i].equals("-noclasspath")) {
noClassPath = true; 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 { } else {
argList.add(args[i]); argList.add(args[i]);
} }
@@ -273,19 +289,24 @@ public class Launcher {
URLClassLoader loader = new URLClassLoader(jars); URLClassLoader loader = new URLClassLoader(jars);
Thread.currentThread().setContextClassLoader(loader); Thread.currentThread().setContextClassLoader(loader);
Class mainClass = null; Class mainClass = null;
int exitCode=0;
try { try {
mainClass = loader.loadClass(MAIN_CLASS);
mainClass = loader.loadClass(mainClassname);
AntMain main = (AntMain) mainClass.newInstance(); AntMain main = (AntMain) mainClass.newInstance();
main.startAnt(newArgs, null, null); main.startAnt(newArgs, null, null);
} catch (InstantiationException ex) { } 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); File mainJar = Locator.getClassSource(mainClass);
System.out.println(
System.err.println(
"Location of this class " + mainJar); "Location of this class " + mainJar);
exitCode=-1;
} catch (Throwable t) { } catch (Throwable t) {
t.printStackTrace();
t.printStackTrace(System.err);
exitCode=-1;
} }
return exitCode;
} }


} }

Loading…
Cancel
Save