Browse Source

Provide a failonerror attribute for javac. This defaults to true. If you set it to

false, an error is logged but the build will continue.

Based on the idea by Ken Wood.


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@268396 13f79535-47bb-0310-9956-ffa450edef68
master
Conor MacNeill 24 years ago
parent
commit
3d76bc5d3a
1 changed files with 50 additions and 26 deletions
  1. +50
    -26
      src/main/org/apache/tools/ant/taskdefs/Javac.java

+ 50
- 26
src/main/org/apache/tools/ant/taskdefs/Javac.java View File

@@ -116,6 +116,7 @@ public class Javac extends MatchingTask {
private Path extdirs; private Path extdirs;
private static String lSep = System.getProperty("line.separator"); private static String lSep = System.getProperty("line.separator");


protected boolean failOnError = true;
protected File[] compileList = new File[0]; protected File[] compileList = new File[0];


/** /**
@@ -229,6 +230,20 @@ public class Javac extends MatchingTask {
return extdirs.createPath(); return extdirs.createPath();
} }


/**
* Throw a BuildException if compilation fails
*/
public void setFailonerror(boolean fail) {
failOnError = fail;
}

/**
* Proceed if compilation fails
*/
public void setProceed(boolean proceed) {
failOnError = !proceed;
}

/** /**
* Set the deprecation flag. * Set the deprecation flag.
*/ */
@@ -330,18 +345,29 @@ public class Javac extends MatchingTask {
+ (compileList.length == 1 ? "" : "s") + (compileList.length == 1 ? "" : "s")
+ (destDir != null ? " to " + destDir : "")); + (destDir != null ? " to " + destDir : ""));


boolean compileSucceeded = false;

if (compiler.equalsIgnoreCase("classic")) { if (compiler.equalsIgnoreCase("classic")) {
doClassicCompile(); compileSucceeded = doClassicCompile();
} else if (compiler.equalsIgnoreCase("modern")) { } else if (compiler.equalsIgnoreCase("modern")) {
doModernCompile(); compileSucceeded = doModernCompile();
} else if (compiler.equalsIgnoreCase("jikes")) { } else if (compiler.equalsIgnoreCase("jikes")) {
doJikesCompile(); compileSucceeded = doJikesCompile();
} else if (compiler.equalsIgnoreCase("jvc")) { } else if (compiler.equalsIgnoreCase("jvc")) {
doJvcCompile(); compileSucceeded = doJvcCompile();
} else { } else {
String msg = "Don't know how to use compiler " + compiler; String msg = "Don't know how to use compiler " + compiler;
throw new BuildException(msg, location); throw new BuildException(msg, location);
} }
if (!compileSucceeded) {
if (failOnError) {
throw new BuildException(FAIL_MSG, location);
}
else {
log(FAIL_MSG, Project.MSG_ERR);
}
}
} }
} }


@@ -456,9 +482,10 @@ public class Javac extends MatchingTask {
/** /**
* Peforms a compile using the classic compiler that shipped with * Peforms a compile using the classic compiler that shipped with
* JDK 1.1 and 1.2. * JDK 1.1 and 1.2.
*
* @return true if the compile succeeded
*/ */

private boolean doClassicCompile() throws BuildException {
private void doClassicCompile() throws BuildException {
log("Using classic compiler", Project.MSG_VERBOSE); log("Using classic compiler", Project.MSG_VERBOSE);
Commandline cmd = setupJavacCommand(); Commandline cmd = setupJavacCommand();


@@ -483,9 +510,7 @@ public class Javac extends MatchingTask {
// Call the compile() method // Call the compile() method
Method compile = c.getMethod("compile", new Class [] { String[].class }); Method compile = c.getMethod("compile", new Class [] { String[].class });
Boolean ok = (Boolean)compile.invoke(compiler, new Object[] {cmd.getArguments()}); Boolean ok = (Boolean)compile.invoke(compiler, new Object[] {cmd.getArguments()});
if (!ok.booleanValue()) { return ok.booleanValue();
throw new BuildException(FAIL_MSG, location);
}
} }
catch (ClassNotFoundException ex) { catch (ClassNotFoundException ex) {
throw new BuildException("Cannot use classic compiler, as it is not available"+ throw new BuildException("Cannot use classic compiler, as it is not available"+
@@ -503,15 +528,15 @@ public class Javac extends MatchingTask {


/** /**
* Performs a compile using the newer compiler that ships with JDK 1.3 * Performs a compile using the newer compiler that ships with JDK 1.3
*
* @return true if the compile succeeded
*/ */

private boolean doModernCompile() throws BuildException {
private void doModernCompile() throws BuildException {
try { try {
Class.forName("com.sun.tools.javac.Main"); Class.forName("com.sun.tools.javac.Main");
} catch (ClassNotFoundException cnfe) { } catch (ClassNotFoundException cnfe) {
log("Modern compiler is not available - using classic compiler", Project.MSG_WARN); log("Modern compiler is not available - using classic compiler", Project.MSG_WARN);
doClassicCompile(); return doClassicCompile();
return;
} }


log("Using modern compiler", Project.MSG_VERBOSE); log("Using modern compiler", Project.MSG_VERBOSE);
@@ -532,9 +557,7 @@ public class Javac extends MatchingTask {
new Class [] {(new String [] {}).getClass ()}); new Class [] {(new String [] {}).getClass ()});
int result = ((Integer) compile.invoke int result = ((Integer) compile.invoke
(compiler, new Object[] {cmd.getArguments()})) .intValue (); (compiler, new Object[] {cmd.getArguments()})) .intValue ();
if (result != MODERN_COMPILER_SUCCESS) { return (result == MODERN_COMPILER_SUCCESS);
throw new BuildException(FAIL_MSG, location);
}
} catch (Exception ex) { } catch (Exception ex) {
if (ex instanceof BuildException) { if (ex instanceof BuildException) {
throw (BuildException) ex; throw (BuildException) ex;
@@ -653,9 +676,10 @@ public class Javac extends MatchingTask {
* It has been successfully tested with jikes >1.10 * It has been successfully tested with jikes >1.10
* *
* @author skanthak@muehlheim.de * @author skanthak@muehlheim.de
*
* @return true if the compile succeeded
*/ */

private boolean doJikesCompile() throws BuildException {
private void doJikesCompile() throws BuildException {
log("Using jikes compiler", Project.MSG_VERBOSE); log("Using jikes compiler", Project.MSG_VERBOSE);


Path classpath = new Path(project); Path classpath = new Path(project);
@@ -762,9 +786,7 @@ public class Javac extends MatchingTask {
int firstFileName = cmd.size(); int firstFileName = cmd.size();
logAndAddFilesToCompile(cmd); logAndAddFilesToCompile(cmd);


if (executeJikesCompile(cmd.getCommandline(), firstFileName) != 0) { return executeExternalCompile(cmd.getCommandline(), firstFileName) == 0;
throw new BuildException(FAIL_MSG, location);
}
} }


/** /**
@@ -772,7 +794,7 @@ public class Javac extends MatchingTask {
* @param args - arguments to pass to process on command line * @param args - arguments to pass to process on command line
* @param firstFileName - index of the first source file in args * @param firstFileName - index of the first source file in args
*/ */
protected int executeJikesCompile(String[] args, int firstFileName) { protected int executeExternalCompile(String[] args, int firstFileName) {
String[] commandArray = null; String[] commandArray = null;
File tmpFile = null; File tmpFile = null;


@@ -856,7 +878,11 @@ public class Javac extends MatchingTask {
} }
} }


private void doJvcCompile() throws BuildException { /*
*
* @return true if the compile succeeded
*/
private boolean doJvcCompile() throws BuildException {
log("Using jvc compiler", Project.MSG_VERBOSE); log("Using jvc compiler", Project.MSG_VERBOSE);


Path classpath = new Path(project); Path classpath = new Path(project);
@@ -906,9 +932,7 @@ public class Javac extends MatchingTask {
int firstFileName = cmd.size(); int firstFileName = cmd.size();
logAndAddFilesToCompile(cmd); logAndAddFilesToCompile(cmd);


if (executeJikesCompile(cmd.getCommandline(), firstFileName) != 0) { return executeExternalCompile(cmd.getCommandline(), firstFileName) == 0;
throw new BuildException(FAIL_MSG, location);
}
} }
} }



||||||
x
 
000:0
Loading…
Cancel
Save