1

I have the following build configuration:

Parent POM:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.8.1</version>
      <configuration>
        <release>11</release>
        <annotationProcessorPaths>
          <path>
            <groupId>com.google.auto.value</groupId>
            <artifactId>auto-value</artifactId>
            <version>1.6.5</version>
          </path>
        </annotationProcessorPaths>
      </configuration>
    </plugin>
  </plugins>
</build>

One of the child project contains the following:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <configuration>
        <annotationProcessorPaths>
          <path>
            <groupId>com.google.dagger</groupId>
            <artifactId>dagger-compiler</artifactId>
            <version>2.24</version>
          </path>
        </annotationProcessorPaths>
      </configuration>
    </plugin>
  </plugins>
</build>

When I run this configuration, I keep getting an org.apache.maven.lifecycle.LifecycleExecutionException:

org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project imgn: Fatal error compiling
...
Caused by: org.apache.maven.plugin.MojoExecutionException: Fatal error compiling
...
Caused by: org.codehaus.plexus.compiler.CompilerException: java.lang.NoSuchMethodError: com.squareup.javapoet.ClassName.withoutAnnotations()Lcom/squareup/javapoet/ClassName;
...
Caused by: java.lang.RuntimeException: java.lang.NoSuchMethodError: com.squareup.javapoet.ClassName.withoutAnnotations()Lcom/squareup/javapoet/ClassName;
...
Caused by: java.lang.NoSuchMethodError: com.squareup.javapoet.ClassName.withoutAnnotations()Lcom/squareup/javapoet/ClassName;
    at dagger.internal.codegen.langmodel.DaggerElements.getTypeElement(DaggerElements.java:105)
    at dagger.internal.codegen.InjectBindingRegistryImpl$BindingsCollection.shouldGenerateBinding(InjectBindingRegistryImpl.java:134)
    ...

When I check the dependencies of those artifacts, com.google.auto.value:auto-value:1.6.5 (after checking parents) depends on com.squareup:javapoet:1.9.0, and com.google.dagger:dagger-compiler:2.24 depends on com.squareup:javapoet:1.11.1.

When I check the signature of ClassName::withoutAnnotation in com:squareup:javapoet:1.11.1 is public ClassName withoutAnnotations().

The signature of ClassName::withoutAnnotation in com:squareup:javapoet:1.9.0 is public TypeName withoutAnnotations().

So indeed, there is a clash.

If it were normal dependencies, I'd know to use the <exclusions> tag, but in this case, if I add such tag, I get the following issue: Cannot find 'exclusions' in class org.apache.maven.plugin.compiler.DependencyCoordinate.

So how do I fix such clash in annotationProcessorPaths?

Olivier Grégoire
  • 33,839
  • 23
  • 96
  • 137

2 Answers2

1

You can manually override the version of JavaPoet by adding it directly as an annotation-processor path:

<path>
  <groupId>com.squareup</groupId>
  <artifactId>javapoet</artifactId>
  <version>1.11.1</version>
</path>

(We did exactly that for the same problem.)

Also, I have set into motion the process of updating AutoValue to avoid (for now) this problem. So far, that's meant just this commit.

(Also, a technical note: The clash isn't as bad as it initially looks: A version of withoutAnnotations that returns TypeName is present in the bytecode for both versions. That's because, in 1.10.0+, the compiler automatically generates a bridge method in addition to the "real" version that returns ClassName. I totally understood this immediately and did not at all file a false bug report ;))

Chris Povirk
  • 3,738
  • 3
  • 29
  • 47
  • Yes! This works! I had found a workaround by redefining both annotation processors at the same time in a specific order (dagger first, AutoValue second, I think), but that was flunky. This is consistent and works. Also, I don't know why asking AutoValue to update their dependency didn't come to mind.Thank you all the line down for doing the appropriate things! – Olivier Grégoire Oct 06 '19 at 19:07
1

As of version 3.11.0 of the maven-compiler-plugin, you can exclude transitive dependencies for <annotationProcessorPaths> as well:

<annotationProcessorPaths>
  <path>
    <groupId>com.google.auto.value</groupId>
    <artifactId>auto-value</artifactId>
    <version>1.6.5</version>
    <exclusions>
      <exclusion>
        <groupId>com.squareup</groupId>
        <artifactId>javapoet</artifactId>
      </exclusion>
    </exclusions>
  </path>
</annotationProcessorPaths>

See also: MCOMPILER-395

beatngu13
  • 7,201
  • 6
  • 37
  • 66