org.apache.tools.ant.taskdefs

Class Javac

public class Javac extends MatchingTask

Compiles Java source files. This task can take the following arguments: 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

UNKNOWN: category="java"

Nested Class Summary
classJavac.ImplementationSpecificArgument
Adds an "compiler" attribute to Commandline$Attribute used to filter command line attributes based on the current implementation.
Field Summary
protected File[]compileList
protected booleanfailOnError
protected booleanlistFiles
Constructor Summary
Javac()
Javac task for compilation of Java files.
Method Summary
protected voidcheckParameters()
Check that all required attributes have been set and nothing silly has been entered.
protected voidcompile()
Perform the compilation.
PathcreateBootclasspath()
Adds a path to the bootclasspath.
PathcreateClasspath()
Adds a path to the classpath.
Javac.ImplementationSpecificArgumentcreateCompilerArg()
Adds an implementation specific command-line argument.
PathcreateExtdirs()
Adds a path to extdirs.
PathcreateSourcepath()
Adds a path to sourcepath.
PathcreateSrc()
Adds a path for source compilation.
voidexecute()
Executes the task.
PathgetBootclasspath()
Gets the bootclasspath that will be used to compile the classes against.
PathgetClasspath()
Gets the classpath to be used for this compilation.
StringgetCompiler()
The implementation for this particular task.
StringgetCompilerVersion()
The implementation for this particular task.
String[]getCurrentCompilerArgs()
Get the additional implementation specific command line arguments.
booleangetDebug()
Gets the debug flag.
StringgetDebugLevel()
Get the value of debugLevel.
booleangetDepend()
Gets the depend flag.
booleangetDeprecation()
Gets the deprecation flag.
FilegetDestdir()
Gets the destination directory into which the java source files should be compiled.
StringgetEncoding()
Gets the java source file encoding name.
StringgetExecutable()
The value of the executable attribute, if any.
PathgetExtdirs()
Gets the extension directories that will be used during the compilation.
booleangetFailonerror()
Gets the failonerror flag.
File[]getFileList()
Gets the list of files to be compiled.
booleangetIncludeantruntime()
Gets whether or not the ant classpath is to be included in the classpath.
booleangetIncludejavaruntime()
Gets whether or not the java runtime should be included in this task's classpath.
StringgetJavacExecutable()
The name of the javac executable to use in fork-mode.
booleangetListfiles()
Get the listfiles flag.
StringgetMemoryInitialSize()
Gets the memoryInitialSize flag.
StringgetMemoryMaximumSize()
Gets the memoryMaximumSize flag.
booleangetNowarn()
Should the -nowarn option be used.
booleangetOptimize()
Gets the optimize flag.
StringgetSource()
Get the value of source.
PathgetSourcepath()
Gets the sourcepath to be used for this compilation.
PathgetSrcdir()
Gets the source dirs to find the source java files.
protected StringgetSystemJavac()
StringgetTarget()
Gets the target VM that the classes will be compiled for.
booleangetTaskSuccess()
Get the result of the javac task (success or failure).
FilegetTempdir()
Where Ant should place temporary files.
booleangetVerbose()
Gets the verbose flag.
booleanisForkedJavac()
Is this a forked invocation of JDK's javac?
booleanisIncludeDestClasses()
Get the value of the includeDestClasses property.
protected booleanisJdkCompiler(String compilerImpl)
Is the compiler implementation a jdk compiler
protected PathrecreateSrc()
Recreate src.
protected voidresetFileLists()
Clear the list of files to be compiled and copied..
protected voidscanDir(File srcDir, File destDir, String[] files)
Scans the directory looking for source files to be compiled.
voidsetBootclasspath(Path bootclasspath)
Sets the bootclasspath that will be used to compile the classes against.
voidsetBootClasspathRef(Reference r)
Adds a reference to a classpath defined elsewhere.
voidsetClasspath(Path classpath)
Set the classpath to be used for this compilation.
voidsetClasspathRef(Reference r)
Adds a reference to a classpath defined elsewhere.
voidsetCompiler(String compiler)
Choose the implementation for this particular task.
voidsetDebug(boolean debug)
Indicates whether source should be compiled with debug information; defaults to off.
voidsetDebugLevel(String v)
Keyword list to be appended to the -g command-line switch.
voidsetDepend(boolean depend)
Enables dependency-tracking for compilers that support this (jikes and classic).
voidsetDeprecation(boolean deprecation)
Indicates whether source should be compiled with deprecation information; defaults to off.
voidsetDestdir(File destDir)
Set the destination directory into which the Java source files should be compiled.
voidsetEncoding(String encoding)
Set the Java source file encoding name.
voidsetErrorProperty(String errorProperty)
The property to set on compliation failure.
voidsetExecutable(String forkExec)
Sets the name of the javac executable.
voidsetExtdirs(Path extdirs)
Sets the extension directories that will be used during the compilation.
voidsetFailonerror(boolean fail)
Indicates whether the build will continue even if there are compilation errors; defaults to true.
voidsetFork(boolean f)
If true, forks the javac compiler.
voidsetIncludeantruntime(boolean include)
If true, includes Ant's own classpath in the classpath.
voidsetIncludeDestClasses(boolean includeDestClasses)
This property controls whether to include the destination classes directory in the classpath given to the compiler.
voidsetIncludejavaruntime(boolean include)
If true, includes the Java runtime libraries in the classpath.
voidsetListfiles(boolean list)
If true, list the source files being handed off to the compiler.
voidsetMemoryInitialSize(String memoryInitialSize)
The initial size of the memory for the underlying VM if javac is run externally; ignored otherwise.
voidsetMemoryMaximumSize(String memoryMaximumSize)
The maximum size of the memory for the underlying VM if javac is run externally; ignored otherwise.
voidsetNowarn(boolean flag)
If true, enables the -nowarn option.
voidsetOptimize(boolean optimize)
If true, compiles with optimization enabled.
voidsetProceed(boolean proceed)
voidsetSource(String v)
Value of the -source command-line switch; will be ignored by all implementations except modern and jikes.
voidsetSourcepath(Path sourcepath)
Set the sourcepath to be used for this compilation.
voidsetSourcepathRef(Reference r)
Adds a reference to a source path defined elsewhere.
voidsetSrcdir(Path srcDir)
Set the source directories to find the source Java files.
voidsetTarget(String target)
Sets the target VM that the classes will be compiled for.
voidsetTempdir(File tmpDir)
Where Ant should place temporary files.
voidsetUpdatedProperty(String updatedProperty)
The property to set on compliation success.
voidsetVerbose(boolean verbose)
If true, asks the compiler for verbose output.

Field Detail

compileList

protected File[] compileList

failOnError

protected boolean failOnError

listFiles

protected boolean listFiles

Constructor Detail

Javac

public Javac()
Javac task for compilation of Java files.

Method Detail

checkParameters

protected void checkParameters()
Check that all required attributes have been set and nothing silly has been entered.

Throws: BuildException if an error occurs

Since: Ant 1.5

compile

protected void compile()
Perform the compilation.

Since: Ant 1.5

createBootclasspath

public Path createBootclasspath()
Adds a path to the bootclasspath.

Returns: a path to be configured

createClasspath

public Path createClasspath()
Adds a path to the classpath.

Returns: a class path to be configured

createCompilerArg

public Javac.ImplementationSpecificArgument createCompilerArg()
Adds an implementation specific command-line argument.

Returns: a ImplementationSpecificArgument to be configured

createExtdirs

public Path createExtdirs()
Adds a path to extdirs.

Returns: a path to be configured

createSourcepath

public Path createSourcepath()
Adds a path to sourcepath.

Returns: a sourcepath to be configured

createSrc

public Path createSrc()
Adds a path for source compilation.

Returns: a nested src element.

execute

public void execute()
Executes the task.

Throws: BuildException if an error occurs

getBootclasspath

public Path getBootclasspath()
Gets the bootclasspath that will be used to compile the classes against.

Returns: the boot path

getClasspath

public Path getClasspath()
Gets the classpath to be used for this compilation.

Returns: the class path

getCompiler

public String getCompiler()
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.

Returns: the compiler.

Since: Ant 1.5

See Also: Javac

getCompilerVersion

public String getCompilerVersion()
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.

Returns: the compiler.

Since: Ant 1.5

See Also: Javac

getCurrentCompilerArgs

public String[] getCurrentCompilerArgs()
Get the additional implementation specific command line arguments.

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

getDebug

public boolean getDebug()
Gets the debug flag.

Returns: the debug flag

getDebugLevel

public String getDebugLevel()
Get the value of debugLevel.

Returns: value of debugLevel.

getDepend

public boolean getDepend()
Gets the depend flag.

Returns: the depend flag

getDeprecation

public boolean getDeprecation()
Gets the deprecation flag.

Returns: the deprecation flag

getDestdir

public File getDestdir()
Gets the destination directory into which the java source files should be compiled.

Returns: the destination directory

getEncoding

public String getEncoding()
Gets the java source file encoding name.

Returns: the source file encoding name

getExecutable

public String getExecutable()
The value of the executable attribute, if any.

Returns: the name of the java executable

Since: Ant 1.6

getExtdirs

public Path getExtdirs()
Gets the extension directories that will be used during the compilation.

Returns: the extension directories as a path

getFailonerror

public boolean getFailonerror()
Gets the failonerror flag.

Returns: the failonerror flag

getFileList

public File[] getFileList()
Gets the list of files to be compiled.

Returns: the list of files as an array

getIncludeantruntime

public boolean getIncludeantruntime()
Gets whether or not the ant classpath is to be included in the classpath.

Returns: whether or not the ant classpath is to be included in the classpath

getIncludejavaruntime

public boolean getIncludejavaruntime()
Gets whether or not the java runtime should be included in this task's classpath.

Returns: the includejavaruntime attribute

getJavacExecutable

public String getJavacExecutable()
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 getExecutable for this.

Returns: the name of the javac executable

getListfiles

public boolean getListfiles()
Get the listfiles flag.

Returns: the listfiles flag

getMemoryInitialSize

public String getMemoryInitialSize()
Gets the memoryInitialSize flag.

Returns: the memoryInitialSize flag

getMemoryMaximumSize

public String getMemoryMaximumSize()
Gets the memoryMaximumSize flag.

Returns: the memoryMaximumSize flag

getNowarn

public boolean getNowarn()
Should the -nowarn option be used.

Returns: true if the -nowarn option should be used

getOptimize

public boolean getOptimize()
Gets the optimize flag.

Returns: the optimize flag

getSource

public String getSource()
Get the value of source.

Returns: value of source.

getSourcepath

public Path getSourcepath()
Gets the sourcepath to be used for this compilation.

Returns: the source path

getSrcdir

public Path getSrcdir()
Gets the source dirs to find the source java files.

Returns: the source directories as a path

getSystemJavac

protected String getSystemJavac()

Returns: the executable name of the java compiler

getTarget

public String getTarget()
Gets the target VM that the classes will be compiled for.

Returns: the target VM

getTaskSuccess

public boolean getTaskSuccess()
Get the result of the javac task (success or failure).

Returns: true if compilation succeeded, or was not neccessary, false if the compilation failed.

getTempdir

public File getTempdir()
Where Ant should place temporary files.

Returns: the temporary directory

Since: Ant 1.6

getVerbose

public boolean getVerbose()
Gets the verbose flag.

Returns: the verbose flag

isForkedJavac

public boolean isForkedJavac()
Is this a forked invocation of JDK's javac?

Returns: true if this is a forked invocation

isIncludeDestClasses

public boolean isIncludeDestClasses()
Get the value of the includeDestClasses property.

Returns: the value.

isJdkCompiler

protected boolean isJdkCompiler(String compilerImpl)
Is the compiler implementation a jdk compiler

Parameters: compilerImpl the name of the compiler implementation

Returns: true if compilerImpl is "modern", "classic", "javac1.1", "javac1.2", "javac1.3", "javac1.4", "javac1.5" or "javac1.6".

recreateSrc

protected Path recreateSrc()
Recreate src.

Returns: a nested src element.

resetFileLists

protected void resetFileLists()
Clear the list of files to be compiled and copied..

scanDir

protected void scanDir(File srcDir, File destDir, String[] files)
Scans the directory looking for source files to be compiled. The results are returned in the class variable compileList

Parameters: srcDir The source directory destDir The destination directory files An array of filenames

setBootclasspath

public void setBootclasspath(Path bootclasspath)
Sets the bootclasspath that will be used to compile the classes against.

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

setBootClasspathRef

public void setBootClasspathRef(Reference r)
Adds a reference to a classpath defined elsewhere.

Parameters: r a reference to a classpath

setClasspath

public void setClasspath(Path classpath)
Set the classpath to be used for this compilation.

Parameters: classpath an Ant Path object containing the compilation classpath.

setClasspathRef

public void setClasspathRef(Reference r)
Adds a reference to a classpath defined elsewhere.

Parameters: r a reference to a classpath

setCompiler

public void setCompiler(String compiler)
Choose the implementation for this particular task.

Parameters: compiler the name of the compiler

Since: Ant 1.5

setDebug

public void setDebug(boolean debug)
Indicates whether source should be compiled with debug information; defaults to off.

Parameters: debug if true compile with debug information

setDebugLevel

public void setDebugLevel(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.

Parameters: v Value to assign to debugLevel.

setDepend

public void setDepend(boolean depend)
Enables dependency-tracking for compilers that support this (jikes and classic).

Parameters: depend if true enable dependency-tracking

setDeprecation

public void setDeprecation(boolean deprecation)
Indicates whether source should be compiled with deprecation information; defaults to off.

Parameters: deprecation if true turn on deprecation information

setDestdir

public void setDestdir(File destDir)
Set the destination directory into which the Java source files should be compiled.

Parameters: destDir the destination director

setEncoding

public void setEncoding(String encoding)
Set the Java source file encoding name.

Parameters: encoding the source file encoding

setErrorProperty

public void setErrorProperty(String errorProperty)
The property to set on compliation failure. This property will be set if the compilation fails.

Parameters: errorProperty the property name to use.

Since: Ant 1.7.1.

setExecutable

public void setExecutable(String forkExec)
Sets the name of the javac executable.

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

Parameters: forkExec the name of the executable

setExtdirs

public void setExtdirs(Path extdirs)
Sets the extension directories that will be used during the compilation.

Parameters: extdirs a path

setFailonerror

public void setFailonerror(boolean fail)
Indicates whether the build will continue even if there are compilation errors; defaults to true.

Parameters: fail if true halt the build on failure

setFork

public void setFork(boolean f)
If true, forks the javac compiler.

Parameters: f "true|false|on|off|yes|no"

setIncludeantruntime

public void setIncludeantruntime(boolean include)
If true, includes Ant's own classpath in the classpath.

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

setIncludeDestClasses

public void setIncludeDestClasses(boolean includeDestClasses)
This property controls whether to include the destination classes directory in the classpath given to the compiler. The default value is "true".

Parameters: includeDestClasses the value to use.

setIncludejavaruntime

public void setIncludejavaruntime(boolean include)
If true, includes the Java runtime libraries in the classpath.

Parameters: include if true, includes the Java runtime libraries in the classpath

setListfiles

public void setListfiles(boolean list)
If true, list the source files being handed off to the compiler.

Parameters: list if true list the source files

setMemoryInitialSize

public void setMemoryInitialSize(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)

Parameters: memoryInitialSize string to pass to VM

setMemoryMaximumSize

public void setMemoryMaximumSize(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)

Parameters: memoryMaximumSize string to pass to VM

setNowarn

public void setNowarn(boolean flag)
If true, enables the -nowarn option.

Parameters: flag if true, enable the -nowarn option

setOptimize

public void setOptimize(boolean optimize)
If true, compiles with optimization enabled.

Parameters: optimize if true compile with optimization enabled

setProceed

public void setProceed(boolean proceed)

Parameters: proceed inverse of failoferror

UNKNOWN: ignore="true"

setSource

public void setSource(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.

Parameters: v Value to assign to source.

setSourcepath

public void setSourcepath(Path sourcepath)
Set the sourcepath to be used for this compilation.

Parameters: sourcepath the source path

setSourcepathRef

public void setSourcepathRef(Reference r)
Adds a reference to a source path defined elsewhere.

Parameters: r a reference to a source path

setSrcdir

public void setSrcdir(Path srcDir)
Set the source directories to find the source Java files.

Parameters: srcDir the source directories as a path

setTarget

public void setTarget(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".

Parameters: target the target VM

setTempdir

public void setTempdir(File tmpDir)
Where Ant should place temporary files.

Parameters: tmpDir the temporary directory

Since: Ant 1.6

setUpdatedProperty

public void setUpdatedProperty(String updatedProperty)
The property to set on compliation success. This property will not be set if the compilation fails, or if there are no files to compile.

Parameters: updatedProperty the property name to use.

Since: Ant 1.7.1.

setVerbose

public void setVerbose(boolean verbose)
If true, asks the compiler for verbose output.

Parameters: verbose if true, asks the compiler for verbose output