源碼版本
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。