如何在 IntelliJ IDEA 中搭建和编译 Spring Framework 5.3.17 的源代码
最编程
2024-02-23 19:17:15
...
源码版本
1、下载地址:https://github.com/spring-projects/spring-framework/tags
2、选择要构建的源码版本并下载,例如:5.3.17
相关环境
1、操作系统:Windows10
2、JDK 版本:Jdk11
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 版本
1、修改 spring-oxm 模块下的 spring-oxm.gradle 配置文件:
将 JavaVersion.VERSION_1_8 改为 JavaVersion.VERSION_11
2、修改 gradle 模块下的 toolchains.gradle 配置文件:
将 JavaLanguageVersion.of(8) 改为 JavaLanguageVersion.of(11)。
将 sourceCompatibility = JavaVersion.VERSION_1_8 改为 sourceCompatibility = JavaVersion.VERSION_11。
将 jvmTarget = '1.8' 改为 jvmTarget = '11'。
遇到的问题及解决方法
缺少 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"/>,然后重新运行。
出现 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") 注解即可。
参考资料
- IDEA + Gradle构建 Spring5 源码阅读环境教程
- Spring 5.3.x 源码阅读环境搭建
- Spring 5.3.2 源码编译
- Spring 5.3.10 源码编译
原文地址:https://www.cnblogs.com/qubo520/p/16044387.html