Package org.codehaus.groovy.ant
Class Groovyc
java.lang.Object
org.apache.tools.ant.ProjectComponent
org.apache.tools.ant.Task
org.apache.tools.ant.taskdefs.MatchingTask
org.codehaus.groovy.ant.Groovyc
- All Implemented Interfaces:
Cloneable
,SelectorContainer
Compiles Groovy source files using Ant.
Typically involves using Ant from the command-line and an Ant build file such as:
<?xml version="1.0"?> <project name="MyGroovyBuild" default="compile"> <property name="groovy.home" value="/Path/To/Groovy"/> <property name="groovy.version" value="X.Y.Z"/> <path id="groovy.classpath"> <fileset dir="${groovy.home}/embeddable"> <include name="groovy-all-${groovy.version}.jar" /> </fileset> </path> <taskdef name="groovyc" classname="org.codehaus.groovy.ant.Groovyc" classpathref="groovy.classpath"/> <target name="compile" description="compile groovy sources"> <groovyc srcdir="src" listfiles="true" classpathref="groovy.classpath"/> </target> </project>
This task can take the following arguments:
- srcdir
- scriptExtension
- targetBytecode
- destdir
- sourcepath
- sourcepathRef
- classpath
- classpathRef
- listfiles
- failonerror
- proceed
- memoryInitialSize
- memoryMaximumSize
- encoding
- verbose
- includeantruntime
- includejavaruntime
- fork
- javaHome
- executable
- updatedProperty
- errorProperty
- includeDestClasses
- jointCompilationOptions
- stacktrace
- indy
- scriptBaseClass
- stubdir
- keepStubs
- forceLookupUnnamedFiles
- configscript
- javac
When this task executes, it will recursively scan srcdir and destdir looking for Groovy source files to compile. This task makes its compile decision based on timestamp.
A more elaborate build file showing joint compilation:
<?xml version="1.0"?> <project name="MyJointBuild" default="compile"> <property name="groovy.home" value="/Path/To/Groovy"/> <property name="groovy.version" value="X.Y.Z"/> <path id="groovy.classpath"> <fileset dir="${groovy.home}/embeddable"> <include name="groovy-all-${groovy.version}.jar" /> </fileset> </path> <target name="clean" description="remove all built files"> <delete dir="classes" /> </target> <target name="compile" depends="init" description="compile java and groovy sources"> <mkdir dir="classes" /> <groovyc destdir="classes" srcdir="src" listfiles="true" keepStubs="true" stubdir="stubs"> <javac debug="on" deprecation="true"/> <classpath> <fileset dir="classes"/> <path refid="groovy.classpath"/> </classpath> </groovyc> </target> <target name="init"> <taskdef name="groovyc" classname="org.codehaus.groovy.ant.Groovyc" classpathref="groovy.classpath"/> </target> </project>
Based on the implementation of the Javac task in Apache Ant.
Can also be used from AntBuilder
to allow the build file to be scripted in Groovy.
-
Field Summary
Modifier and TypeFieldDescriptionprotected File[]
protected CompilerConfiguration
protected boolean
protected boolean
Fields inherited from class org.apache.tools.ant.taskdefs.MatchingTask
fileset
Fields inherited from class org.apache.tools.ant.ProjectComponent
description, location, project
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addConfiguredJavac
(Javac javac) Add the configured nested javac task if present to initiate joint compilation.protected void
addToCompileList
(File[] newFiles) protected GroovyClassLoader
protected void
protected void
compile()
Adds a path to the classpath.Adds a path to sourcepath.Adds a path for source compilation.void
execute()
Executes the task.Gets the classpath to be used for this compilation.Get the configuration file used to customize the compilation configuration.Gets the destination directory into which the java source files should be compiled.Returns the encoding to be used when creating files.The value of the executable attribute, if any.boolean
Gets the failonerror flag.File[]
Gets the list of files to be compiled.boolean
Gets the forceLookupUnnamedFiles flag.boolean
Gets whether or not the ant classpath is to be included in the classpath.boolean
Gets whether or not the java runtime should be included in this task's classpath.boolean
getIndy()
Get the value of the indy flag.boolean
Gets the keepStubs flag.boolean
Get the listfiles flag.Gets the memoryInitialSize flag.Gets the memoryMaximumSize flag.Get the base script class name for the scripts (must derive from Script)Get the extension to use when searching for Groovy source files.Gets the sourcepath to be used for this compilation.Gets the source dirs to find the source java files.Gets the stub directory into which the Java source stub files should be generatedRetrieves the compiler bytecode compatibility level.boolean
Get the result of the groovyc task (success or failure).boolean
Gets the verbose flag.boolean
Get the value of the includeDestClasses property.protected CompilationUnit
protected Path
Recreate src.protected void
Clear the list of files to be compiled and copied.protected void
Scans the directory looking for source files to be compiled.void
setClasspath
(Path classpath) Set the classpath to be used for this compilation.void
Adds a reference to a classpath defined elsewhere.void
setConfigscript
(String configscript) Set the configuration file used to customize the compilation configuration.void
setDestdir
(File destDir) Set the destination directory into which the Java source files should be compiled.void
setEncoding
(String encoding) Sets the file encoding for generated files.void
setErrorProperty
(String errorProperty) The property to set on compilation failure.void
setExecutable
(String forkExecPath) Sets the name of the java executable to use when invoking the compiler in forked mode, ignored otherwise.void
setFailonerror
(boolean fail) Indicates whether the build will continue even if there are compilation errors; defaults to true.void
setForceLookupUnnamedFiles
(boolean forceLookupUnnamedFiles) Set the forceLookupUnnamedFiles flag.void
setFork
(boolean f) If true forks the Groovy compiler.void
setIncludeantruntime
(boolean include) If true, includes Ant's own classpath in the classpath.void
setIncludeDestClasses
(boolean includeDestClasses) This property controls whether to include the destination classes directory in the classpath given to the compiler.void
setIncludejavaruntime
(boolean include) If true, includes the Java runtime libraries in the classpath.void
setIndy
(boolean useIndy) Set the indy flag.void
setJavaHome
(File home) The JDK Home to use when forked.void
setKeepStubs
(boolean keepStubs) Set the keepStubs flag.void
setListfiles
(boolean list) If true, list the source files being handed off to the compiler.void
setMemoryInitialSize
(String memoryInitialSize) The initial size of the memory for the underlying VM if javac is run externally; ignored otherwise.void
setMemoryMaximumSize
(String memoryMaximumSize) The maximum size of the memory for the underlying VM if javac is run externally; ignored otherwise.void
setProceed
(boolean proceed) void
setScriptBaseClass
(String scriptBaseClass) Set the base script class name for the scripts (must derive from Script)void
setScriptExtension
(String scriptExtension) Set the extension to use when searching for Groovy source files.void
setSourcepath
(Path sourcepath) Set the sourcepath to be used for this compilation.void
Adds a reference to a source path defined elsewhere.void
Set the source directories to find the source Java files.void
setStacktrace
(boolean stacktrace) Enable compiler to report stack trace information if a problem occurs during compilation.void
setStubdir
(File stubDir) Set the stub directory into which the Java source stub files should be generated.void
setTargetBytecode
(String version) Sets the bytecode compatibility level.void
setUpdatedProperty
(String updatedProperty) The property to set on compilation success.void
setVerbose
(boolean verbose) Enable verbose compiling which will display which files are being compiled.Methods inherited from class org.apache.tools.ant.taskdefs.MatchingTask
add, addAnd, addContains, addContainsRegexp, addCustom, addDate, addDepend, addDepth, addDifferent, addFilename, addMajority, addModified, addNone, addNot, addOr, addPresent, addSelector, addSize, addType, appendSelector, createExclude, createExcludesFile, createInclude, createIncludesFile, createPatternSet, getDirectoryScanner, getImplicitFileSet, getSelectors, hasSelectors, selectorCount, selectorElements, setCaseSensitive, setDefaultexcludes, setExcludes, setExcludesfile, setFollowSymlinks, setIncludes, setIncludesfile, setProject, XsetIgnore, XsetItems
Methods inherited from class org.apache.tools.ant.Task
bindToOwner, getOwningTarget, getRuntimeConfigurableWrapper, getTaskName, getTaskType, getWrapper, handleErrorFlush, handleErrorOutput, handleFlush, handleInput, handleOutput, init, isInvalid, log, log, log, log, maybeConfigure, perform, reconfigure, setOwningTarget, setRuntimeConfigurableWrapper, setTaskName, setTaskType
Methods inherited from class org.apache.tools.ant.ProjectComponent
clone, getDescription, getLocation, getProject, setDescription, setLocation
-
Field Details
-
failOnError
protected boolean failOnError -
listFiles
protected boolean listFiles -
compileList
-
configuration
-
-
Constructor Details
-
Groovyc
public Groovyc()
-
-
Method Details
-
createSrc
Adds a path for source compilation.- Returns:
- a nested src element.
-
recreateSrc
Recreate src.- Returns:
- a nested src element.
-
setSrcdir
Set the source directories to find the source Java files.- Parameters:
srcDir
- the source directories as a path
-
getSrcdir
Gets the source dirs to find the source java files.- Returns:
- the source directories as a path
-
setScriptExtension
Set the extension to use when searching for Groovy source files. Accepts extensions in the form *.groovy, .groovy or groovy- Parameters:
scriptExtension
- the extension of Groovy source files
-
getScriptExtension
Get the extension to use when searching for Groovy source files.- Returns:
- the extension of Groovy source files
-
setTargetBytecode
Sets the bytecode compatibility level. The parameter can take one of the values inCompilerConfiguration.ALLOWED_JDKS
.- Parameters:
version
- the bytecode compatibility level
-
getTargetBytecode
Retrieves the compiler bytecode compatibility level.- Returns:
- bytecode compatibility level. Can be one of the values in
CompilerConfiguration.ALLOWED_JDKS
.
-
setDestdir
Set the destination directory into which the Java source files should be compiled.- Parameters:
destDir
- the destination director
-
getDestdir
Gets the destination directory into which the java source files should be compiled.- Returns:
- the destination directory
-
setSourcepath
Set the sourcepath to be used for this compilation.- Parameters:
sourcepath
- the source path
-
getSourcepath
Gets the sourcepath to be used for this compilation.- Returns:
- the source path
-
createSourcepath
Adds a path to sourcepath.- Returns:
- a sourcepath to be configured
-
setSourcepathRef
Adds a reference to a source path defined elsewhere.- Parameters:
r
- a reference to a source path
-
setClasspath
Set the classpath to be used for this compilation.- Parameters:
classpath
- an Ant Path object containing the compilation classpath.
-
getClasspath
Gets the classpath to be used for this compilation.- Returns:
- the class path
-
createClasspath
Adds a path to the classpath.- Returns:
- a class path to be configured
-
setClasspathRef
Adds a reference to a classpath defined elsewhere.- Parameters:
r
- a reference to a classpath
-
setListfiles
public void setListfiles(boolean list) If true, list the source files being handed off to the compiler. Default is false.- Parameters:
list
- if true list the source files
-
getListfiles
public boolean getListfiles()Get the listfiles flag.- Returns:
- the listfiles flag
-
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
-
setProceed
public void setProceed(boolean proceed) - Parameters:
proceed
- inverse of failonerror
-
getFailonerror
public boolean getFailonerror()Gets the failonerror flag.- Returns:
- the failonerror flag
-
setMemoryInitialSize
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
-
getMemoryInitialSize
Gets the memoryInitialSize flag.- Returns:
- the memoryInitialSize flag
-
setMemoryMaximumSize
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
-
getMemoryMaximumSize
Gets the memoryMaximumSize flag.- Returns:
- the memoryMaximumSize flag
-
setEncoding
Sets the file encoding for generated files.- Parameters:
encoding
- the file encoding to be used
-
getEncoding
Returns the encoding to be used when creating files.- Returns:
- the file encoding to use
-
setVerbose
public void setVerbose(boolean verbose) Enable verbose compiling which will display which files are being compiled. Default is false. -
getVerbose
public boolean getVerbose()Gets the verbose flag.- Returns:
- the verbose flag
-
setIncludeantruntime
public void setIncludeantruntime(boolean include) If true, includes Ant's own classpath in the classpath. Default is true. If setting to false and using groovyc in conjunction with AntBuilder you might need to explicitly add the Groovy jar(s) to the groovyc classpath using a nested classpath task.- Parameters:
include
- if true, includes Ant's own classpath in the classpath
-
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
-
setIncludejavaruntime
public void setIncludejavaruntime(boolean include) If true, includes the Java runtime libraries in the classpath. Default is false.- Parameters:
include
- if true, includes the Java runtime libraries 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
-
setFork
public void setFork(boolean f) If true forks the Groovy compiler. Default is false.- Parameters:
f
- "true|false|on|off|yes|no"
-
setJavaHome
The JDK Home to use when forked. Ignored if "executable" is specified.- Parameters:
home
- the java.home value to use, default is the current JDK's home
-
setExecutable
Sets the name of the java executable to use when invoking the compiler in forked mode, ignored otherwise.- Parameters:
forkExecPath
- the name of the executable- Since:
- Groovy 1.8.7
-
getExecutable
The value of the executable attribute, if any.- Returns:
- the name of the java executable
- Since:
- Groovy 1.8.7
-
setUpdatedProperty
The property to set on compilation 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.
-
setErrorProperty
The property to set on compilation failure. This property will be set if the compilation fails.- Parameters:
errorProperty
- the property name to use.
-
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.
-
isIncludeDestClasses
public boolean isIncludeDestClasses()Get the value of the includeDestClasses property.- Returns:
- the value.
-
getTaskSuccess
public boolean getTaskSuccess()Get the result of the groovyc task (success or failure).- Returns:
- true if compilation succeeded, or was not necessary, false if the compilation failed.
-
addConfiguredJavac
Add the configured nested javac task if present to initiate joint compilation. -
setStacktrace
public void setStacktrace(boolean stacktrace) Enable compiler to report stack trace information if a problem occurs during compilation. Default is false. -
setIndy
public void setIndy(boolean useIndy) Set the indy flag.- Parameters:
useIndy
- the indy flag
-
getIndy
public boolean getIndy()Get the value of the indy flag.- Returns:
- if to use indy
-
setScriptBaseClass
Set the base script class name for the scripts (must derive from Script)- Parameters:
scriptBaseClass
- Base class name for scripts (must derive from Script)
-
getScriptBaseClass
Get the base script class name for the scripts (must derive from Script)- Returns:
- Base class name for scripts (must derive from Script)
-
getConfigscript
Get the configuration file used to customize the compilation configuration.- Returns:
- a path to a configuration script
-
setConfigscript
Set the configuration file used to customize the compilation configuration.- Parameters:
configscript
- a path to a configuration script
-
setStubdir
Set the stub directory into which the Java source stub files should be generated. The directory need not exist and will not be deleted automatically - though its contents will be cleared unless 'keepStubs' is true. Ignored when forked.- Parameters:
stubDir
- the stub directory
-
getStubdir
Gets the stub directory into which the Java source stub files should be generated- Returns:
- the stub directory
-
setKeepStubs
public void setKeepStubs(boolean keepStubs) Set the keepStubs flag. Defaults to false. Set to true for debugging. Ignored when forked.- Parameters:
keepStubs
- should stubs be retained
-
getKeepStubs
public boolean getKeepStubs()Gets the keepStubs flag.- Returns:
- the keepStubs flag
-
setForceLookupUnnamedFiles
public void setForceLookupUnnamedFiles(boolean forceLookupUnnamedFiles) Set the forceLookupUnnamedFiles flag. Defaults to false. The Groovyc Ant task is frequently used in the context of a build system that knows the complete list of source files to be compiled. In such a context, it is wasteful for the Groovy compiler to go searching the classpath when looking for source files and hence by default the Groovyc Ant task calls the compiler in a special mode with such searching turned off. If you wish the compiler to search for source files then you need to set this flag totrue
.- Parameters:
forceLookupUnnamedFiles
- should unnamed source files be searched for on the classpath
-
getForceLookupUnnamedFiles
public boolean getForceLookupUnnamedFiles()Gets the forceLookupUnnamedFiles flag.- Returns:
- the forceLookupUnnamedFiles flag
-
execute
Executes the task.- Overrides:
execute
in classTask
- Throws:
BuildException
- if an error occurs
-
resetFileLists
protected void resetFileLists()Clear the list of files to be compiled and copied. -
scanDir
Scans the directory looking for source files to be compiled. The results are returned in the class variable compileList- Parameters:
srcDir
- The source directorydestDir
- The destination directoryfiles
- An array of filenames
-
addToCompileList
-
getFileList
Gets the list of files to be compiled.- Returns:
- the list of files as an array
-
checkParameters
- Throws:
BuildException
-
compile
protected void compile() -
makeCompileUnit
-
buildClassLoaderFor
-