【问题标题】:Exclude dependencies in AnnotationProcessorPaths排除 AnnotationProcessorPaths 中的依赖项
【发布时间】:2019-08-14 07:55:02
【问题描述】:

我有以下构建配置:

父 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.5 (after checking parents) 依赖于 com.squareup:javapoet:1.9.0com.google.dagger:dagger-compiler:2.24 依赖于 com.squareup:javapoet:1.11.1

当我检查signature of ClassName::withoutAnnotation in com:squareup:javapoet:1.11.1public ClassName withoutAnnotations()

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

确实,有冲突。

如果是普通依赖,我会知道使用&lt;exclusions&gt; 标签,但在这种情况下,如果我添加这样的标签,我会遇到以下问题:Cannot find 'exclusions' in class org.apache.maven.plugin.compiler.DependencyCoordinate

那么我该如何解决annotationProcessorPaths 中的这种冲突?

【问题讨论】:

    标签: maven dagger-2 maven-compiler-plugin auto-value


    【解决方案1】:

    您可以通过直接将其添加为注释处理器路径来手动覆盖 JavaPoet 的版本:

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

    (我们@9​​87654321@同样的问题。)

    另外,我已经启动了更新 AutoValue 的过程,以避免(目前)这个问题。到目前为止,这只是this commit

    (另外,技术说明:冲突并不像最初看起来那么糟糕:返回 TypeNamewithoutAnnotations 版本存在于两个版本的字节码中。这是因为,在 1.10.0+ , 编译器除了返回ClassName的“真实”版本外,还自动生成桥接方法。我立即完全理解了这一点,根本没有文件a false bug report;))

    【讨论】:

    • 是的!这行得通!我通过以特定顺序同时重新定义两个注释处理器找到了一种解决方法(我认为首先是匕首,其次是 AutoValue),但这很笨拙。这是一致的并且有效。另外,我不知道为什么没有想到要求 AutoValue 更新他们的依赖关系。谢谢大家做适当的事情!
    猜你喜欢
    • 1970-01-01
    • 2012-02-25
    • 2017-09-08
    • 1970-01-01
    • 1970-01-01
    • 2019-02-12
    • 2010-10-07
    • 2019-04-16
    • 2018-10-18
    相关资源
    最近更新 更多