FileDocCategorySizeDatePackage
Javac.javaAPI DocApache Ant 1.7029905Wed Dec 13 06:16:20 GMT 2006org.apache.tools.ant.taskdefs

Javac

public class Javac extends MatchingTask
Compiles Java source files. This task can take the following arguments:
  • sourcedir
  • destdir
  • deprecation
  • classpath
  • bootclasspath
  • extdirs
  • optimize
  • debug
  • encoding
  • target
  • depend
  • verbose
  • failonerror
  • includeantruntime
  • includejavaruntime
  • source
  • compiler
Of these arguments, the sourcedir and destdir are required.

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.

since
Ant 1.1
ant.task
category="java"

Fields Summary
private static final String
FAIL_MSG
private static final String
JAVAC16
private static final String
JAVAC15
private static final String
JAVAC14
private static final String
JAVAC13
private static final String
JAVAC12
private static final String
JAVAC11
private static final String
MODERN
private static final String
CLASSIC
private static final String
EXTJAVAC
private org.apache.tools.ant.types.Path
src
private File
destDir
private org.apache.tools.ant.types.Path
compileClasspath
private org.apache.tools.ant.types.Path
compileSourcepath
private String
encoding
private boolean
debug
private boolean
optimize
private boolean
deprecation
private boolean
depend
private boolean
verbose
private String
targetAttribute
private org.apache.tools.ant.types.Path
bootclasspath
private org.apache.tools.ant.types.Path
extdirs
private boolean
includeAntRuntime
private boolean
includeJavaRuntime
private boolean
fork
private String
forkedExecutable
private boolean
nowarn
private String
memoryInitialSize
private String
memoryMaximumSize
private org.apache.tools.ant.util.facade.FacadeTaskHelper
facade
protected boolean
failOnError
protected boolean
listFiles
protected File[]
compileList
private String
source
private String
debugLevel
private File
tmpDir
Constructors Summary
public Javac()
Javac task for compilation of Java files.


                
      
        facade = new FacadeTaskHelper(assumedJavaVersion());
    
Methods Summary
private java.lang.StringassumedJavaVersion()

        if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_2)) {
            return JAVAC12;
        } else if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_3)) {
            return JAVAC13;
        } else if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_4)) {
            return JAVAC14;
        } else if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_5)) {
            return JAVAC15;
        } else if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_6)) {
            return JAVAC16;
        } else {
            return CLASSIC;
        }
    
protected voidcheckParameters()
Check that all required attributes have been set and nothing silly has been entered.

since
Ant 1.5
exception
BuildException if an error occurs

        if (src == null) {
            throw new BuildException("srcdir attribute must be set!",
                                     getLocation());
        }
        if (src.size() == 0) {
            throw new BuildException("srcdir attribute must be set!",
                                     getLocation());
        }

        if (destDir != null && !destDir.isDirectory()) {
            throw new BuildException("destination directory \""
                                     + destDir
                                     + "\" does not exist "
                                     + "or is not a directory", getLocation());
        }
    
protected voidcompile()
Perform the compilation.

since
Ant 1.5

        String compilerImpl = getCompiler();

        if (compileList.length > 0) {
            log("Compiling " + compileList.length + " source file"
                + (compileList.length == 1 ? "" : "s")
                + (destDir != null ? " to " + destDir : ""));

            if (listFiles) {
                for (int i = 0; i < compileList.length; i++) {
                  String filename = compileList[i].getAbsolutePath();
                  log(filename);
                }
            }

            CompilerAdapter adapter =
                CompilerAdapterFactory.getCompiler(compilerImpl, this);

            // now we need to populate the compiler adapter
            adapter.setJavac(this);

            // finally, lets execute the compiler!!
            if (!adapter.execute()) {
                if (failOnError) {
                    throw new BuildException(FAIL_MSG, getLocation());
                } else {
                    log(FAIL_MSG, Project.MSG_ERR);
                }
            }
        }
    
public org.apache.tools.ant.types.PathcreateBootclasspath()
Adds a path to the bootclasspath.

return
a path to be configured

        if (bootclasspath == null) {
            bootclasspath = new Path(getProject());
        }
        return bootclasspath.createPath();
    
public org.apache.tools.ant.types.PathcreateClasspath()
Adds a path to the classpath.

return
a class path to be configured

        if (compileClasspath == null) {
            compileClasspath = new Path(getProject());
        }
        return compileClasspath.createPath();
    
public org.apache.tools.ant.taskdefs.Javac$ImplementationSpecificArgumentcreateCompilerArg()
Adds an implementation specific command-line argument.

return
a ImplementationSpecificArgument to be configured

        ImplementationSpecificArgument arg =
            new ImplementationSpecificArgument();
        facade.addImplementationArgument(arg);
        return arg;
    
public org.apache.tools.ant.types.PathcreateExtdirs()
Adds a path to extdirs.

return
a path to be configured

        if (extdirs == null) {
            extdirs = new Path(getProject());
        }
        return extdirs.createPath();
    
public org.apache.tools.ant.types.PathcreateSourcepath()
Adds a path to sourcepath.

return
a sourcepath to be configured

        if (compileSourcepath == null) {
            compileSourcepath = new Path(getProject());
        }
        return compileSourcepath.createPath();
    
public org.apache.tools.ant.types.PathcreateSrc()
Adds a path for source compilation.

return
a nested src element.

        if (src == null) {
            src = new Path(getProject());
        }
        return src.createPath();
    
public voidexecute()
Executes the task.

exception
BuildException if an error occurs

        checkParameters();
        resetFileLists();

        // scan source directories and dest directory to build up
        // compile lists
        String[] list = src.list();
        for (int i = 0; i < list.length; i++) {
            File srcDir = getProject().resolveFile(list[i]);
            if (!srcDir.exists()) {
                throw new BuildException("srcdir \""
                                         + srcDir.getPath()
                                         + "\" does not exist!", getLocation());
            }

            DirectoryScanner ds = this.getDirectoryScanner(srcDir);
            String[] files = ds.getIncludedFiles();

            scanDir(srcDir, destDir != null ? destDir : srcDir, files);
        }

        compile();
    
private java.lang.StringgetAltCompilerName(java.lang.String anImplementation)

        if (JAVAC16.equalsIgnoreCase(anImplementation)
                || JAVAC15.equalsIgnoreCase(anImplementation)
                || JAVAC14.equalsIgnoreCase(anImplementation)
                || JAVAC13.equalsIgnoreCase(anImplementation)) {
            return MODERN;
        }
        if (JAVAC12.equalsIgnoreCase(anImplementation)
                || JAVAC11.equalsIgnoreCase(anImplementation)) {
            return CLASSIC;
        }
        if (MODERN.equalsIgnoreCase(anImplementation)) {
            String nextSelected = assumedJavaVersion();
            if (JAVAC16.equalsIgnoreCase(nextSelected)
                    || JAVAC15.equalsIgnoreCase(nextSelected)
                    || JAVAC14.equalsIgnoreCase(nextSelected)
                    || JAVAC13.equalsIgnoreCase(nextSelected)) {
                return nextSelected;
            }
        }
        if (CLASSIC.equals(anImplementation)) {
            return assumedJavaVersion();
        }
        if (EXTJAVAC.equalsIgnoreCase(anImplementation)) {
            return assumedJavaVersion();
        }
        return null;
    
public org.apache.tools.ant.types.PathgetBootclasspath()
Gets the bootclasspath that will be used to compile the classes against.

return
the boot path

        return bootclasspath;
    
public org.apache.tools.ant.types.PathgetClasspath()
Gets the classpath to be used for this compilation.

return
the class path

        return compileClasspath;
    
public java.lang.StringgetCompiler()
The implementation for this particular task.

Defaults to the build.compiler property but can be overridden via the compiler and fork attributes.

If fork has been set to true, the result will be extJavac and not classic or java1.2 - no matter what the compiler attribute looks like.

see
#getCompilerVersion
return
the compiler.
since
Ant 1.5

        String compilerImpl = getCompilerVersion();
        if (fork) {
            if (isJdkCompiler(compilerImpl)) {
                compilerImpl = "extJavac";
            } else {
                log("Since compiler setting isn't classic or modern,"
                    + "ignoring fork setting.", Project.MSG_WARN);
            }
        }
        return compilerImpl;
    
public java.lang.StringgetCompilerVersion()
The implementation for this particular task.

Defaults to the build.compiler property but can be overridden via the compiler attribute.

This method does not take the fork attribute into account.

see
#getCompiler
return
the compiler.
since
Ant 1.5

        facade.setMagicValue(getProject().getProperty("build.compiler"));
        return facade.getImplementation();
    
public java.lang.String[]getCurrentCompilerArgs()
Get the additional implementation specific command line arguments.

return
array of command line arguments, guaranteed to be non-null.

        String chosen = facade.getExplicitChoice();
        try {
            // make sure facade knows about magic properties and fork setting
            String appliedCompiler = getCompiler();
            facade.setImplementation(appliedCompiler);

            String[] result = facade.getArgs();

            String altCompilerName = getAltCompilerName(facade.getImplementation());

            if (result.length == 0 && altCompilerName != null) {
                facade.setImplementation(altCompilerName);
                result = facade.getArgs();
            }

            return result;

        } finally {
            facade.setImplementation(chosen);
        }
    
public booleangetDebug()
Gets the debug flag.

return
the debug flag

        return debug;
    
public java.lang.StringgetDebugLevel()
Get the value of debugLevel.

return
value of debugLevel.

        return debugLevel;
    
public booleangetDepend()
Gets the depend flag.

return
the depend flag

        return depend;
    
public booleangetDeprecation()
Gets the deprecation flag.

return
the deprecation flag

        return deprecation;
    
public java.io.FilegetDestdir()
Gets the destination directory into which the java source files should be compiled.

return
the destination directory

        return destDir;
    
public java.lang.StringgetEncoding()
Gets the java source file encoding name.

return
the source file encoding name

        return encoding;
    
public java.lang.StringgetExecutable()
The value of the executable attribute, if any.

since
Ant 1.6
return
the name of the java executable

        return forkedExecutable;
    
public org.apache.tools.ant.types.PathgetExtdirs()
Gets the extension directories that will be used during the compilation.

return
the extension directories as a path

        return extdirs;
    
public booleangetFailonerror()
Gets the failonerror flag.

return
the failonerror flag

        return failOnError;
    
public java.io.File[]getFileList()
Gets the list of files to be compiled.

return
the list of files as an array

        return compileList;
    
public booleangetIncludeantruntime()
Gets whether or not the ant classpath is to be included in the classpath.

return
whether or not the ant classpath is to be included in the classpath

        return includeAntRuntime;
    
public booleangetIncludejavaruntime()
Gets whether or not the java runtime should be included in this task's classpath.

return
the includejavaruntime attribute

        return includeJavaRuntime;
    
public java.lang.StringgetJavacExecutable()
The name of the javac executable to use in fork-mode.

This is either the name specified with the executable attribute or the full path of the javac compiler of the VM Ant is currently running in - guessed by Ant.

You should not invoke this method if you want to get the value of the executable command - use {@link #getExecutable getExecutable} for this.

return
the name of the javac executable

        if (forkedExecutable == null && isForkedJavac()) {
            forkedExecutable = getSystemJavac();
        } else if (forkedExecutable != null && !isForkedJavac()) {
            forkedExecutable = null;
        }
        return forkedExecutable;
    
public booleangetListfiles()
Get the listfiles flag.

return
the listfiles flag

        return listFiles;
    
public java.lang.StringgetMemoryInitialSize()
Gets the memoryInitialSize flag.

return
the memoryInitialSize flag

        return memoryInitialSize;
    
public java.lang.StringgetMemoryMaximumSize()
Gets the memoryMaximumSize flag.

return
the memoryMaximumSize flag

        return memoryMaximumSize;
    
public booleangetNowarn()
Should the -nowarn option be used.

return
true if the -nowarn option should be used

        return nowarn;
    
public booleangetOptimize()
Gets the optimize flag.

return
the optimize flag

        return optimize;
    
public java.lang.StringgetSource()
Get the value of source.

return
value of source.

        return source != null
            ? source : getProject().getProperty(MagicNames.BUILD_JAVAC_SOURCE);
    
public org.apache.tools.ant.types.PathgetSourcepath()
Gets the sourcepath to be used for this compilation.

return
the source path

        return compileSourcepath;
    
public org.apache.tools.ant.types.PathgetSrcdir()
Gets the source dirs to find the source java files.

return
the source directories as a path

        return src;
    
protected java.lang.StringgetSystemJavac()

return
the executable name of the java compiler

        return JavaEnvUtils.getJdkExecutable("javac");
    
public java.lang.StringgetTarget()
Gets the target VM that the classes will be compiled for.

return
the target VM

        return targetAttribute != null
            ? targetAttribute
            : getProject().getProperty(MagicNames.BUILD_JAVAC_TARGET);
    
public java.io.FilegetTempdir()
Where Ant should place temporary files.

since
Ant 1.6
return
the temporary directory

        return tmpDir;
    
public booleangetVerbose()
Gets the verbose flag.

return
the verbose flag

        return verbose;
    
public booleanisForkedJavac()
Is this a forked invocation of JDK's javac?

return
true if this is a forked invocation

        return fork || "extJavac".equals(getCompiler());
    
protected booleanisJdkCompiler(java.lang.String compilerImpl)
Is the compiler implementation a jdk compiler

param
compilerImpl the name of the compiler implementation
return
true if compilerImpl is "modern", "classic", "javac1.1", "javac1.2", "javac1.3", "javac1.4", "javac1.5" or "javac1.6".

        return MODERN.equals(compilerImpl)
            || CLASSIC.equals(compilerImpl)
            || JAVAC16.equals(compilerImpl)
            || JAVAC15.equals(compilerImpl)
            || JAVAC14.equals(compilerImpl)
            || JAVAC13.equals(compilerImpl)
            || JAVAC12.equals(compilerImpl)
            || JAVAC11.equals(compilerImpl);
    
protected org.apache.tools.ant.types.PathrecreateSrc()
Recreate src.

return
a nested src element.

        src = null;
        return createSrc();
    
protected voidresetFileLists()
Clear the list of files to be compiled and copied..

        compileList = new File[0];
    
protected voidscanDir(java.io.File srcDir, java.io.File destDir, java.lang.String[] files)
Scans the directory looking for source files to be compiled. The results are returned in the class variable compileList

param
srcDir The source directory
param
destDir The destination directory
param
files An array of filenames

        GlobPatternMapper m = new GlobPatternMapper();
        m.setFrom("*.java");
        m.setTo("*.class");
        SourceFileScanner sfs = new SourceFileScanner(this);
        File[] newFiles = sfs.restrictAsFiles(files, srcDir, destDir, m);

        if (newFiles.length > 0) {
            File[] newCompileList
                = new File[compileList.length + newFiles.length];
            System.arraycopy(compileList, 0, newCompileList, 0,
                    compileList.length);
            System.arraycopy(newFiles, 0, newCompileList,
                    compileList.length, newFiles.length);
            compileList = newCompileList;
        }
    
public voidsetBootClasspathRef(org.apache.tools.ant.types.Reference r)
Adds a reference to a classpath defined elsewhere.

param
r a reference to a classpath

        createBootclasspath().setRefid(r);
    
public voidsetBootclasspath(org.apache.tools.ant.types.Path bootclasspath)
Sets the bootclasspath that will be used to compile the classes against.

param
bootclasspath a path to use as a boot class path (may be more than one)

        if (this.bootclasspath == null) {
            this.bootclasspath = bootclasspath;
        } else {
            this.bootclasspath.append(bootclasspath);
        }
    
public voidsetClasspath(org.apache.tools.ant.types.Path classpath)
Set the classpath to be used for this compilation.

param
classpath an Ant Path object containing the compilation classpath.

        if (compileClasspath == null) {
            compileClasspath = classpath;
        } else {
            compileClasspath.append(classpath);
        }
    
public voidsetClasspathRef(org.apache.tools.ant.types.Reference r)
Adds a reference to a classpath defined elsewhere.

param
r a reference to a classpath

        createClasspath().setRefid(r);
    
public voidsetCompiler(java.lang.String compiler)
Choose the implementation for this particular task.

param
compiler the name of the compiler
since
Ant 1.5

        facade.setImplementation(compiler);
    
public voidsetDebug(boolean debug)
Indicates whether source should be compiled with debug information; defaults to off.

param
debug if true compile with debug information

        this.debug = debug;
    
public voidsetDebugLevel(java.lang.String v)
Keyword list to be appended to the -g command-line switch. This will be ignored by all implementations except modern and classic(ver >= 1.2). Legal values are none or a comma-separated list of the following keywords: lines, vars, and source. If debuglevel is not specified, by default, :none will be appended to -g. If debug is not turned on, this attribute will be ignored.

param
v Value to assign to debugLevel.

        this.debugLevel = v;
    
public voidsetDepend(boolean depend)
Enables dependency-tracking for compilers that support this (jikes and classic).

param
depend if true enable dependency-tracking

        this.depend = depend;
    
public voidsetDeprecation(boolean deprecation)
Indicates whether source should be compiled with deprecation information; defaults to off.

param
deprecation if true turn on deprecation information

        this.deprecation = deprecation;
    
public voidsetDestdir(java.io.File destDir)
Set the destination directory into which the Java source files should be compiled.

param
destDir the destination director

        this.destDir = destDir;
    
public voidsetEncoding(java.lang.String encoding)
Set the Java source file encoding name.

param
encoding the source file encoding

        this.encoding = encoding;
    
public voidsetExecutable(java.lang.String forkExec)
Sets the name of the javac executable.

Ignored unless fork is true or extJavac has been specified as the compiler.

param
forkExec the name of the executable

        forkedExecutable = forkExec;
    
public voidsetExtdirs(org.apache.tools.ant.types.Path extdirs)
Sets the extension directories that will be used during the compilation.

param
extdirs a path

        if (this.extdirs == null) {
            this.extdirs = extdirs;
        } else {
            this.extdirs.append(extdirs);
        }
    
public voidsetFailonerror(boolean fail)
Indicates whether the build will continue even if there are compilation errors; defaults to true.

param
fail if true halt the build on failure

        failOnError = fail;
    
public voidsetFork(boolean f)
If true, forks the javac compiler.

param
f "true|false|on|off|yes|no"

        fork = f;
    
public voidsetIncludeantruntime(boolean include)
If true, includes Ant's own classpath in the classpath.

param
include if true, includes Ant's own classpath in the classpath

        includeAntRuntime = include;
    
public voidsetIncludejavaruntime(boolean include)
If true, includes the Java runtime libraries in the classpath.

param
include if true, includes the Java runtime libraries in the classpath

        includeJavaRuntime = include;
    
public voidsetListfiles(boolean list)
If true, list the source files being handed off to the compiler.

param
list if true list the source files

        listFiles = list;
    
public voidsetMemoryInitialSize(java.lang.String memoryInitialSize)
The initial size of the memory for the underlying VM if javac is run externally; ignored otherwise. Defaults to the standard VM memory setting. (Examples: 83886080, 81920k, or 80m)

param
memoryInitialSize string to pass to VM

        this.memoryInitialSize = memoryInitialSize;
    
public voidsetMemoryMaximumSize(java.lang.String memoryMaximumSize)
The maximum size of the memory for the underlying VM if javac is run externally; ignored otherwise. Defaults to the standard VM memory setting. (Examples: 83886080, 81920k, or 80m)

param
memoryMaximumSize string to pass to VM

        this.memoryMaximumSize = memoryMaximumSize;
    
public voidsetNowarn(boolean flag)
If true, enables the -nowarn option.

param
flag if true, enable the -nowarn option

        this.nowarn = flag;
    
public voidsetOptimize(boolean optimize)
If true, compiles with optimization enabled.

param
optimize if true compile with optimization enabled

        this.optimize = optimize;
    
public voidsetProceed(boolean proceed)

ant.attribute
ignore="true"
param
proceed inverse of failoferror

        failOnError = !proceed;
    
public voidsetSource(java.lang.String v)
Value of the -source command-line switch; will be ignored by all implementations except modern and jikes. If you use this attribute together with jikes, you must make sure that your version of jikes supports the -source switch. Legal values are 1.3, 1.4, 1.5, and 5 - by default, no -source argument will be used at all.

param
v Value to assign to source.

        this.source = v;
    
public voidsetSourcepath(org.apache.tools.ant.types.Path sourcepath)
Set the sourcepath to be used for this compilation.

param
sourcepath the source path

        if (compileSourcepath == null) {
            compileSourcepath = sourcepath;
        } else {
            compileSourcepath.append(sourcepath);
        }
    
public voidsetSourcepathRef(org.apache.tools.ant.types.Reference r)
Adds a reference to a source path defined elsewhere.

param
r a reference to a source path

        createSourcepath().setRefid(r);
    
public voidsetSrcdir(org.apache.tools.ant.types.Path srcDir)
Set the source directories to find the source Java files.

param
srcDir the source directories as a path

        if (src == null) {
            src = srcDir;
        } else {
            src.append(srcDir);
        }
    
public voidsetTarget(java.lang.String target)
Sets the target VM that the classes will be compiled for. Valid values depend on the compiler, for jdk 1.4 the valid values are "1.1", "1.2", "1.3", "1.4", "1.5", "1.6", "5" and "6".

param
target the target VM

        this.targetAttribute = target;
    
public voidsetTempdir(java.io.File tmpDir)
Where Ant should place temporary files.

since
Ant 1.6
param
tmpDir the temporary directory

        this.tmpDir = tmpDir;
    
public voidsetVerbose(boolean verbose)
If true, asks the compiler for verbose output.

param
verbose if true, asks the compiler for verbose output

        this.verbose = verbose;