maven-compiler-plugin icon indicating copy to clipboard operation
maven-compiler-plugin copied to clipboard

[MCOMPILER-310] Different behaviour between JDK 8 / JDK 9 related to annotation processor usage

Open jira-importer opened this issue 8 years ago • 7 comments

Karl Heinz Marbaise opened MCOMPILER-310 and commented

Based on the SO question is looks like we have a difference in behaviour between JDK 8 / JDK 9 related to the picking up of annotation processors. If you run the following code under JDK 8 (except for a module-info.java file):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>example</groupId>
    <artifactId>jigsaw</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.immutables</groupId>
            <artifactId>value</artifactId>
            <version>2.5.6</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.7.0</version>
        </plugin>
      </plugins>
    </build>
</project>

The maven-compiler-plugin will automatically pickup the annotation processor and produce the classed from the annotation. If you run the same with JDK 9 this will not work anymore. Only if you explicitly add the annotation processor configuration to maven-compiler-plugin it will work as expected:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>example</groupId>
    <artifactId>jigsaw</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.immutables</groupId>
            <artifactId>value</artifactId>
            <version>2.5.6</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.7.0</version>
          <configuration>
            <source>9</source>
            <target>9</target>
            <annotationProcessorPaths>
              <dependency>
                  <groupId>org.immutables</groupId>
                  <artifactId>value</artifactId>
                  <version>2.5.6</version>
              </dependency>
            </annotationProcessorPaths>
          </configuration>
        </plugin>
      </plugins>
    </build>
</project>

I'm not sure if this is based on the usage of modules (module-path instead of classpath)?


Affects: 3.7.0

5 votes, 10 watchers

jira-importer avatar Sep 30 '17 16:09 jira-importer

Werner Glanzer commented

Is there any activity on this issue? Is it an issue?

jira-importer avatar Dec 11 '17 12:12 jira-importer

Martin Miller commented

Well its not a big issue now that I found out about the change required, but I lost many hours !

I have my own little Annotation Processor that was working under maven, but as soon as module-info.java are created, it complained it did not find the <annotationprocessor> specified in the pom. Compiling by hand did work but not with maven.

by hand:

/usr/lib/jvm/java-9-jdk/bin/javac -d target/classes -classpath target/classes:../annotation-processor/target/annotation-processor-1.0-SNAPSHOT.jar -sourcepath example/src/main/java -target 1.9 -source 1.9 src/main/java/com/test/App.java

I don't know how different maven used javac parameters to compile, but it didn't work out of the box without the processor paths.

Maybe this should be more documented (java 9 modules + annotation processors now require paths) ?

jira-importer avatar Feb 04 '18 05:02 jira-importer

Cody Lerum commented

This issue has also popped up for me with Hibernate JPA Metamodel Generator.

Behavior changes between Java 8 and 9+

 

http://hibernate.org/orm/tooling/

jira-importer avatar Jun 01 '18 01:06 jira-importer

Paul Scholz commented

Behavior did not change while using classpath. This should be a new feature, annotation processing discovery using the Java 9+ way. This will be acoblished by using --processor-module-path instead of -processorpath when using module-info.java in a project.

There is also a open issue on the plexus-compiler issue #53 related to this behavior.

jira-importer avatar Jul 02 '19 10:07 jira-importer

Paul Scholz commented

Necessary changes in plexus-compiler where merged, those changes will be includes in the next release.

jira-importer avatar May 10 '21 06:05 jira-importer

Ralph Goers commented

One issue I have had with Log4j is that in the module where we create the annotation processor I have to:

  1. Compile the classes with proc=none with no module-info.java to create the annotation processor class.
  2. Compile the classes with proc=only with no module-info.java to be able to find the newly created processor class. If a module-info.java is present the annotation processor cannot be located because there is no way to specify the current classes directory - it only accepts a dependency.
  3. Compile the unit tests without a module-info.java for the same reason.
  4. Compile the module-info.java for both the main and tests classes.

Will this change allow the current classes directory to be included in the processor path?

jira-importer avatar May 11 '21 03:05 jira-importer

Paul Scholz commented

This seams to be a bit out of scope.This issue focuses on the compiler options "--processor-module-path" and "--processor-path".

Point 2 could be caused by the annotation processor not being declared inside the module-info. (I think this is because ServiceLocator behavior is dependent on class-/module-path.) 

jira-importer avatar Jun 03 '21 16:06 jira-importer