将 AspectJ 1.8.1 升级到 1.9.8 并与 Gradle 7 + Java 11 集成失败 无法找到 Aspectjrt.jar

Upgrading AspectJ 1.8.1 to 1.9.8 and Integrate with Gradle 7 + Java 11 Fail To locate Aspectjrt.jar

提问人:Edward Sun 提问时间:11/17/2023 最后编辑:kriegaexEdward Sun 更新时间:11/18/2023 访问量:32

问:

我正在将基于 Java 8 的 spring 代码库(gradle 项目)升级到 Java 11。 该项目确实包含 AOP 代码 (https://en.wikipedia.org/wiki/Aspect-oriented_programming),并且依赖于这些依赖项

org.springframework:spring-aspects:4.3.27.RELEASE
org.aspectj:aspectjtools:1.8.1
org.aspectj:aspectjrt:1.8.1

升级到 1.9.8 后的上述依赖项:

dependencies {
    ajc 'org.aspectj:aspectjtools:1.9.8'
    implementation 'javax.persistence:persistence-api:1.0'
    implementation 'org.aspectj:aspectjtools:1.9.8'
    implementation 'org.aspectj:aspectjrt:1.9.8'
    aspects 'javax.persistence:persistence-api:1.0'

    aspects "org.springframework:spring-aspects:${springVersion}"
    aspects "org.springframework:spring-tx:${springVersion}"
    aspects "org.springframework:spring-orm:${springVersion}"

根据以下文档,AspectJ 1.8.x 与 java 8+ 不兼容: https://github.com/eclipse-aspectj/aspectj/blob/master/docs/dist/doc/JavaVersionCompatibility.md

Java 17 是我们计划最终升级到的。 关于 AspectJ 1.9.8 及其支持的官方文档:https://htmlpreview.github.io/?https://github.com/kriegaex/org.aspectj/blob/4b9d86acd096e5ee9e108ff0a450c420c880b6ea/docs/dist/doc/README-198.html

将 gradle 5 成功升级到 gradle 7 后,尝试运行失败,并出现以下错误:gradlew clean build

> Task :compileJava
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: /Users/someDir/someService/src/main/java/someCode.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

[ant:iajc] [warning] couldn't find aspectjrt.jar on classpath, checked: /Library/Java/JavaVirtualMachines/jdk-11.0.14.jdk/Contents/Home/lib/jrt-fs.jar:/Users/user/.gradle/caches/modules-2/files-2.1/javax.persistence/persistence-api/1.0/5725f57873e05e068803e2bf9d5a8ea3740ffec5/persistence-api-1.0.jar:/Users/user/.gradle/caches/modules-2/files-2.1/org.springframework/spring-aspects/5.3.19/a60d729e20daa34220b63be28d1b3cd20c1128e8/spring-aspects-5.3.19.jar:/Users/user/.gradle/caches/modules-2/files-2.1/org.springframework/spring-tx/5.3.19/f0b442718e67000845e8662ea10f138d7c47a3ae/spring-tx-5.3.19.jar:/Users/user/.gradle/caches/modules-2/files-2.1/org.springframework/spring-orm/5.3.19/963b297aed7a1f9252cf4d925e6178190448ec81/spring-orm-5.3.19.jar:/Users/user/.gradle/caches/modules-2/files-2.1/javax.persistence/persistence-api/1.0/5725f57873e05e068803e2bf9d5a8ea3740ffec5/persistence-api-1.0.jar:/Users/user/.gradle/caches/modules-2/files-2.1/org.springframework/spring-aspects/5.3.19/a60d729e20daa34220b63be28d1b3cd20c1128e8/spring-aspects-5.3.19.jar:/Users/user/.gradle/caches/modules-2/files-2.1/org.springframework/spring-tx/5.3.19/f0b442718e67000845e8662ea10f138d7c47a3ae/spring-tx-5.3.19.jar:/Users/user/.gradle/caches/modules-2/files-2.1/org.springframework/spring-orm/5.3.19/963b297aed7a1f9252cf4d925e6178190448ec81/spring-orm-5.3.19.jar
[ant:iajc]
[ant:iajc] [error] can't determine implemented interfaces of missing type org.springframework.beans.factory.DisposableBean
[ant:iajc] when batch building BuildConfig[null] #Files=180 AopXmls=#0
[ant:iajc]  [Xlint:cantFindType]
[ant:iajc]
[ant:iajc] [error] can't determine implemented interfaces of missing type org.springframework.beans.factory.BeanFactoryAware
[ant:iajc] when batch building BuildConfig[null] #Files=180 AopXmls=#0
[ant:iajc]  [Xlint:cantFindType]
[ant:iajc]
[ant:iajc] [error] can't determine implemented interfaces of missing type org.springframework.beans.factory.InitializingBean
[ant:iajc] when batch building BuildConfig[null] #Files=180 AopXmls=#0
[ant:iajc]  [Xlint:cantFindType]
[ant:iajc]
[ant:iajc] [error] can't determine superclass of missing type org.springframework.aop.interceptor.AsyncExecutionAspectSupport
[ant:iajc] when batch building BuildConfig[null] #Files=180 AopXmls=#0
[ant:iajc]  [Xlint:cantFindType]
[ant:iajc]
[ant:iajc] [error] can't determine superclass of missing type org.springframework.cache.interceptor.CacheAspectSupport
[ant:iajc] when batch building BuildConfig[null] #Files=180 AopXmls=#0
[ant:iajc]  [Xlint:cantFindType]
[ant:iajc]
[ant:iajc] [error] can't determine implemented interfaces of missing type org.springframework.beans.factory.DisposableBean
[ant:iajc] when batch building BuildConfig[null] #Files=180 AopXmls=#0
[ant:iajc]  [Xlint:cantFindType]
[ant:iajc]
[ant:iajc] [error] can't determine implemented interfaces of missing type org.springframework.beans.factory.BeanFactoryAware
[ant:iajc] when batch building BuildConfig[null] #Files=180 AopXmls=#0
[ant:iajc]  [Xlint:cantFindType]
[ant:iajc]
[ant:iajc] [error] can't determine implemented interfaces of missing type org.springframework.beans.factory.InitializingBean
[ant:iajc] when batch building BuildConfig[null] #Files=180 AopXmls=#0
[ant:iajc]  [Xlint:cantFindType]
[ant:iajc]
[ant:iajc] [error] can't determine implemented interfaces of missing type org.springframework.beans.factory.DisposableBean
[ant:iajc] when batch building BuildConfig[null] #Files=180 AopXmls=#0
[ant:iajc]  [Xlint:cantFindType]
[ant:iajc]
[ant:iajc] [error] classpath error: unable to find org.aspectj.lang.JoinPoint (check that aspectjrt.jar is in your classpath)
[ant:iajc]
[ant:iajc]
[ant:iajc] 10 errors, 1 warning

我已经仔细检查并验证了我的主目录中是否存在:aspectjrt.jar.gradle

/Users/user/.gradle/caches/modules-2/files-2.1/org.aspectj/aspectjrt/1.9.8/7a5d13787d5268d415055eba51024fb7348fc533/aspectjrt-1.9.8.jar

但是,由于某种原因,我现有的文件似乎没有检查此目录......(我怀疑这是这里的根本原因)build.gradle

我目前的build.gradle设置如下:

configurations.all {
    transitive = false
}

compileJava {
    doLast {
        ant.taskdef( resource:"org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties", classpath: configurations.ajc.asPath)
        ant.iajc(
                source:"1.11",
                target:"1.11",
                destDir:sourceSets.main.output.classesDirs.getAsPath(),
                maxmem:"512m", fork:"true",
                aspectPath:configurations.aspects.asPath,
                sourceRootCopyFilter:"**/*.java",
                classpath:"${configurations.compile.asPath}") {
                    sourceroots {
                        sourceSets.main.java.srcDirs.each {
                            srcDir ->
                            pathelement(location:srcDir.absolutePath)
                        }
                    }
        }
    }
}


configurations{
    ajc
    aspects
    aspectCompile
    compile {
        extendsFrom aspects
    }
}
....

task sourcesJar(type: Jar) {
    from sourceSets.main.allSource
    classifier = 'sources'
}

我已经在官方文档上检查了有关 AjcTask(iajc) 安装的所有详细信息,不幸的是,这些步骤都是为 maven 项目编写的: https://eclipse.dev/aspectj/doc/next/devguide/printable.html#antTasks-install

如果有人碰巧知道解决此问题的链接/指南,将非常感谢您的分享!

java gradle ant build.gradle aspectj

评论

0赞 kriegaex 11/23/2023
“不幸的是,这些步骤都是为 maven 项目编写的” - 更正:不,它们是为 Ant 项目编写的。

答:

0赞 kriegaex 11/18/2023 #1

我已经仔细检查并验证了我的主目录中是否存在aspectjrt.jar.gradle

嗯,这可能是真的,但显然它不在 Ant 任务的类路径上(添加了换行符以限制水平滚动):

[ant:iajc] [warning] couldn't find aspectjrt.jar on classpath, checked
  /Library/Java/JavaVirtualMachines/jdk-11.0.14.jdk/Contents/Home/lib/jrt-fs.jar
  /Users/user/.gradle/caches/modules-2/files-2.1/javax.persistence/persistence-api/1.0/5725f57873e05e068803e2bf9d5a8ea3740ffec5/persistence-api-1.0.jar
  /Users/user/.gradle/caches/modules-2/files-2.1/org.springframework/spring-aspects/5.3.19/a60d729e20daa34220b63be28d1b3cd20c1128e8/spring-aspects-5.3.19.jar
  /Users/user/.gradle/caches/modules-2/files-2.1/org.springframework/spring-tx/5.3.19/f0b442718e67000845e8662ea10f138d7c47a3ae/spring-tx-5.3.19.jar
  /Users/user/.gradle/caches/modules-2/files-2.1/org.springframework/spring-orm/5.3.19/963b297aed7a1f9252cf4d925e6178190448ec81/spring-orm-5.3.19.jar
  /Users/user/.gradle/caches/modules-2/files-2.1/javax.persistence/persistence-api/1.0/5725f57873e05e068803e2bf9d5a8ea3740ffec5/persistence-api-1.0.jar
  /Users/user/.gradle/caches/modules-2/files-2.1/org.springframework/spring-aspects/5.3.19/a60d729e20daa34220b63be28d1b3cd20c1128e8/spring-aspects-5.3.19.jar
  /Users/user/.gradle/caches/modules-2/files-2.1/org.springframework/spring-tx/5.3.19/f0b442718e67000845e8662ea10f138d7c47a3ae/spring-tx-5.3.19.jar
  /Users/user/.gradle/caches/modules-2/files-2.1/org.springframework/spring-orm/5.3.19/963b297aed7a1f9252cf4d925e6178190448ec81/spring-orm-5.3.19.jar

解决这个问题,你应该没事的。

顺便说一句,为什么你使用 Gradle 的古老 Ant 任务而不是 Freefair 之类的东西?

评论

0赞 Edward Sun 11/21/2023
首先感谢您的钟声。引用,是的......这正是我试图解决的问题,还没有运气找到有用的指南...... 井。。。简短的回答是,如果 Java 8 还活着,我们永远不会碰它,这是一个古老的服务(建于 6 年多前)。顺便说一句,Freefair 是升级到 AOP 代码库的 Java 11 的必备条件吗?but obviously it is not on the Ant task's classpathBTW, why are you using an ancient Ant task from Gradle and not something like Freefair?
0赞 kriegaex 11/21/2023
不,这只是使用 Gradle 的 AspectJ 编译器的一种更简单的方法。我对 AspectJ + Maven 了如指掌,但从未使用过 Gradle。但是我过去使用 Freefair 来回答 AspectJ + Gradle 相关问题,而且它比现在少得多,而且使用起来非常容易。此外,我也不是 Ant 用户,当然以前从未使用过 Gradle 的 Ant。因此,如果 GitHub 上没有一个完整的、最小的复制者,我无法帮助你。
0赞 kriegaex 11/23/2023
我想更好地帮助你,但我需要你在 GitHub 上提供一个 MCVE 项目——不是你的原始代码,只是一个最小的复制者。我不会从头开始创建一个。