org.gradle.api.tasks.compile.GroovyCompileOptions Maven / Gradle / Ivy
Show all versions of gradle-api Show documentation
/*
* Copyright 2007-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.api.tasks.compile;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import org.gradle.api.Incubating;
import org.gradle.api.Nullable;
import org.gradle.api.tasks.Console;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Nested;
import org.gradle.api.tasks.Optional;
import java.io.File;
import java.util.List;
import java.util.Map;
/**
* Compilation options to be passed to the Groovy compiler.
*/
public class GroovyCompileOptions extends AbstractOptions {
private static final long serialVersionUID = 0;
private static final ImmutableSet EXCLUDE_FROM_ANT_PROPERTIES =
ImmutableSet.of("forkOptions", "optimizationOptions", "stubDir", "keepStubs", "fileExtensions");
private boolean failOnError = true;
private boolean verbose;
private boolean listFiles;
private String encoding = "UTF-8";
private boolean fork = true;
private boolean keepStubs;
private List fileExtensions = ImmutableList.of("java", "groovy");
private GroovyForkOptions forkOptions = new GroovyForkOptions();
private Map optimizationOptions = Maps.newHashMap();
private File stubDir;
private File configurationScript;
private boolean javaAnnotationProcessing;
/**
* Tells whether the compilation task should fail if compile errors occurred. Defaults to {@code true}.
*/
@Input
public boolean isFailOnError() {
return failOnError;
}
/**
* Sets whether the compilation task should fail if compile errors occurred. Defaults to {@code true}.
*/
public void setFailOnError(boolean failOnError) {
this.failOnError = failOnError;
}
/**
* Tells whether to turn on verbose output. Defaults to {@code false}.
*/
@Console
public boolean isVerbose() {
return verbose;
}
/**
* Sets whether to turn on verbose output. Defaults to {@code false}.
*/
public void setVerbose(boolean verbose) {
this.verbose = verbose;
}
/**
* Tells whether to print which source files are to be compiled. Defaults to {@code false}.
*/
@Console
public boolean isListFiles() {
return listFiles;
}
/**
* Sets whether to print which source files are to be compiled. Defaults to {@code false}.
*/
public void setListFiles(boolean listFiles) {
this.listFiles = listFiles;
}
/**
* Tells the source encoding. Defaults to {@code UTF-8}.
*/
@Input
public String getEncoding() {
return encoding;
}
/**
* Sets the source encoding. Defaults to {@code UTF-8}.
*/
public void setEncoding(String encoding) {
this.encoding = encoding;
}
/**
* Tells whether to run the Groovy compiler in a separate process. Defaults to {@code true}.
*/
@Input
public boolean isFork() {
return fork;
}
/**
* Sets whether to run the Groovy compiler in a separate process. Defaults to {@code true}.
*/
public void setFork(boolean fork) {
this.fork = fork;
}
/**
* A Groovy script file that configures the compiler, allowing extensive control over how the code is compiled.
*
* The script is executed as Groovy code, with the following context:
*
* - The instance of CompilerConfiguration available as the {@code configuration} variable.
* - All static members of CompilerCustomizationBuilder pre imported.
*
*
*
* This facilitates the following pattern:
*
* withConfig(configuration) {
* // use compiler configuration DSL here
* }
*
*
*
* For example, to activate type checking for all Groovy classes…
*
* import groovy.transform.TypeChecked
*
* withConfig(configuration) {
* ast(TypeChecked)
* }
*
*
*
* Please see the Groovy compiler customization builder documentation
* for more information about the compiler configuration DSL.
*
*
* This feature is only available if compiling with Groovy 2.1 or later.
*
* @see CompilerConfiguration
* @see CompilerCustomizationBuilder
*/
@InputFile
@Incubating
@Optional
public File getConfigurationScript() {
return configurationScript;
}
/**
* Sets the path to the groovy configuration file.
*
* @see #getConfigurationScript()
*/
@Incubating
public void setConfigurationScript(@Nullable File configurationFile) {
this.configurationScript = configurationFile;
}
/**
* Whether the Groovy code should be subject to Java annotation processing.
*
* Annotation processing of Groovy code works by having annotation processors visit the Java stubs generated by the
* Groovy compiler in order to support joint compilation of Groovy and Java source.
*
* When set to {@code true}, stubs will be unconditionally generated for all Groovy sources, and Java annotations processors will be executed on those stubs.
*
* When this option is set to {@code false} (the default), Groovy code will not be subject to annotation processing, but any joint compiled Java code will be.
* If the compiler argument {@code "-proc:none"} was specified as part of the Java compile options, the value of this flag will be ignored.
* No annotation processing will be performed regardless, on Java or Groovy source.
*/
@Incubating
@Input
public boolean isJavaAnnotationProcessing() {
return javaAnnotationProcessing;
}
/**
* Sets whether Java annotation processors should process annotations on stubs.
*
* Defaults to {@code false}.
*/
@Incubating
public void setJavaAnnotationProcessing(boolean javaAnnotationProcessing) {
this.javaAnnotationProcessing = javaAnnotationProcessing;
}
/**
* Returns options for running the Groovy compiler in a separate process. These options only take effect
* if {@code fork} is set to {@code true}.
*/
@Nested
public GroovyForkOptions getForkOptions() {
return forkOptions;
}
/**
* Sets options for running the Groovy compiler in a separate process. These options only take effect
* if {@code fork} is set to {@code true}.
*/
public void setForkOptions(GroovyForkOptions forkOptions) {
this.forkOptions = forkOptions;
}
/**
* Returns optimization options for the Groovy compiler. Allowed values for an option are {@code true} and {@code false}.
* Only takes effect when compiling against Groovy 1.8 or higher.
*
*
Known options are:
*
*
* - indy
*
- Use the invokedynamic bytecode instruction. Requires JDK7 or higher and Groovy 2.0 or higher. Disabled by default.
*
- int
*
- Optimize operations on primitive types (e.g. integers). Enabled by default.
*
- all
*
- Enable or disable all optimizations. Note that some optimizations might be mutually exclusive.
*
*/
@Optional @Input
public Map getOptimizationOptions() {
return optimizationOptions;
}
/**
* Sets optimization options for the Groovy compiler. Allowed values for an option are {@code true} and {@code false}.
* Only takes effect when compiling against Groovy 1.8 or higher.
*/
public void setOptimizationOptions(Map optimizationOptions) {
this.optimizationOptions = optimizationOptions;
}
/**
* Returns the directory where Java stubs for Groovy classes will be stored during Java/Groovy joint
* compilation. Defaults to {@code null}, in which case a temporary directory will be used.
*/
@Internal
// TOOD:LPTR Should be just a relative path
public File getStubDir() {
return stubDir;
}
/**
* Sets the directory where Java stubs for Groovy classes will be stored during Java/Groovy joint
* compilation. Defaults to {@code null}, in which case a temporary directory will be used.
*/
public void setStubDir(File stubDir) {
this.stubDir = stubDir;
}
/**
* Returns the list of acceptable source file extensions. Only takes effect when compiling against
* Groovy 1.7 or higher. Defaults to {@code ImmutableList.of("java", "groovy")}.
*/
@Input
@Incubating
public List getFileExtensions() {
return fileExtensions;
}
/**
* Sets the list of acceptable source file extensions. Only takes effect when compiling against
* Groovy 1.7 or higher. Defaults to {@code ImmutableList.of("java", "groovy")}.
*/
@Incubating
public void setFileExtensions(List fileExtensions) {
this.fileExtensions = fileExtensions;
}
/**
* Tells whether Java stubs for Groovy classes generated during Java/Groovy joint compilation
* should be kept after compilation has completed. Useful for joint compilation debugging purposes.
* Defaults to {@code false}.
*/
@Input
public boolean isKeepStubs() {
return keepStubs;
}
/**
* Sets whether Java stubs for Groovy classes generated during Java/Groovy joint compilation
* should be kept after compilation has completed. Useful for joint compilation debugging purposes.
* Defaults to {@code false}.
*/
public void setKeepStubs(boolean keepStubs) {
this.keepStubs = keepStubs;
}
/**
* Convenience method to set {@link GroovyForkOptions} with named parameter syntax.
* Calling this method will set {@code fork} to {@code true}.
*/
public GroovyCompileOptions fork(Map forkArgs) {
fork = true;
forkOptions.define(forkArgs);
return this;
}
@Override
protected boolean excludeFromAntProperties(String fieldName) {
return EXCLUDE_FROM_ANT_PROPERTIES.contains(fieldName);
}
/**
* Internal method.
*/
@Override
public Map optionMap() {
Map map = super.optionMap();
map.putAll(forkOptions.optionMap());
if (optimizationOptions.containsKey("indy")) {
map.put("indy", optimizationOptions.get("indy"));
}
return map;
}
}