auto-matter icon indicating copy to clipboard operation
auto-matter copied to clipboard

Does not compile twice with maven-compiler-plugin 3.3

Open jooon opened this issue 9 years ago • 10 comments

I noticed this in one of my own projects where I was using the latest maven-compiler-plugin 3.3, but you can reproduce it in auto-matter as well.

$ git diff
diff --git a/pom.xml b/pom.xml
index eee9221..b31cb35 100644
--- a/pom.xml
+++ b/pom.xml
@@ -57,7 +57,7 @@
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-compiler-plugin</artifactId>
-        <version>2.5.1</version>
+        <version>3.3</version>
         <configuration>
           <source>1.7</source>
           <target>1.7</target>
$ mvn compile
...
[INFO] BUILD SUCCESS

First time works.

$ java -cp example/target/classes io.norberg.automatter.example.SimpleExample 
bar: 17
foo: hello world
foobar: Foobar{foo=hello world, bar=17}
modified: Foobar{foo=hello world, bar=18}

Compiling a second time breaks

$ mvn compile
...
[INFO] --- maven-compiler-plugin:3.3:compile (default-compile) @ auto-matter-example ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 12 source files to /var/tmp/auto-matter/example/target/classes
An exception has occurred in the compiler (1.8.0_40). Please file a bug at the Java Developer Connection (http://java.sun.com/webapps/bugreport)  after checking the Bug Parade 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)
...

https://gist.github.com/jooon/386352b9f922d39dda78

Is this an actual java bug or a problem with maven-compiler-plugin or auto-matter?

jooon avatar Apr 16 '15 14:04 jooon

This seems to be an issue with javac and/or maven-compiler-plugin

https://bugs.openjdk.java.net/browse/JDK-8067747 https://jira.codehaus.org/browse/MCOMPILER-236

One straightforward fix is to get into the habit of mvn clean <command>. This also works around issues like e.g. maven-compiler-plugin leaving orphan class files around in target/, etc.

Another suggested workaround is to set maven-compiler-plugin configuration option useIncrementalCompilation to false. I have not verified myself but I think @mattnworb has had success with that.

danielnorberg avatar Apr 17 '15 00:04 danielnorberg

Invoking javac directly seems to work correctly.

javac example/src/main/java/io/norberg/automatter/example/*.java \
> -d example/target/classes \
> -classpath example/target/classes:processor/target/classes:annotation/target/classes:$HOME/.m2/repository/com/squareup/javawriter/2.4.0/javawriter-2.4.0.jar:$HOME/.m2/repository/org/modeshape/modeshape-common/3.8.1.Final/modeshape-common-3.8.1.Final.jar:jackson/target/classes:$HOME/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.4.0/jackson-databind-2.4.0.jar:$HOME/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.4.0/jackson-annotations-2.4.0.jar:$HOME/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.4.0/jackson-core-2.4.0.jar:gson/target/classes:$HOME/.m2/repository/com/google/code/gson/gson/2.3/gson-2.3.jar:$HOME/.m2/repository/com/google/code/findbugs/jsr305/3.0.0/jsr305-3.0.0.jar:$HOME/.m2/repository/com/google/guava/guava/18.0/guava-18.0.jar: \
> -sourcepath example/src/main/java:example/target/generated-sources/annotations: \
> -s example/target/generated-sources/annotations \
> -g -nowarn \
> -target 1.6 -source 1.6 \
> -encoding UTF-8
Note: example/target/generated-sources/annotations/io/norberg/automatter/example/OptionalFoobarBuilder.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

danielnorberg avatar Apr 17 '15 00:04 danielnorberg

Added a readme comment about this issue:

https://github.com/danielnorberg/auto-matter#known-issues

Ideally we'd contribute to maven-compiler-plugin and try to resolve this issue upstream. Any takers? :smiley:

danielnorberg avatar Apr 17 '15 05:04 danielnorberg

Correct that setting useIncrementalCompilation to false as a workaround works (at least with 1.8.0_40), and I've only seen this when compiling with JDK8 (i.e. when JAVA_HOME is set to 1.8.x).

mattnworb avatar Apr 17 '15 11:04 mattnworb

Regarding the javac example above - Maven sets the -sourcepath to be both src/main/java and target/generated-sources/annotations:

$ mvn -X -Dmaven.compiler.verbose=true compile
...
[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-compiler-plugin:3.2:compile' with basic configurator -->
...
[DEBUG] Source roots:
[DEBUG]  /Users/mattbrown/code/some-project/src/main/java
[DEBUG]  /Users/mattbrown/code/some-project/target/generated-sources/annotations
[DEBUG] Command line options:
[DEBUG] -d /Users/mattbrown/code/some-project/target/classes -classpath ... -sourcepath /Users/mattbrown/code/some-project/src/main/java:/Users/mattbrown/code/some-project/target/generated-sources/annotations: -s /Users/mattbrown/code/some-project/target/generated-sources/annotations -g -verbose -nowarn -target 1.8 -source 1.8 -encoding UTF-8

and the compiler output seems to show that the javac bug is hit when parsing one of the generated files (but I could be reading it wrong, since the error comes after javac says "parsing completed" of the Builder that auto-matter generated):

...
[loading ZipFileIndexFileObject[/Users/mattbrown/.m2/repository/io/norberg/auto-matter-annotation/0.10.0/auto-matter-annotation-0.10.0.jar(io/norberg/automatter/AutoMatter$Field.class)]]
[loading ZipFileIndexFileObject[/Users/mattbrown/.m2/repository/com/google/guava/guava/17.0/guava-17.0.jar(com/google/common/annotations/GwtIncompatible.class)]]
Round 1:
    input files: {com.spotify.foobar.VideoResource, com.spotify.foobar.ServiceRunner, com.spotify.foobar.SSPRequestBuilder, com.spotify.foobar.UrlResolver, com.spotify.foobar.ConfigUrlResolver, com.spotify.foobar.Service, com.spotify.foobar.SSPRequest}
    annotations: [java.lang.Override, javax.annotation.Generated, io.norberg.automatter.AutoMatter.Field, io.norberg.automatter.AutoMatter]
    last round: false
Processor io.norberg.automatter.processor.AutoMatterProcessor matches [io.norberg.automatter.AutoMatter] and returns false.
[parsing started RegularFileObject[/Users/mattbrown/code/some-project/target/generated-sources/annotations/com/spotify/foobar/SSPRequestBuilder.java]]
[parsing completed 4ms]
An exception has occurred in the compiler (1.8.0_40). Please file a bug at the Java Developer Connection (http://java.sun.com/webapps/bugreport)  after checking the Bug Parade for duplicates. Include your program and the following diagnostic in your report.  Thank you.
java.lang.IllegalStateException: endPosTable already set

mattnworb avatar Apr 17 '15 11:04 mattnworb

maven-compiler-plugin 3.1 and earlier work. 3.2 and 3.3 fail.

Compiling with java7 also breaks but with a different error message:

$ echo $JAVA_HOME
/usr/lib/jvm/jdk-7-oracle-x64
$ mvn -X -Dmaven.compiler.verbose=true compile
...
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.3:compile (default-compile) on project auto-matter-example: Compilation failure: Compilation failure:
[ERROR] /home/jon/src/github.com/danielnorberg/auto-matter/example/target/generated-sources/annotations/io/norberg/automatter/example/CollectionsFoobarBuilder.java:[15,7] duplicate class: io.norberg.automatter.example.CollectionsFoobarBuilder
[ERROR] /home/jon/src/github.com/danielnorberg/auto-matter/example/target/generated-sources/annotations/io/norberg/automatter/example/NullableFoobarBuilder.java:[7,7] duplicate class: io.norberg.automatter.example.NullableFoobarBuilder
[ERROR] /home/jon/src/github.com/danielnorberg/auto-matter/example/target/generated-sources/annotations/io/norberg/automatter/example/FoobarBuilder.java:[7,14] duplicate class: io.norberg.automatter.example.FoobarBuilder
[ERROR] /home/jon/src/github.com/danielnorberg/auto-matter/example/target/generated-sources/annotations/io/norberg/automatter/example/OptionalFoobarBuilder.java:[8,7] duplicate class: io.norberg.automatter.example.OptionalFoobarBuilder
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.3:compile (default-compile) on project auto-matter-example: Compilation failure
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:213)

jooon avatar Apr 17 '15 14:04 jooon

From the maven-compiler-plugin changelog for 3.2, sounds like maybe MCOMPILER-159 could be related?

mattnworb avatar Apr 17 '15 14:04 mattnworb

Works fine with the following environment.

Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T13:57:37+02:00)
Maven home: /home/udoprog/usr/apache-maven-3.3.3
Java version: 1.8.0_60, vendor: Oracle Corporation
Java home: /usr/lib/jvm/jdk-8-oracle-x64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.16.0-4-amd64", arch: "amd64", family: "unix"

Might have been fixed?

udoprog avatar Aug 21 '15 20:08 udoprog

Still breaks for me with maven 3.3.3. @udoprog, did you compile twice without a clean in between?

danielnorberg avatar Aug 21 '15 21:08 danielnorberg

My bad, missed to remove useIncrementalCompilation. I can indeed produce this.

udoprog avatar Aug 22 '15 06:08 udoprog