使用 IntelliJ IDEA 构建 Spring Framework 5.3.21 源码问题解决

源码版本

1、下载地址:https://github.com/spring-projects/spring-framework/tags
2、选择要构建的源码版本并下载,例如:5.3.21

相关环境

1、操作系统:Windows10
2、JDK 版本:Jdk17
3、IDE 工具:IntelliJ IDEA 2021.3.3
4、项目构建工具:Gradle 7.3.3

使用 IntelliJ IDEA 构建 Spring 源码步骤

1、打开 cmd 命令行工具,进入当前项目根目录,然后使用命令 gradlew.bat :spring-oxm:compileTestJava 预编译 spring-oxm 模块
2、将项目导入 IntelliJ IDEA,步骤为 File -> New -> Project from Existing Sources -> 选择项目根路径下的 build.gradle 导入

修改 jdk 版本(好像不改也行,但需要保证 java -version 命令的结果是 java 17.xxx)

1、修改 spring-oxm 模块下的 spring-oxm.gradle 配置文件:
  将 JavaVersion.VERSION_1_8 改为 JavaVersion.VERSION_17

2、修改 gradle 模块下的  toolchains.gradle 配置文件:
  将 JavaLanguageVersion.of(8) 改为 JavaLanguageVersion.of(17)。
  将 sourceCompatibility = JavaVersion.VERSION_1_8 改为 sourceCompatibility = JavaVersion.VERSION_17。
  将 jvmTarget = '1.8' 改为 jvmTarget = '17'。

遇到的问题及解决方法

预编译 spring-oxm (Windows 环境下在项目根路径执行命令 gradlew.bat :spring-oxm:compileTestJava)模块报错:org.gradle.process.internal.ExecException: Process 'command 'git'' finished with non-zero exit value 128

多半是因为你的项目源码是通过下载的方式获得,而非通过 git clone 命令克隆而来,可以不解决。
如果有强迫症,非要解决,建议通过 git clone 命令去克隆项目源码,然后重新编译。

缺少 spring-cglib-repack-xxx.jar 和 spring-objenesis-repack-xxx.jar 依赖

解决方案:
【方法一】在源码项目根路径下执行:gradle objenesisRepackJar、gradle cglibRepackJar。
【方法二】在 IntelliJ IDEA 的侧边工具打开 gradle,分别双击 spring-core -> Tasks  -> other 下的 objenesisRepackJar 和 cglibRepackJar。
以上两种方法均会在项目的 spring-core\build\libs 目录下生成所需 jar 包。

在运行某些测试类时,IntelliJ IDEA 报错:Command line is too long …… 的解决办法

解决方案:
找到当前项目 .idea\workspace.xml 文件中的 <component name="PropertiesComponent">,并在其中加一行 <property name="dynamic.classpath" value="true"/>,然后重新运行。

运行时报错:No tests found for given includes: [**/Tests.class, **/Test.class]......

报错详情:
Execution failed for task ':模块名称:test'.
> No tests found for given includes: [**/*Tests.class, **/*Test.class](include rules) [......](--tests filter)

解决方案:
配置测试运行器为 IntelliJ IDEA 即可。IDEA 中的具体步骤:
1、进入 Gradle 设置页面:File -> Settings -> Build,Execution,Deployment -> Build Tools -> Gradle
2、修改:将 Gradle Projects 面板中的 Run tests using 选项值改为 IntelliJ IDEA

【说明】在 Run tests using 选项列表中,为 Gradle 项目指定测试运行器,区别如下:
1、Gradle:选择此选项将使用 Gradle 作为测试运行器。结果是在持续集成(CI)服务器上获得相同的测试结果,在命令行中运行的测试将始终在 IDE 中运行。
2、IntelliJ IDEA:选择此选项可将测试过程委派给 IntelliJ IDEA。在这种情况下,IntelliJ IDEA 使用 JUnit 测试运行器,并且由于增量编译,测试运行得更快。
3、Choose pre test:选择此选项可配置每个测试专门使用哪个测试运行器(Gradle 或 IntelliJ IDEA)。

出现 AutowiredAnnotationBeanPostProcessor.java:542: 错误: 对 determineRequiredStatus 的引用不明确

报错详情:
spring-beans\src\main\java\org\springframework\beans\factory\annotation\AutowiredAnnotationBeanPostProcessor.java:542: 错误: 对determineRequiredStatus的引用不明确
return determineRequiredStatus(
AutowiredAnnotationBeanPostProcessor 中的方法 determineRequiredStatus(MergedAnnotation<?>) 和 AutowiredAnnotationBeanPostProcessor 中的方法 determineRequiredStatus(AnnotationAttributes) 都匹配
  
解决方案:
将 return determineRequiredStatus(ann.asMap(mergedAnnotation -> new AnnotationAttributes(mergedAnnotation.getType()))); 
更改为 return determineRequiredStatus(ann.<AnnotationAttributes> asMap(mergedAnnotation -> new AnnotationAttributes(mergedAnnotation.getType())));

出现 CoroutinesUtils.java:74: 警告: [deprecation] AccessibleObject中的isAccessible()已过时

报错详情:
spring-core\src\main\java\org\springframework\core\CoroutinesUtils.java:74: 警告: [deprecation] AccessibleObject中的isAccessible()已过时
if (method.isAccessible() && !KCallablesJvm.isAccessible(function)) {

解决方案:
在 org.springframework.core.CoroutinesUtils.invokeSuspendingFunction(Method method, Object target, Object... args) 方法上加 @SuppressWarnings("deprecation") 注解即可。 

编译过程中出现与 '-Werror' 有关的错误及其解决方案

原因:-Werror 的作用是将全部的 warning 当成 error。

解决方案:全局搜索 -Werror 关键字,并注释该选项。

使用高版本 jdk 时,出现:module java.base does not "opens java.lang" to unnamed module...... 错误

参考 https://blog.csdn.net/qq_27525611/article/details/108685030
原因:由 jdk9 及以上版本中引入的 Java Platform Module System 导致。

解决方案1【降级 jdk 版本】:降级至 jdk8 即可。
解决方案2【添加 VM 参数】:--add-opens java.base/java.lang=ALL-UNNAMED。

出现工程某些类找不到的错误

原因:大部分情况都是在 xxx.gradle 文件中使用了 optional 选项引入的依赖。

解决方案:参考其他依赖引入方式,比如将 optional 改为 api。
posted @ 2022-04-01 12:39  本宫在,尔等都是妃  Views(1996)  Comments(0Edit  收藏  举报