diff --git a/WHATSNEW b/WHATSNEW index 99e2bd693..492c89756 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -10,11 +10,13 @@ Changes that could break older environments: * Path and EnumeratedAttribute have been moved from org.apache.tools.ant to org.apache.tools.ant.types. +* the class attribute of has been removed. + Other changes: -------------- -* New tasks: sql, junit, javacc, execon. All pending documentation, -most of them pending review. +* New tasks: sql, junit, mparse, execon. All except sql pending +documentation, most of them pending review. * uses ClassLoader of its own in no-fork mode if a classpath is specified. diff --git a/build.xml b/build.xml index 599fb765e..fdbb1d246 100644 --- a/build.xml +++ b/build.xml @@ -295,8 +295,8 @@ + diff --git a/src/main/org/apache/tools/ant/taskdefs/ExecuteJava.java b/src/main/org/apache/tools/ant/taskdefs/ExecuteJava.java index f45783ae6..fbe7be5f7 100644 --- a/src/main/org/apache/tools/ant/taskdefs/ExecuteJava.java +++ b/src/main/org/apache/tools/ant/taskdefs/ExecuteJava.java @@ -55,9 +55,11 @@ package org.apache.tools.ant.taskdefs; +import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.BuildException; -import org.apache.tools.ant.types.Path; +import org.apache.tools.ant.Project; import org.apache.tools.ant.types.Commandline; +import org.apache.tools.ant.types.Path; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -69,17 +71,28 @@ import java.lang.reflect.Method; public class ExecuteJava { private Commandline javaCommand = null; + private Path classpath = null; public void setJavaCommand(Commandline javaCommand) { this.javaCommand = javaCommand; } - public void execute() throws BuildException{ + public void setClasspath(Path p) { + classpath = p; + } + + public void execute(Project project) throws BuildException{ final String classname = javaCommand.getExecutable(); final Object[] argument = { javaCommand.getArguments() }; - final Class[] param = { argument[0].getClass() }; try { - final Class target = Class.forName(classname); + final Class[] param = { Class.forName("[Ljava.lang.String;") }; + Class target = null; + if (classpath == null) { + target = Class.forName(classname); + } else { + AntClassLoader loader = new AntClassLoader(project, classpath); + target = loader.forceLoadClass(classname); + } final Method main = target.getMethod("main", param); main.invoke(null, argument); } catch (NullPointerException e) { @@ -89,7 +102,7 @@ public class ExecuteJava { } catch (InvocationTargetException e) { Throwable t = e.getTargetException(); if (!(t instanceof SecurityException)) { - throw new BuildException(t.toString()); + throw new BuildException(t); } // else ignore because the security exception is thrown // if the invoked application tried to call System.exit() diff --git a/src/main/org/apache/tools/ant/taskdefs/Java.java b/src/main/org/apache/tools/ant/taskdefs/Java.java index bd9b1fd7b..5ca68421d 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Java.java +++ b/src/main/org/apache/tools/ant/taskdefs/Java.java @@ -57,8 +57,13 @@ package org.apache.tools.ant.taskdefs; import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; +import org.apache.tools.ant.Task; +import org.apache.tools.ant.types.Commandline; +import org.apache.tools.ant.types.CommandlineJava; import org.apache.tools.ant.types.Path; +import java.io.File; +import java.io.IOException; import java.lang.reflect.*; import java.util.*; @@ -68,28 +73,35 @@ import java.util.*; * * @author Stefano Mazzocchi stefano@apache.org */ -public class Java extends Exec { +public class Java extends Task { - private String classname = null; - private String args = null; - private String jvmargs = null; - private Path classpath = null; + private CommandlineJava cmdl = new CommandlineJava(); private boolean fork = false; + private File dir = null; + private boolean failOnError = false; /** * Do the execution. */ public void execute() throws BuildException { - executeJava(); + int err = -1; + if ((err = executeJava()) != 0) { + if (failOnError) { + throw new BuildException("Java returned: "+err, location); + } else { + log("Java Result: " + err, Project.MSG_ERR); + } + } } /** * Do the execution and return a return code. * - * @return the return code from the execute java cklass if it was executed in + * @return the return code from the execute java class if it was executed in * a separate VM (fork = "yes"). */ public int executeJava() throws BuildException { + String classname = cmdl.getClassname(); log("Calling " + classname, Project.MSG_VERBOSE); if (classname == null) { @@ -97,32 +109,16 @@ public class Java extends Exec { } if (fork) { - StringBuffer b = new StringBuffer(); - b.append("java "); - if (classpath != null) { - b.append("-classpath "); - b.append(classpath.toString()); - b.append(" "); - } - if (jvmargs != null) { - b.append(jvmargs); - b.append(" "); + return run(cmdl.getCommandline()); + } else { + if (cmdl.getVmCommand().size() > 1) { + log("JVM args ignored when same JVM is used.", Project.MSG_WARN); } - b.append(classname); - if (args != null) { - b.append(" "); - b.append(args); + if (dir != null) { + log("Working directory ignored when same JVM is used.", Project.MSG_WARN); } - return run(b.toString()); - } else { - Vector argList = tokenize(args); - if (jvmargs != null) { - log("JVM args and classpath ignored when same JVM is used.", Project.MSG_VERBOSE); - } - - log("Java args: " + argList.toString(), Project.MSG_VERBOSE); - run(classname, argList); + run(cmdl); return 0; } } @@ -131,123 +127,117 @@ public class Java extends Exec { * Set the classpath to be used for this compilation. */ public void setClasspath(Path s) { - if (this.classpath == null) { - this.classpath = s; - } else { - this.classpath.append(s); - } + createClasspath().append(s); } /** * Creates a nested classpath element */ public Path createClasspath() { - if (classpath == null) { - classpath = new Path(project); - } - return classpath; + return cmdl.createClasspath(project); } /** - * Set the source file (deprecated). + * Set the class name. */ - public void setClass(String s) { - log("The class attribute is deprecated. " + - "Please use the classname attribute.", - Project.MSG_WARN); - this.classname = s; + public void setClassname(String s) { + cmdl.setClassname(s); } /** - * Set the source file. + * Set the command line arguments for the class. */ - public void setClassname(String s) { - this.classname = s; + public void setArgs(String s) { + cmdl.createArgument().setLine(s); } /** - * Set the destination file. + * Creates a nested arg element. */ - public void setArgs(String s) { - this.args = s; + public Commandline.Argument createArg() { + return cmdl.createArgument(); } /** * Set the forking flag. */ - public void setFork(String s) { - this.fork = Project.toBoolean(s); + public void setFork(boolean s) { + this.fork = s; } /** - * Set the jvm arguments. + * Set the command line arguments for the JVM. */ public void setJvmargs(String s) { - this.jvmargs = s; + cmdl.createVmArgument().setLine(s); } + /** + * Creates a nested jvmarg element. + */ + public Commandline.Argument createJvmarg() { + return cmdl.createVmArgument(); + } + + /** + * Throw a BuildException if process returns non 0. + */ + public void setFailonerror(boolean fail) { + failOnError = fail; + } + + /** + * The working directory of the process + */ + public void setDir(File d) { + this.dir = d; + } + /** * Executes the given classname with the given arguments as it * was a command line application. */ - protected void run(String classname, Vector args) throws BuildException { - try { - Class c = null; - if (classpath == null) { - c = Class.forName(classname); - } - else { - AntClassLoader loader = new AntClassLoader(project, classpath); - c = loader.forceLoadClass(classname); - } - - Class[] param = { Class.forName("[Ljava.lang.String;") }; - Method main = c.getMethod("main", param); - Object[] a = { array(args) }; - main.invoke(null, a); - } catch (NullPointerException e) { - throw new BuildException("Could not find main() method in " + classname); - } catch (ClassNotFoundException e) { - throw new BuildException("Could not find " + classname + ". Make sure you have it in your classpath"); - } catch (InvocationTargetException e) { - Throwable t = e.getTargetException(); - if (!(t instanceof SecurityException)) { - throw new BuildException(t.toString()); - } - // else ignore because the security exception is thrown - // if the invoked application tried to call System.exit() - } catch (Exception e) { - throw new BuildException(e.toString()); - } + private void run(CommandlineJava command) throws BuildException { + ExecuteJava exe = new ExecuteJava(); + exe.setJavaCommand(command.getJavaCommand()); + exe.setClasspath(command.getClasspath()); + exe.execute(project); } /** - * Transforms an argument string into a vector of strings. + * Executes the given classname with the given arguments in a separate VM. */ - protected Vector tokenize(String args) { - Vector v = new Vector(); - if (args == null) return v; + private int run(String[] command) throws BuildException { + Execute exe = new Execute(new LogStreamHandler(this, Project.MSG_INFO, + Project.MSG_WARN), + null); + exe.setAntRun(project); - StringTokenizer t = new StringTokenizer(args, " "); - - while (t.hasMoreTokens()) { - v.addElement(t.nextToken()); - } + if (dir == null) dir = project.getBaseDir(); + exe.setWorkingDirectory(dir); - return v; + exe.setCommandline(command); + try { + return exe.execute(); + } catch (IOException e) { + throw new BuildException(e, location); + } } - + /** - * Transforms a vector of strings into an array. + * Executes the given classname with the given arguments as it + * was a command line application. */ - protected String[] array(Vector v) { - String[] s = new String[v.size()]; - Enumeration e = v.elements(); - - for (int i = 0; e.hasMoreElements(); i++) { - s[i] = (String) e.nextElement(); + protected void run(String classname, Vector args) throws BuildException { + CommandlineJava cmdj = new CommandlineJava(); + cmdj.setClassname(classname); + for (int i=0; i