添加sourceSet时已设置endPosTable

时间:2016-12-19 09:55:22

标签: java eclipse gradle preprocessor

我正在使用自定义注释处理器并遇到新问题。 我正在使用Gradle(3.1.1),当我将处理器生成的文件路径添加到sourceSet时,我会遇到奇怪的问题。

之后我执行gradle build然后执行An exception has occurred in the compiler (1.8.0_91). Please file a bug against the Java compiler via the Java bug reporting page (http://bugreport.java.com) after checking the Bug Database (http://bugs.java.com) for duplicates. Include your program and the following diagnostic in your report. Thank you. java.lang.IllegalStateException: endPosTable already set at com.sun.tools.javac.util.DiagnosticSource.setEndPosTable(DiagnosticSource.java:136) at com.sun.tools.javac.util.Log.setEndPosTable(Log.java:350) at com.sun.tools.javac.main.JavaCompiler.parse(JavaCompiler.java:667) at com.sun.tools.javac.main.JavaCompiler.parseFiles(JavaCompiler.java:950) at com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.<init>(JavacProcessingEnvironment.java:892) at com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.next(JavacProcessingEnvironment.java:921) at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1187) at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1170) at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:856) at com.sun.tools.javac.main.Main.compile(Main.java:523) at com.sun.tools.javac.main.Main.compile(Main.java:381) at com.sun.tools.javac.main.Main.compile(Main.java:370) at com.sun.tools.javac.main.Main.compile(Main.java:361) at com.sun.tools.javac.Main.compile(Main.java:56) at com.sun.tools.javac.Main.main(Main.java:42) ,并且构建获取被销毁。 我得到的堆栈跟踪是一个非常常见的令我惊讶的事情。

{{1}}

如果我在构建之前总是清理我没有问题,那么当我不添加sourceSet时,构建总是成功的。 我甚至尝试在我的注释处理器中创建新生成的文件之前删除该文件,但这也没有做到。

在研究时我也发现了这个有趣的链接: JDK Bug report

但是考虑到我要么总是首先要清理,要么将它从我的sourceSet中删除,这不是很好。因为Eclipse不喜欢找不到符合惯例的文件。

您对如何解决此问题有任何建议吗?

2 个答案:

答案 0 :(得分:6)

由于这是JDK 8的官方Bug,旨在在JDK 9中解决,我实现了一个解决方法,如果已经存在,则使用gradle删除该文件。处理器不再有问题了,我可以将它保存在我的sourceSet中。

问候:)

编辑:解决方法本身。

gradle.ext.generatedQueriesDir = 'build/generated-sources/local/query'
/*
 * This is a workaround to delete the file that will be created by the annotation processor if it already exists.
 * There is a known bug in the Java compiler and JDK 8 which should be fixed at JDK 9.
 * http://bugs.java.com/bugdatabase/view_bug.do?bug_id=8067747 <-- Master report
 * http://bugs.java.com/bugdatabase/view_bug.do?bug_id=8146348 <-- duplicates master report
 */
if ( file( gradle.ext.generatedQueriesDir ).exists() ) {
  FileCollection collection = files { file( gradle.ext.generatedQueriesDir ).listFiles() }
  collection.each { delete it }
}

我们使用gradle,因此这是gradle脚本的变通方法。但基本上解决方法是删除在开始编译和预编译步骤之前生成的文件。

答案 1 :(得分:6)

我正在尝试在Android应用程序的测试源集中使用生成的Dagger *文件。我在@Nico的解决方法中改变了一些事情,现在一切都对我有用。

/build.gradle:

ext {
    generatedSourcesDir = 'build/generated/source/apt/test/debug'
}

/app/build.gradle

android {
    sourceSets {
        test.java.srcDirs += generatedSourcesDir
    }
}

tasks.withType(JavaCompile) {
   if (file(generatedSourcesDir).exists()) {
       FileCollection collection = files { file(generatedSourcesDir).listFiles() }
       collection.each { delete it }
   }
}

dependencies {
    ...
}