我正在为一种新语言开发一个插件,并试图向编译器添加对编译选项的支持。我使用了
org.gradle.api.tasks.compile.CompileOptions
类作为起点,并实现了我自己的类,如下所示:
class SvCompileOptions extends AbstractOptions {
private List<String> compilerArgs = Lists.newArrayList();
@Input
public List<String> getCompilerArgs() {
return compilerArgs;
}
public void setCompilerArgs(List<String> compilerArgs) {
this.compilerArgs = compilerArgs;
}
}
在我的构建中。gradle文件,我尝试了以下操作:
compileSv {
options.compilerArgs += [ "-foo" ]
}
(compileSv是一个具有SvCompileOptions类型的options属性的任务。)
我得到以下错误:
A problem occurred evaluating project ':uvc2'.
> java.lang.AbstractMethodError (no error message)
如果我将该行替换为:
compileSv {
options.compilerArgs.add("-foo")
}
然后一切都很好,但不是很有层次感。
有人能指出我做错了什么吗?
根据@tim\u yates的建议,我添加了一个函数来附加到
compilerArgs
:
class SvCompileOptions extends AbstractOptions {
void compilerArgs(String... args) {
this.compilerArgs.addAll(args as List)
}
}
根据@Opal的建议,我创建了一个赤裸裸的示例:
// File 'build.gradle'
buildscript {
dependencies {
classpath 'com.google.guava:guava:16+'
}
repositories {
mavenCentral()
}
}
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
class SvCompileOptions extends AbstractOptions {
private List<String> compilerArgs = Lists.newArrayList();
@Input
public List<String> getCompilerArgs() {
return compilerArgs;
}
public void setCompilerArgs(List<String> compilerArgs) {
this.compilerArgs = compilerArgs;
}
void compilerArgs(String... args) {
this.compilerArgs.addAll(args as List)
}
}
class SvCompile extends DefaultTask {
@TaskAction
protected void compile() {
println options.compilerArgs
}
@Nested
SvCompileOptions options = new SvCompileOptions()
}
task compileSv(type: SvCompile)
compileSv {
options.compilerArgs 'foo', 'bar'
}
代码将参数附加到空列表并打印
[foo, bar]
正如所料。如果我们尝试使用以下内容覆盖参数:
compileSv {
options.compilerArgs = ['one', 'two']
}
将打印错误消息:
* What went wrong:
A problem occurred evaluating root project 'so_compile_options2'.
> SvCompileOptions.setProperty(Ljava/lang/String;Ljava/lang/Object;)V
我不知道当类内联在中时,错误消息为什么会不同
build.gradle
,但我想这就是导致
AbstractMethodError
我看到了。
正如@Opal指出的,这个问题是由于在
AbstractOptions
班我尝试将以下方法添加到compile options类,但错误消息仍然存在:
class SvCompileOptions extends AbstractOptions {
private static final ImmutableSet<String> EXCLUDE_FROM_ANT_PROPERTIES =
ImmutableSet.of("compilerArgs");
@Override
protected boolean excludeFromAntProperties(String fieldName) {
return EXCLUDE_FROM_ANT_PROPERTIES.contains(fieldName);
}
// ...
}
exclude函数似乎根本没有被调用,就像我在其中添加了一个伪打印一样,它永远不会被发出。