基于Gradle搭建Spring?5.3.13-release源碼閱讀環(huán)境的詳細(xì)流程
# 基于Gradle
搭建Spring 5.5.13-release
源碼閱讀環(huán)境
Spring
版本:5.3.13-release
# 1、安裝JDK
首先需要保證本地已經(jīng)安裝JDK1.8
及以上版本。這里不做過多贅述,自行安裝即可。
# 2、安裝Gradle
Spring 5.x
開始全部都采用Gradle
進(jìn)行編譯,構(gòu)建源碼前需要提前安裝Gradle
。Gradle
官網(wǎng)下載地址為:Gradle官網(wǎng)地址。- 我這里使用的版本是最新的
gradle-7.3.1
。下載鏈接為:Gradle-7.3.1下載地址。 - 下載完成之后解壓到文件夾。
- 配置
Gradle
環(huán)境變量,在環(huán)境變量的系統(tǒng)變量中添加如下:
$ GRADLE_HOME $ D:\develop\IDE-Gradle\gradle-7.3.1
在系統(tǒng)環(huán)境變量的path
中添加環(huán)境變量:
$ %GRADLE_HOME%\bin
檢測環(huán)境,使用gradle -v
命令在Terminal
中查看:
$ gradle -v
顯示如下圖則代表gradle
安裝成功。
在gradle
安裝目錄下的init.d
文件夾下新建一個init.gradle
文件,加入如下配置:
repositories
中配置獲取依賴jar
包的順序:- 先是從本地
maven
倉庫中獲取 - 然后
mavenLocal()
是獲取maven本地倉庫的路徑,和第一條一樣,但是不沖突 - 第三條第四條分別為國內(nèi)
alibaba
鏡像倉庫和國外bstek
鏡像倉庫 - 最后
mavenCentral()
是從apache提供的中央倉庫中獲取依賴jar
包
allprojects { repositories { maven { url 'file:///D:/develop/IDE-Repository'} mavenLocal() maven { name "Alibaba" ; url "https://maven.aliyun.com/repository/public" } maven { name "Bstek" ; url "https://nexus.bsdn.org/content/groups/public/" } mavenCentral() } buildscript { repositories { maven { url 'https://maven.aliyun.com/repository/google'} maven { url 'https://maven.aliyun.com/repository/jcenter'} maven { url 'https://maven.aliyun.com/nexus/content/groups/public'} } }
# 3、Spring版本命名規(guī)則
版本名稱 | 版本 | 版本意思 |
---|---|---|
snapshot | 快照版 | 尚不穩(wěn)定,處于開發(fā)中的版本 |
release | 穩(wěn)定版 | 功能相對穩(wěn)定的版本,可以對外發(fā)行,但是有時間限制 |
GA(General Availability) | 正式版 | 可廣泛使用的穩(wěn)定版本 |
M(Milestone) | 里程碑版 | 具有一些全新的功能貨時具有里程碑意義的版本 |
RC(Release Candidate) | 最終測試 | 即將作為正式版本發(fā)布的版本 |
# 4、下載Spring 5.3.13-release源碼
自Spring 3.x
開始,Spring
官方不在提供源碼下載,所有源代碼全部托管在github
。Spring Github托管代碼首頁地址。
spring framework 5.3.13-release
:
- 源碼訪問地址:
https://github.com/spring-projects/spring-framework/releases/tag/v5.3.13
- 源碼
zip
壓縮包下載地址:https://github.com/spring-projects/spring-framework/archive/refs/tags/v5.3.13.zip
- 源碼
tar.gz
壓縮包下載地址:https://github.com/spring-projects/spring-framework/archive/refs/tags/v5.3.13.tar.gz
- 國內(nèi)
gitee
托管源碼訪問地址:https://gitee.com/mirrors/Spring-Framework?_from=gitee_search
- 國內(nèi)
getee
源碼zip
壓縮包下載地址:https://gitee.com/mirrors/Spring-Framework/repository/archive/v5.3.13
下載完成之后解壓即可。
# 5、修改Spring源碼中Gradle配置
修改Spring
源碼的Gradle
構(gòu)建配置,在Spring
源碼下修改gradle/wrapper/gradle-wrapper.properties
文件如下:
修改distributionUrl
為本地的gradle
安裝包路徑,從本地拉去,加快源碼構(gòu)建編譯的速度。
distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists ## distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip ## 配置本地gradle, 配置之后需要配置gradle的中央倉庫(阿里云maven中央倉庫) distributionUrl=file:///D:/develop/IDE-Gradle/gradle-7.3.1-all.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists
設(shè)置阿里云鏡像:
修改根目錄下build.gradle
文件中的repositories
:
repositories { // 配置本地maven倉庫 mavenLocal() // 配置阿里云maven倉庫 maven { url "https://maven.aliyun.com/nexus/content/groups/public/" } maven { url "https://maven.aliyun.com/nexus/content/repositories/jcenter/" } // maven中央倉庫 mavenCentral() maven { url "https://repo.spring.io/libs-spring-framework-build" } }
修改根目錄下settings.gradle
文件中pluginManagement
下的repositories
:
pluginManagement { repositories { // 配置阿里云 maven 中央倉庫 maven { url 'https://maven.aliyun.com/repository/public/' } gradlePluginPortal() maven { url 'https://repo.spring.io/plugins-release/' } } }
修改根目錄下gradle.properties
文件,將jvmargs
根據(jù)自己本機(jī)內(nèi)存重新分配內(nèi)存大小,我這里分配了10G
的內(nèi)存:
version=5.3.13 org.gradle.jvmargs=-Xmx10240m org.gradle.caching=true org.gradle.parallel=true kotlin.stdlib.default.dependency=false
# 6、構(gòu)建Spring源碼
使用Terminal
進(jìn)入解壓后Spring
源碼所在的文件目錄下,預(yù)編譯spring-oxm
模塊:
$ ./gradlew :spring-oxm:compileTestJava
如某個jar
包沒下載成功等,只需要重新執(zhí)行./gradlew :spring-oxm:compileTestJava
再次進(jìn)行預(yù)編譯就行了。
構(gòu)建完成之后修改根目錄下setting.gradle
文件,注釋掉spring-aspects
:
.... include "spring-aop" // 移除aspects // include "spring-aspects" include "spring-beans" include "spring-context" ....
# 7、導(dǎo)入IDEA 點擊File --> New --> Project from Existing Sources...
然后選擇Spring
源碼所在的目錄
點擊ok
后選擇使用gradle
導(dǎo)入然后點擊Finish
導(dǎo)入之后IDEA
會自動進(jìn)行構(gòu)建,終止自動構(gòu)建,此時還需要進(jìn)行一些設(shè)置,Ctrl+Alt+S
快捷鍵打開IDEA
設(shè)置。
點擊File | Settings | Build, Execution, Deployment | Compiler
,修改構(gòu)建時堆內(nèi)存大?。ǜ鶕?jù)自己的電腦內(nèi)存自行分配即可,我這里里是分配了5G
):
點擊File | Settings | Build, Execution, Deployment | Build Tools | Gradle
,配置IDEA
的Gradle
配置:
Ctrl+Alt+Shift+S
快捷鍵打開Project Structure
。修改工程的SDK
,分別將Project
、Modules
、SDKs
中的JDK
設(shè)置為本地安裝的JDK
(版本為1.8及以上)。
Ctrl+Alt+Shift+S
快捷鍵打開Project Structure
。在Modules
中排除spring-aspects
:
Alt+F12
快捷鍵打開Terminal
,使用gradlew.bat
命令進(jìn)行編譯
$ gradlew.bat
編譯成功后會出現(xiàn)BUILD SUCCESSFUL
的提示。如果有報錯根據(jù)報錯信息進(jìn)行處理,多編譯幾次即可。
編譯成功后整個工程如下圖所示:
使用shift+shift
快捷鍵輸入ApplicationContext
類,使用Ctrl+Shift+Alt+U
如果能夠成功打開類圖,也證明Spring
源碼構(gòu)建成功:
# 8、創(chuàng)建Spring源碼debug調(diào)試模塊
使用gradle
創(chuàng)建一新的debug
模塊。
創(chuàng)建完成之后將新建模塊的build.gradle
修改為:新建的模塊名稱。如我新建的模塊叫spring-context-debug
,則將其修改為:spring-context-debug.gradle
并在其中配置:
plugins { id 'java' } group 'org.springframework' version '5.3.13' repositories { // 配置本地 maven 倉庫 mavenLocal() // 配置阿里云 maven 倉庫 maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' } maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter' } // 配置 maven 中央倉庫 mavenCentral() dependencies { // 測試需要依賴 testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0' // 導(dǎo)入 spring-context模塊, 包含bean工廠 implementation(project(':spring-context')) // 導(dǎo)入 spring-instrument 模塊, 此模塊為 spring-context 模塊編譯所必須的 implementation(project('::spring-instrument')) // compile已經(jīng)被gradle 7.x 棄用, 爺也是醉了 // compile(project(":spring-context")) test { useJUnitPlatform()
為新建模塊添加spring-context
和spring-instrument
依賴。配置完成之后刷新gradle
。如下圖所示,gradle
中新建模塊的runtimeClasspath
中已經(jīng)新增project spring-conetxt
和project spring-instrument
代表以來成功。
創(chuàng)建一個TestBean
實現(xiàn)DebugBean
DebugBean.java
:
package com.kapcb.ccc.model; /** * <a>Title: Bean </a> * <a>Author: Kapcb <a> * <a>Description: Bean <a> * * @author Kapcb * @version 1.0 * @date 2021/12/11 23:21 * @since 1.0 */ public interface DebugBean { /** * say method */ void say(); }
TestBean.java
:
package com.kapcb.ccc.model; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * <a>Title: TestBean </a> * <a>Author: Kapcb <a> * <a>Description: TestBean <a> * * @author Kapcb * @version 1.0 * @date 2021/12/11 23:21 * @since 1.0 */ public class TestBean implements DebugBean { protected final Log log = LogFactory.getLog(getClass()); private String username; public TestBean() { log.info("調(diào)用TestBean無參構(gòu)造器"); } @Override public void say() { log.info("Hi, I'm " + this.username); public void setUsername(String username) { log.info("調(diào)用TestBean中的setUsername方法注入屬性"); this.username = username; }
添加測試代碼,使用注解模式聲明一個Bean
:
package com.kapcb.ccc.configuration; import com.kapcb.ccc.model.DebugBean; import com.kapcb.ccc.model.TestBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope; /** * <a>Title: AnnotationTestConfiguration </a> * <a>Author: Kapcb <a> * <a>Description: AnnotationTestConfiguration <a> * * @author Kapcb * @date 2021/12/15 16:06 */ @Configuration public class AnnotationTestConfiguration { @Bean("testBean") @Scope("singleton") public DebugBean debugBean() { TestBean testBean = new TestBean(); testBean.setUsername("Kapcb(Annotation)"); return testBean; } }
添加測試代碼:
package com.kapcb.ccc; import com.kapcb.ccc.configuration.AnnotationTestConfiguration; import com.kapcb.ccc.model.DebugBean; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /** * <a>Title: AnnotationApplication </a> * <a>Author: Kapcb <a> * <a>Description: AnnotationApplication <a> * * @author Kapcb * @date 2021/12/15 16:05 */ public class AnnotationApplication { public static void main(String[] args) { BeanFactory beanFactory = new AnnotationConfigApplicationContext(AnnotationTestConfiguration.class); DebugBean testBean = beanFactory.getBean("testBean", DebugBean.class); testBean.say(); } }
使用debug
模式運(yùn)行,如果debug
模式啟動main
方法報錯,嘗試修改:File | Settings | Build, Execution, Deployment | Debugger | Data Views | Kotlin
在Disable coroutine agent前勾選后保存。
啟動成功后控制臺會輸出如下日志:
Connected to the target VM, address: '127.0.0.1:55594', transport: 'socket'
一月 06, 2022 2:56:42 下午 com.kapcb.ccc.model.TestBean <init>
信息: 調(diào)用TestBean無參構(gòu)造器
一月 06, 2022 2:56:42 下午 com.kapcb.ccc.model.TestBean setUsername
信息: 調(diào)用TestBean中的setUsername方法注入屬性
一月 06, 2022 2:56:42 下午 com.kapcb.ccc.model.TestBean say
信息: Hi, I'm Kapcb(Annotation)
Disconnected from the target VM, address: '127.0.0.1:55594', transport: 'socket'
GitHub源碼地址:https://github.com/kapbc/kapcb-spring-source/tree/master/Spring-Framework-v5.3.13
到此這篇關(guān)于基于Gradle搭建Spring 5.3.13-release源碼閱讀環(huán)境的文章就介紹到這了,更多相關(guān)Gradle搭建Spring源碼閱讀環(huán)境內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
推薦一款I(lǐng)ntelliJ IDEA提示快捷鍵的Key Promoter X插件
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA提示快捷鍵的Key Promoter X插件,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10SpringBoot+RabbitMQ+Redis實現(xiàn)商品秒殺的示例代碼
本文主要介紹了SpringBoot+RabbitMQ+Redis實現(xiàn)商品秒殺,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11Java數(shù)據(jù)類型之細(xì)講char類型與編碼關(guān)系
這幾天一直在復(fù)習(xí)Java基礎(chǔ)知識,特地寫了一篇文章來做一下筆記,文中有非常詳細(xì)的圖文示例,對正在學(xué)習(xí)java的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05java如何實現(xiàn)基于opencv全景圖合成實例代碼
全景圖相信大家應(yīng)該都不陌生,下面這篇文章主要給大家介紹了關(guān)于java如何實現(xiàn)基于opencv全景圖合成的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07詳解在Spring3中使用注解(@Scheduled)創(chuàng)建計劃任務(wù)
本篇文章主要介紹了詳解在Spring3中使用注解(@Scheduled)創(chuàng)建計劃任務(wù),具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03