/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Ant", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
*
* When this task executes, it will recursively scan the sourcedir and
* destdir looking for Java source files to compile. This task makes its
* compile decision based on timestamp.
*
* @author James Davidson duncan@x180.com
* @author Robin Green greenrd@hotmail.com
* @author Stefan Bodewig
* @author J D Glanville
*/
public class Javac extends MatchingTask {
private final static String FAIL_MSG
= "Compile failed, messages should have been provided.";
private Path src;
private File destDir;
private Path compileClasspath;
private String encoding;
private boolean debug = false;
private boolean optimize = false;
private boolean deprecation = false;
private boolean depend = false;
private boolean verbose = false;
private String target;
private Path bootclasspath;
private Path extdirs;
private boolean includeAntRuntime = true;
private boolean includeJavaRuntime = false;
private String fork = "false";
private String forkedExecutable = null;
private boolean nowarn = false;
private String memoryInitialSize;
private String memoryMaximumSize;
private Vector implementationSpecificArgs = new Vector();
protected boolean failOnError = true;
protected File[] compileList = new File[0];
private String source;
private String debugLevel;
/**
* Get the value of debugLevel.
* @return value of debugLevel.
*/
public String getDebugLevel() {
return debugLevel;
}
/**
* Set the value of debugLevel.
* @param v Value to assign to debugLevel.
*/
public void setDebugLevel(String v) {
this.debugLevel = v;
}
/**
* Get the value of source.
* @return value of source.
*/
public String getSource() {
return source;
}
/**
* Set the value of source.
* @param v Value to assign to source.
*/
public void setSource(String v) {
this.source = v;
}
/**
* Create a nested src element for multiple source path
* support.
*
* @return a nested src element.
*/
public Path createSrc() {
if (src == null) {
src = new Path(project);
}
return src.createPath();
}
/**
* Recreate src
*
* @return a nested src element.
*/
protected Path recreateSrc() {
src = null;
return createSrc();
}
/**
* Set the source dirs to find the source Java files.
*/
public void setSrcdir(Path srcDir) {
if (src == null) {
src = srcDir;
} else {
src.append(srcDir);
}
}
/** Gets the source dirs to find the source java files. */
public Path getSrcdir() {
return src;
}
/**
* Set the destination directory into which the Java source
* files should be compiled.
*/
public void setDestdir(File destDir) {
this.destDir = destDir;
}
/**
* Gets the destination directory into which the java source files
* should be compiled.
*/
public File getDestdir() {
return destDir;
}
/**
* Set the classpath to be used for this compilation.
*/
public void setClasspath(Path classpath) {
if (compileClasspath == null) {
compileClasspath = classpath;
} else {
compileClasspath.append(classpath);
}
}
/** Gets the classpath to be used for this compilation. */
public Path getClasspath() {
return compileClasspath;
}
/**
* Maybe creates a nested classpath element.
*/
public Path createClasspath() {
if (compileClasspath == null) {
compileClasspath = new Path(project);
}
return compileClasspath.createPath();
}
/**
* Adds a reference to a CLASSPATH defined elsewhere.
*/
public void setClasspathRef(Reference r) {
createClasspath().setRefid(r);
}
/**
* Sets the bootclasspath that will be used to compile the classes
* against.
*/
public void setBootclasspath(Path bootclasspath) {
if (this.bootclasspath == null) {
this.bootclasspath = bootclasspath;
} else {
this.bootclasspath.append(bootclasspath);
}
}
/**
* Gets the bootclasspath that will be used to compile the classes
* against.
*/
public Path getBootclasspath() {
return bootclasspath;
}
/**
* Maybe creates a nested classpath element.
*/
public Path createBootclasspath() {
if (bootclasspath == null) {
bootclasspath = new Path(project);
}
return bootclasspath.createPath();
}
/**
* Adds a reference to a CLASSPATH defined elsewhere.
*/
public void setBootClasspathRef(Reference r) {
createBootclasspath().setRefid(r);
}
/**
* Sets the extension directories that will be used during the
* compilation.
*/
public void setExtdirs(Path extdirs) {
if (this.extdirs == null) {
this.extdirs = extdirs;
} else {
this.extdirs.append(extdirs);
}
}
/**
* Gets the extension directories that will be used during the
* compilation.
*/
public Path getExtdirs() {
return extdirs;
}
/**
* Maybe creates a nested classpath element.
*/
public Path createExtdirs() {
if (extdirs == null) {
extdirs = new Path(project);
}
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;
}
/**
* Gets the failonerror flag.
*/
public boolean getFailonerror() {
return failOnError;
}
/**
* Set the deprecation flag.
*/
public void setDeprecation(boolean deprecation) {
this.deprecation = deprecation;
}
/** Gets the deprecation flag. */
public boolean getDeprecation() {
return deprecation;
}
/**
* Set the memoryInitialSize flag.
*/
public void setMemoryInitialSize(String memoryInitialSize) {
this.memoryInitialSize = memoryInitialSize;
}
/** Gets the memoryInitialSize flag. */
public String getMemoryInitialSize() {
return memoryInitialSize;
}
/**
* Set the memoryMaximumSize flag.
*/
public void setMemoryMaximumSize(String memoryMaximumSize) {
this.memoryMaximumSize = memoryMaximumSize;
}
/** Gets the memoryMaximumSize flag. */
public String getMemoryMaximumSize() {
return memoryMaximumSize;
}
/**
* Set the Java source file encoding name.
*/
public void setEncoding(String encoding) {
this.encoding = encoding;
}
/** Gets the java source file encoding name. */
public String getEncoding() {
return encoding;
}
/**
* Set the debug flag.
*/
public void setDebug(boolean debug) {
this.debug = debug;
}
/** Gets the debug flag. */
public boolean getDebug() {
return debug;
}
/**
* Set the optimize flag.
*/
public void setOptimize(boolean optimize) {
this.optimize = optimize;
}
/** Gets the optimize flag. */
public boolean getOptimize() {
return optimize;
}
/**
* Set the depend flag.
*/
public void setDepend(boolean depend) {
this.depend = depend;
}
/** Gets the depend flag. */
public boolean getDepend() {
return depend;
}
/**
* Set the verbose flag.
*/
public void setVerbose(boolean verbose) {
this.verbose = verbose;
}
/** Gets the verbose flag. */
public boolean getVerbose() {
return verbose;
}
/**
* Sets the target VM that the classes will be compiled for. Valid
* strings are "1.1", "1.2", and "1.3".
*/
public void setTarget(String target) {
this.target = target;
}
/** Gets the target VM that the classes will be compiled for. */
public String getTarget() {
return target;
}
/**
* Include ant's own classpath in this task's classpath?
*/
public void setIncludeantruntime( boolean include ) {
includeAntRuntime = include;
}
/**
* Gets whether or not the ant classpath is to be included in the
* task's classpath.
*/
public boolean getIncludeantruntime() {
return includeAntRuntime;
}
/**
* Sets whether or not to include the java runtime libraries to this
* task's classpath.
*/
public void setIncludejavaruntime( boolean include ) {
includeJavaRuntime = include;
}
/**
* Gets whether or not the java runtime should be included in this
* task's classpath.
*/
public boolean getIncludejavaruntime() {
return includeJavaRuntime;
}
/**
* Sets whether to fork the javac compiler.
*
* @param f "true|false|on|off|yes|no" or the name of the javac
* executable.
*/
public void setFork(String f) {
if (f.equalsIgnoreCase("on")
|| f.equalsIgnoreCase("true")
|| f.equalsIgnoreCase("yes")) {
fork = "true";
forkedExecutable = getSystemJavac();
} else if (f.equalsIgnoreCase("off")
|| f.equalsIgnoreCase("false")
|| f.equalsIgnoreCase("no")) {
fork = "false";
forkedExecutable = null;
} else {
fork = "true";
forkedExecutable = f;
}
}
/**
* Is this a forked invocation of JDK's javac?
*/
public boolean isForkedJavac() {
return !"false".equals(fork) ||
"extJavac".equals(project.getProperty("build.compiler"));
}
/**
* The name of the javac executable to use in fork-mode.
*/
public String getJavacExecutable() {
if (forkedExecutable == null && isForkedJavac()) {
forkedExecutable = getSystemJavac();
} else if (forkedExecutable != null && !isForkedJavac()) {
forkedExecutable = null;
}
return forkedExecutable;
}
/**
* Sets whether the -nowarn option should be used.
*/
public void setNowarn(boolean flag) {
this.nowarn = flag;
}
/**
* Should the -nowarn option be used.
*/
public boolean getNowarn() {
return nowarn;
}
/**
* Adds an implementation specific command line argument.
*/
public ImplementationSpecificArgument createCompilerArg() {
ImplementationSpecificArgument arg =
new ImplementationSpecificArgument();
implementationSpecificArgs.addElement(arg);
return arg;
}
/**
* Get the additional implementation specific command line arguments.
* @return array of command line arguments, guaranteed to be non-null.
*/
public String[] getCurrentCompilerArgs() {
Vector args = new Vector();
for (Enumeration enum = implementationSpecificArgs.elements();
enum.hasMoreElements();
) {
String[] curr =
((ImplementationSpecificArgument) enum.nextElement()).getParts();
for (int i=0; i