AspectJ 找不到 .raw 超类

AspectJ cannot find .raw superclass

背景

一个项目使用 Aspects 进行日志记录。详情:

build.gradle 文件类似于:

apply plugin: "io.freefair.aspectj.post-compile-weaving"

dependencies {
    compileOnly group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1'
    compileOnly group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.12.1'
    compileOnly group: 'org.osgi', name: 'org.osgi.framework', version: '1.9.0'
    compileOnly group: 'org.mockito', name: 'mockito-core', version: '2.25.1'

    inpath project(":company.project.main")
}

问题

编译应用程序时,AspectJ找不到MockMethodDispatcher,报错:

.../mockito-core-2.25.1.jar [error] can't determine superclass of missing type org.mockito.internal.creation.bytebuddy.inject.MockMethodDispatcher
when weaving type org.mockito.internal.creation.bytebuddy.MockMethodAdvice
when weaving classes 
when weaving 
when batch building BuildConfig[null] #Files=0 AopXmls=#0
 [Xlint:cantFindType]
(no source information available)
        [Xlint:cantFindType]
.../org.mockito/mockito-core/2.25.1/e8fa2864b65c0b6fbb20daa436a94853bcd17e5e/mockito-core-2.25.1.jar [warning] can't find type org.mockito.internal.creation.bytebuddy.inject.MockMethodDispatcher whilst determining signatures of call or execution join point for java.util.concurrent.Callable org.mockito.internal.creation.bytebuddy.MockMethodAdvice.handle(java.lang.Object, java.lang.reflect.Method, java.lang.Object[]), this may cause a pointcut to fail to match at this join point 
when weaving type org.mockito.internal.creation.bytebuddy.MockMethodAdvice
when weaving classes 
when weaving 
when batch building BuildConfig[null] #Files=0 AopXmls=#0
 [Xlint:cantFindTypeAffectingJPMatch]

我怀疑这是因为文件存储为 .raw 文件而不是 .class 文件(根据 issue 845):

1778 Mon Jul 08 13:47:02 PDT 2019 org/mockito/internal/creation/bytebuddy/inject/MockMethodDispatcher.raw

问题

您将如何更新 Gradle 文件以指示 post-compile-weaving 插件完全忽略编织(或扫描)Mockito classes?

注释

在命令行中,编织似乎有效:

java -cp aspectjtools-1.9.4.jar:aspectjrt-1.9.4.jar org.aspectj.tools.ajc.Main \
-inpath application.jar \
-aspectpath ../aspects/build/classes/java/main \
-Xlint:warning \
-verbose \
-showWeaveInfo \
-log aop.log \
-outjar woven.jar

尽管 woven.jar 中的输出 classes 应该注入 application.jar.

附录

注:

相关

尝试次数

调用compileJava时如下:

compileJava {
    ajc {
        enabled = true
        classpath = configurations.aspectj
        options {
            aspectpath = configurations.aspect
            compilerArgs = [""]
        }
    }
}

Gradle报如下错误:

Cannot set the value of read-only property 'classpath' for object of type io.freefair.gradle.plugins.aspectj.AjcAction.

使用:

compileJava {
  ajc {
    options {
      compilerArgs = [""]
    }
  }
}

Gradle 报告:

Could not find method options() for arguments [...] on object of type io.freefair.gradle.plugins.aspectj.AjcAction.

master 上的 source code 似乎为其 "configurable things" 暴露了不同的名称:

task.getInputs().property("ajcArgs", this.getOptions().getCompilerArgs())

声明的依赖是可传递的:

inpath project(":company.project.main")

这会将 :company.project.main 的完整运行时类路径(由所述项目 生成的 类 所有依赖项)传递到 -inpath 的 ajc。 (查看 build/tmp/compileJava/ajc.options 文件进行确认。)

为避免将建议编织到外部 类,声明一个 non-transitive dependency 如下:

inpath(project(":company.project.main")) { 
    transitive = false
}

根据您的具体要求和项目结构,将 io.freefair.aspectj.post-compile-weaving 插件直接应用于 :company.project.main 项目可能是更好的方法。

补充上述答案(对我帮助很大,谢谢!),您还可以将此策略应用于存在于外部依赖项中的传递依赖项,例如:

    inpath('org.apache.kafka:kafka-streams:3.1.1') {
        transitive = false
    }