| Method from org.codehaus.groovy.ant.Groovyc Detail: |
public void addConfiguredJavac(Javac javac) {
this.javac = javac;
jointCompilation = true;
}
Add the configured nested javac task if present to initiate joint compilation. |
protected void addToCompileList(File[] newFiles) {
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;
}
}
|
protected GroovyClassLoader buildClassLoaderFor() {
ClassLoader parent = this.getClass().getClassLoader();
if (parent instanceof AntClassLoader) {
AntClassLoader antLoader = (AntClassLoader) parent;
String[] pathElm = antLoader.getClasspath().split(File.pathSeparator);
List classpath = configuration.getClasspath();
/*
* Iterate over the classpath provided to groovyc, and add any missing path
* entries to the AntClassLoader. This is a workaround, since for some reason
* 'directory' classpath entries were not added to the AntClassLoader' classpath.
*/
for (Iterator iter = classpath.iterator(); iter.hasNext();) {
String cpEntry = (String) iter.next();
boolean found = false;
for (int i = 0; i < pathElm.length; i++) {
if (cpEntry.equals(pathElm[i])) {
found = true;
break;
}
}
/*
* fix for GROOVY-2284
* seems like AntClassLoader doesn't check if the file
* may not exist in the classpath yet
*/
if (!found && new File(cpEntry).exists())
antLoader.addPathElement(cpEntry);
}
}
return new GroovyClassLoader(parent, configuration);
}
|
protected void checkParameters() throws BuildException {
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());
}
if (encoding != null && !Charset.isSupported(encoding)) {
throw new BuildException("encoding \"" + encoding + "\" not supported.");
}
}
|
protected void compile() {
try {
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);
}
}
Path classpath = getClasspath() != null ? getClasspath() : new Path(getProject());
// extract joint options, some get pushed up...
List jointOptions = new ArrayList();
if (jointCompilation) {
for (Iterator i = javac.getRuntimeConfigurableWrapper().getAttributeMap().entrySet().iterator(); i.hasNext();) {
final Map.Entry e = (Map.Entry) i.next();
final String key = e.getKey().toString();
final String value = e.getValue().toString();
if (key.indexOf("debug") != -1) {
String level = "";
if (javac.getDebugLevel() != null) {
level = ":" + javac.getDebugLevel();
}
jointOptions.add("-Fg" + level);
} else if (key.indexOf("debugLevel") != -1) {
// ignore, taken care of in debug
} else if (((key.indexOf("nowarn") != -1)
|| (key.indexOf("verbose") != -1)
|| (key.indexOf("deprecation") != -1)
) && ("on".equalsIgnoreCase(value) || "true".equalsIgnoreCase(value) || "yes".equalsIgnoreCase("value"))
) {
jointOptions.add("-F" + key);
} else if (key.indexOf("classpath") != -1) {
classpath.add(javac.getClasspath());
} else if ((key.indexOf("depend") != -1)
|| (key.indexOf("extdirs") != -1)
|| (key.indexOf("encoding") != -1)
|| (key.indexOf("source") != -1)
|| (key.indexOf("target") != -1)
|| (key.indexOf("verbose") != -1)
|| (key.indexOf("depend") != -1)) {
jointOptions.add("-J" + key + "=" + value);
} else {
log("The option " + key + " cannot be set on the contained < javac > element. The option will be ignored", Project.MSG_WARN);
}
// includes? excludes?
}
}
String separator = System.getProperty("file.separator");
List commandLineList = new ArrayList();
if (fork) {
String javaHome;
if (forkJDK != null) {
javaHome = forkJDK.getPath();
} else {
javaHome = System.getProperty("java.home");
}
if (includeAntRuntime) {
classpath.addExisting((new Path(getProject())).concatSystemClasspath("last"));
}
if (includeJavaRuntime) {
classpath.addJavaRuntime();
}
commandLineList.add(javaHome + separator + "bin" + separator + "java");
commandLineList.add("-classpath");
commandLineList.add(classpath.toString());
if ((memoryInitialSize != null) && !memoryInitialSize.equals("")) {
commandLineList.add("-Xms" + memoryInitialSize);
}
if ((memoryMaximumSize != null) && !memoryMaximumSize.equals("")) {
commandLineList.add("-Xmx" + memoryMaximumSize);
}
commandLineList.add("org.codehaus.groovy.tools.FileSystemCompiler");
}
commandLineList.add("--classpath");
commandLineList.add(classpath.toString());
if (jointCompilation) {
commandLineList.add("-j");
commandLineList.addAll(jointOptions);
}
commandLineList.add("-d");
commandLineList.add(destDir.getPath());
if (encoding != null) {
commandLineList.add("--encoding");
commandLineList.add(encoding);
}
if (stacktrace) {
commandLineList.add("-e");
}
// check to see if an external file is needed
int count = 0;
if (fork) {
for (int i = 0; i < compileList.length; i++) {
count += compileList[i].getPath().length();
}
for (Iterator iter = commandLineList.iterator(); iter.hasNext();) {
count += iter.next().toString().length();
}
count += compileList.length;
count += commandLineList.size();
}
// 32767 is the command line length limit on Windows
if (fork && (count > 32767)) {
try {
File tempFile = File.createTempFile("groovyc-files-", ".txt");
temporaryFiles.add(tempFile);
PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
for (int i = 0; i < compileList.length; i++) {
pw.println(compileList[i].getPath());
}
pw.close();
commandLineList.add("@" + tempFile.getPath());
} catch (IOException e) {
log("Error createing file list", e, Project.MSG_ERR);
}
} else {
for (int i = 0; i < compileList.length; i++) {
commandLineList.add(compileList[i].getPath());
}
}
final String[] commandLine = new String[commandLineList.size()];
for (int i = 0; i < commandLine.length; ++i) {
commandLine[i] = (String) commandLineList.get(i);
}
if (fork) {
// use the main method in FileSystemCompiler
final Execute executor = new Execute(); // new LogStreamHandler ( attributes , Project.MSG_INFO , Project.MSG_WARN ) ) ;
executor.setAntRun(getProject());
executor.setWorkingDirectory(getProject().getBaseDir());
executor.setCommandline(commandLine);
try {
executor.execute();
}
catch (final IOException ioe) {
throw new BuildException("Error running forked groovyc.", ioe);
}
final int returnCode = executor.getExitValue();
if (returnCode != 0) {
if (failOnError) {
throw new BuildException("Forked groovyc returned error code: " + returnCode);
} else {
log("Forked groovyc returned error code: " + returnCode, Project.MSG_ERR);
}
}
} else {
// hand crank it so we can add our own compiler configuration
try {
Options options = FileSystemCompiler.createCompilationOptions();
PosixParser cliParser = new PosixParser();
CommandLine cli;
cli = cliParser.parse(options, commandLine);
configuration = FileSystemCompiler.generateCompilerConfigurationFromOptions(cli);
//
// Load the file name list
String[] filenames = FileSystemCompiler.generateFileNamesFromOptions(cli);
boolean fileNameErrors = filenames == null;
fileNameErrors = fileNameErrors && !FileSystemCompiler.validateFiles(filenames);
if (!fileNameErrors) {
FileSystemCompiler.doCompilation(configuration, makeCompileUnit(), filenames);
}
} catch (Exception re) {
Throwable t = re;
if ((re.getClass() == RuntimeException.class) && (re.getCause() != null)) {
// unwrap to the real exception
t = re.getCause();
}
StringWriter writer = new StringWriter();
new ErrorReporter(t, false).write(new PrintWriter(writer));
String message = writer.toString();
if (failOnError) {
log(message, Project.MSG_INFO);
throw new BuildException("Compilation Failed", t, getLocation());
} else {
log(message, Project.MSG_ERR);
}
}
}
}
} finally {
Iterator< File > files = temporaryFiles.iterator();
while (files.hasNext()) {
File tmpFile = files.next();
try {
FileSystemCompiler.deleteRecursive(tmpFile);
} catch (Throwable t) {
System.err.println("error: could not delete temp files - " + tmpFile.getPath());
}
}
}
}
|
public Path createClasspath() {
if (compileClasspath == null) {
compileClasspath = new Path(getProject());
}
return compileClasspath.createPath();
}
Adds a path to the classpath. |
public Path createSourcepath() {
if (compileSourcepath == null) {
compileSourcepath = new Path(getProject());
}
return compileSourcepath.createPath();
}
Adds a path to sourcepath. |
public Path createSrc() {
if (src == null) {
src = new Path(getProject());
}
return src.createPath();
}
Adds a path for source compilation. |
public void execute() throws BuildException {
checkParameters();
resetFileLists();
if (javac != null) jointCompilation = true;
// scan source directories and dest directory to build up
// compile lists
String[] list = src.list();
for (int i = 0; i < list.length; i++) {
File file = getProject().resolveFile(list[i]);
if (!file.exists()) {
throw new BuildException("srcdir \"" + file.getPath() + "\" does not exist!", getLocation());
}
DirectoryScanner ds = this.getDirectoryScanner(file);
String[] files = ds.getIncludedFiles();
scanDir(file, destDir != null ? destDir : file, files);
}
compile();
if (updatedProperty != null
&& taskSuccess
&& compileList.length != 0) {
getProject().setNewProperty(updatedProperty, "true");
}
}
|
public Path getClasspath() {
return compileClasspath;
}
Gets the classpath to be used for this compilation. |
public File getDestdir() {
return destDir;
}
Gets the destination directory into which the java source files
should be compiled. |
public String getEncoding() {
return encoding;
}
Returns the encoding to be used when creating files. |
public boolean getFailonerror() {
return failOnError;
}
Gets the failonerror flag. |
public File[] getFileList() {
return compileList;
}
Gets the list of files to be compiled. |
public boolean getIncludeantruntime() {
return includeAntRuntime;
}
Gets whether or not the ant classpath is to be included in the classpath. |
public boolean getIncludejavaruntime() {
return includeJavaRuntime;
}
Gets whether or not the java runtime should be included in this
task's classpath. |
public boolean getListfiles() {
return listFiles;
}
|
public String getMemoryInitialSize() {
return memoryInitialSize;
}
Gets the memoryInitialSize flag. |
public String getMemoryMaximumSize() {
return memoryMaximumSize;
}
Gets the memoryMaximumSize flag. |
public Path getSourcepath() {
return compileSourcepath;
}
Gets the sourcepath to be used for this compilation. |
public Path getSrcdir() {
return src;
}
Gets the source dirs to find the source java files. |
public File getStubdir() {
return (File) configuration.getJointCompilationOptions().get("stubDir");
}
Gets the stub directory into which the Java source stub
files should be generated |
public boolean getTaskSuccess() {
return taskSuccess;
}
Get the result of the groovyc task (success or failure). |
public boolean getVerbose() {
return verbose;
}
|
public boolean isIncludeDestClasses() {
return includeDestClasses;
}
Get the value of the includeDestClasses property. |
protected CompilationUnit makeCompileUnit() {
if (configuration.getJointCompilationOptions() != null) {
if (!configuration.getJointCompilationOptions().containsKey("stubDir")) {
try {
File tempStubDir = FileSystemCompiler.createTempDir();
temporaryFiles.add(tempStubDir);
configuration.getJointCompilationOptions().put("stubDir", tempStubDir);
} catch (IOException ioe) {
throw new BuildException(ioe);
}
}
return new JavaAwareCompilationUnit(configuration, buildClassLoaderFor());
} else {
return new CompilationUnit(configuration, null, buildClassLoaderFor());
}
}
|
protected Path recreateSrc() {
src = null;
return createSrc();
}
|
protected void resetFileLists() {
compileList = new File[0];
}
Clear the list of files to be compiled and copied.. |
protected void scanDir(File srcDir,
File destDir,
String[] files) {
GlobPatternMapper m = new GlobPatternMapper();
m.setFrom("*.groovy");
m.setTo("*.class");
SourceFileScanner sfs = new SourceFileScanner(this);
File[] newFiles = sfs.restrictAsFiles(files, srcDir, destDir, m);
addToCompileList(newFiles);
if (jointCompilation) {
m.setFrom("*.java");
m.setTo("*.class");
newFiles = sfs.restrictAsFiles(files, srcDir, destDir, m);
addToCompileList(newFiles);
}
}
Scans the directory looking for source files to be compiled.
The results are returned in the class variable compileList |
public void setClasspath(Path classpath) {
if (compileClasspath == null) {
compileClasspath = classpath;
} else {
compileClasspath.append(classpath);
}
}
Set the classpath to be used for this compilation. |
public void setClasspathRef(Reference r) {
createClasspath().setRefid(r);
}
Adds a reference to a classpath defined elsewhere. |
public void setDestdir(File destDir) {
this.destDir = destDir;
}
Set the destination directory into which the Java source
files should be compiled. |
public void setEncoding(String encoding) {
this.encoding = encoding;
}
Sets the file encoding for generated files. |
public void setErrorProperty(String errorProperty) {
this.errorProperty = errorProperty;
}
The property to set on compliation failure.
This property will be set if the compilation
fails. |
public void setFailonerror(boolean fail) {
failOnError = fail;
}
Indicates whether the build will continue
even if there are compilation errors; defaults to true. |
public void setFork(boolean f) {
fork = f;
}
If true forks the Groovy compiler. |
public void setIncludeDestClasses(boolean includeDestClasses) {
this.includeDestClasses = includeDestClasses;
}
This property controls whether to include the
destination classes directory in the classpath
given to the compiler.
The default value is "true". |
public void setIncludeantruntime(boolean include) {
includeAntRuntime = include;
}
If true, includes Ant's own classpath in the classpath. |
public void setIncludejavaruntime(boolean include) {
includeJavaRuntime = include;
}
If true, includes the Java runtime libraries in the classpath. |
public void setJavaHome(File home) {
forkJDK = home;
}
The JDK Home to use when forked. |
public void setListfiles(boolean list) {
listFiles = list;
}
If true, list the source files being handed off to the compiler. |
public void setMemoryInitialSize(String memoryInitialSize) {
this.memoryInitialSize = 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) |
public void setMemoryMaximumSize(String memoryMaximumSize) {
this.memoryMaximumSize = 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) |
public void setProceed(boolean proceed) {
failOnError = !proceed;
}
|
public void setSourcepath(Path sourcepath) {
if (compileSourcepath == null) {
compileSourcepath = sourcepath;
} else {
compileSourcepath.append(sourcepath);
}
}
Set the sourcepath to be used for this compilation. |
public void setSourcepathRef(Reference r) {
createSourcepath().setRefid(r);
}
Adds a reference to a source path defined elsewhere. |
public void setSrcdir(Path srcDir) {
if (src == null) {
src = srcDir;
} else {
src.append(srcDir);
}
}
Set the source directories to find the source Java files. |
public void setStacktrace(boolean stacktrace) {
this.stacktrace = stacktrace;
}
Enable compiler to report stack trace information if a problem occurs
during compilation. |
public void setStubdir(File stubDir) {
jointCompilation = true;
configuration.getJointCompilationOptions().put("stubDir", stubDir);
}
Set the stub directory into which the Java source stub
files should be generated. The directory should exist
will not be deleted automatically. |
public void setUpdatedProperty(String updatedProperty) {
this.updatedProperty = 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. |
public void setVerbose(boolean verbose) {
this.verbose = verbose;
}
Enable verbose compiling which will display which files
are being compiled |