maven 排除AnnotationProcessorPaths中的依赖项

2eafrhcq  于 2023-03-01  发布在  Maven
关注(0)|答案(2)|浏览(428)

我有以下构建配置:
母体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>

其中一个子项目包含以下内容:

<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>

当我运行这个配置时,我总是得到一个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)
    ...

当我检查这些工件的依赖关系时,com.google.auto.value:auto-value:1.6.5after checking parents)依赖于com.squareup:javapoet:1.9.0com.google.dagger:dagger-compiler:2.24依赖于com.squareup:javapoet:1.11.1
当我检查ClassName::withoutAnnotationcom:squareup:javapoet:1.11.1中的签名时,发现是public ClassName withoutAnnotations()
ClassName::withoutAnnotationcom:squareup:javapoet:1.9.0中的签名是public TypeName withoutAnnotations()
因此,确实存在冲突。
如果是普通的依赖项,我知道要使用<exclusions>标记,但在本例中,如果添加这样的标记,会出现以下问题:Cannot find 'exclusions' in class org.apache.maven.plugin.compiler.DependencyCoordinate .
那么如何在annotationProcessorPaths中修复这样的冲突呢?

fkaflof6

fkaflof61#

从maven-compiler-plugin 3.11.0版开始,您还可以排除<annotationProcessorPaths>的传递依赖项:

<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>

另请参阅:MCOMPILER-395

5anewei6

5anewei62#

您可以手动覆盖JavaPoet的版本,方法是将其直接添加为注解处理器路径:

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

(We对于同样的问题,我也是这么做的。)
此外,我已经启动了更新AutoValue的过程来避免(目前)这个问题。到目前为止,这意味着只有这个提交。
(Also,技术说明:冲突并不像最初看起来那么严重:withoutAnnotations返回TypeName的版本在两个版本的字节码中都存在,这是因为,在1.10.0+中,编译器除了返回ClassName的"真实"版本之外,还自动生成了一个桥方法,我完全理解了这一点,根本没有文件a false bug report;))

相关问题