duncan 99/10/31 15:06:37
Modified: . ant.jar
ant/src/main/org/apache/tools/ant Main.java Project.java
Log:
The way properties can be defined has been updated. To override a default
project property, use the `-Dpropname=value` syntax. This follows the same
pattern as setting system properties when executing the java interpreter.
Also, starting to beef up documentation of internal code. Needs a lot more
work, but this is a start.
Revision Changes Path
1.10 +91 -102 jakarta-tools/ant.jar
<<Binary file>>
1.3 +75 -32 jakarta-tools/ant/src/main/org/apache/tools/ant/Main.java
Index: Main.java
===================================================================
RCS file: /home/cvs/jakarta-tools/ant/src/main/org/apache/tools/ant/Main.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- Main.java 1999/10/11 01:57:18 1.2
+++ Main.java 1999/10/31 23:06:36 1.3
@@ -3,31 +3,42 @@
import java.io.File;
import java.util.Properties;
import java.util.Enumeration;
+
/**
- * Command line entry point into BuildTool.
+ * Command line entry point into Ant. This class is entered via the
+ * cannonical `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.
*
* @author duncan@x180.com
*/
public class Main {
-
+
private static int msgOutputLevel = Project.MSG_INFO;
private static File buildFile = new File("build.xml");
private static String target = null;
+ private static Properties definedProps = new Properties();
- private static Properties defines=new Properties();
/**
- *
+ * 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 args.
*/
- public static void main(String[] args) {
-
+ public static void main(String[] args) {
+
// cycle through given args
-
+
for (int i = 0; i < args.length; i++) {
String arg = args[i];
+
if (arg.equals("-help") || arg.equals("help")) {
printUsage();
return;
@@ -37,12 +48,7 @@
} else if (arg.equals("-verbose") || arg.equals("-v") ||
arg.equals("v")) {
msgOutputLevel = Project.MSG_VERBOSE;
- } else if (arg.equals("-define") || arg.equals("-d")) {
- String n=args[i+1];
- String v=args[i+2];
- i+=2;
- defines.put( n, v );
- } else if (arg.equals("-buildfile") || arg.equals("-file")) {
+ } else if (arg.equals("-buildfile") || arg.equals("-file")) {
try {
buildFile = new File(args[i+1]);
i++;
@@ -52,7 +58,11 @@
System.out.println(msg);
return;
}
- } else if (arg.startsWith("-")) {
+ } else if (arg.startsWith("-D")) {
+ arg = arg.substring(2, arg.length());
+ String value = args[++i];
+ definedProps.put(arg, value);
+ } else if (arg.startsWith("-")) {
// we don't have any more args to recognize!
String msg = "Unknown arg: " + arg;
System.out.println(msg);
@@ -63,43 +73,46 @@
target = arg;
}
}
-
+
// make sure buildfile exists
-
+
if (!buildFile.exists()) {
System.out.println("Buildfile: " + buildFile + " does not exist!");
return;
}
-
+
// make sure it's not a directory (this falls into the ultra
// paranoid lets check everything catagory
-
+
if (buildFile.isDirectory()) {
System.out.println("What? Buildfile: " + buildFile + " is a dir!");
return;
}
// ok, so if we've made it here, let's run the damn build allready
-
+
runBuild();
}
+ /**
+ * Executes the build.
+ */
+
private static void runBuild() {
+
+ // track when we started
+
long startTime = System.currentTimeMillis();
if (msgOutputLevel >= Project.MSG_INFO) {
System.out.println("Buildfile: " + buildFile);
}
-
+
Project project = new Project();
- Enumeration preDef=defines.keys();
- while( preDef.hasMoreElements() ) {
- String n=(String)preDef.nextElement();
- String v=(String)defines.get( n );
- project.setProperty( n, v );
- }
-
project.setOutputLevel(msgOutputLevel);
+ // first use the ProjectHelper to create the project object
+ // from the given build file.
+
try {
ProjectHelper.configureProject(project, buildFile);
} catch (BuildException be) {
@@ -107,11 +120,26 @@
System.out.println(msg + be.getMessage());
return;
}
-
+
+ // cycle through command line defined properties after the
+ // build.xml file properties have been set so that command line
+ // props take precedence
+
+ Enumeration e = definedProps.keys();
+ while (e.hasMoreElements()) {
+ String arg = (String)e.nextElement();
+ String value = (String)definedProps.get(arg);
+ project.setProperty(arg, value);
+ }
+
+ // make sure that we have a target to execute
+
if (target == null) {
target = project.getDefaultTarget();
}
+ // actually do some work
+
try {
project.executeTarget(target);
} catch (BuildException be) {
@@ -119,7 +147,10 @@
System.out.println(msg + be.getMessage());
return;
}
-
+
+ // track our stop time and let the user know how long things
+ // took.
+
long finishTime = System.currentTimeMillis();
long elapsedTime = finishTime - startTime;
if (msgOutputLevel >= Project.MSG_INFO) {
@@ -128,10 +159,22 @@
}
}
+ /**
+ * Prints the usage of how to use this class to System.out
+ */
+
private static void printUsage() {
- String msg = "javab [-help] [-quiet] [-verbose] " +
- "[-buildfile buildfile] target";
- System.out.println(msg);
+ String lSep = System.getProperty("line.separator");
+ StringBuffer msg = new StringBuffer();
+ msg.append("ant [options] [target]" + lSep);
+ msg.append("Options: " + lSep);
+ msg.append(" -help print this message" + lSep);
+ msg.append(" -quiet be extra quiet" + lSep);
+ msg.append(" -verbose be extra verbose" + lSep);
+ msg.append(" -buildfile <file> use given buildfile" + lSep);
+ msg.append(" -D<property>=<value> use value for given property"
+ + lSep);
+ System.out.println(msg.toString());
}
}
1.6 +9 -5 jakarta-tools/ant/src/main/org/apache/tools/ant/Project.java
Index: Project.java
===================================================================
RCS file: /home/cvs/jakarta-tools/ant/src/main/org/apache/tools/ant/Project.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- Project.java 1999/10/22 19:54:45 1.5
+++ Project.java 1999/10/31 23:06:37 1.6
@@ -10,8 +10,14 @@
import java.util.StringTokenizer;
/**
+ * Central representation of an Ant project. This class defines a
+ * Ant project with all of it's targets and tasks. It also provides
+ * the mechanism to kick off a build using a particular target name.
+ * <p>
+ * This class also encapsulates methods which allow Files to be refered
+ * to using abstract path names which are translated to native system
+ * file paths at runtime as well as defining various project properties.
*
- *
* @author duncan@x180.com
*/
@@ -79,10 +85,8 @@
}
public void setProperty(String name, String value) {
- if( properties.get( name ) != null )
- return; // this way command line arguments have priority.
- // XXX we need a way to redefine properties
-
+ log("Setting project property: " + name + " to " +
+ value, MSG_VERBOSE);
properties.put(name, value);
}
|