基于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及以上版本。這里不做過(guò)多贅述,自行安裝即可。
# 2、安裝Gradle
Spring 5.x開(kāi)始全部都采用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

檢測(cè)環(huán)境,使用gradle -v命令在Terminal中查看:
$ gradle -v
顯示如下圖則代表gradle安裝成功。

在gradle安裝目錄下的init.d文件夾下新建一個(gè)init.gradle文件,加入如下配置:
repositories中配置獲取依賴(lài)jar包的順序:- 先是從本地
maven倉(cāng)庫(kù)中獲取 - 然后
mavenLocal()是獲取maven本地倉(cāng)庫(kù)的路徑,和第一條一樣,但是不沖突 - 第三條第四條分別為國(guó)內(nèi)
alibaba鏡像倉(cāng)庫(kù)和國(guó)外bstek鏡像倉(cāng)庫(kù) - 最后
mavenCentral()是從apache提供的中央倉(cāng)庫(kù)中獲取依賴(lài)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ī)則
| 版本名稱(chēng) | 版本 | 版本意思 |
|---|---|---|
| snapshot | 快照版 | 尚不穩(wěn)定,處于開(kāi)發(fā)中的版本 |
| release | 穩(wěn)定版 | 功能相對(duì)穩(wěn)定的版本,可以對(duì)外發(fā)行,但是有時(shí)間限制 |
| GA(General Availability) | 正式版 | 可廣泛使用的穩(wěn)定版本 |
| M(Milestone) | 里程碑版 | 具有一些全新的功能貨時(shí)具有里程碑意義的版本 |
| RC(Release Candidate) | 最終測(cè)試 | 即將作為正式版本發(fā)布的版本 |
# 4、下載Spring 5.3.13-release源碼
自Spring 3.x開(kāi)始,Spring官方不在提供源碼下載,所有源代碼全部托管在github。Spring Github托管代碼首頁(yè)地址。
spring framework 5.3.13-release:
- 源碼訪(fǎng)問(wèn)地址:
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 - 國(guó)內(nèi)
gitee托管源碼訪(fǎng)問(wèn)地址:https://gitee.com/mirrors/Spring-Framework?_from=gitee_search - 國(guó)內(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的中央倉(cāng)庫(kù)(阿里云maven中央倉(cāng)庫(kù)) 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倉(cāng)庫(kù)
mavenLocal()
// 配置阿里云maven倉(cāng)庫(kù)
maven { url "https://maven.aliyun.com/nexus/content/groups/public/" }
maven { url "https://maven.aliyun.com/nexus/content/repositories/jcenter/" }
// maven中央倉(cāng)庫(kù)
mavenCentral()
maven { url "https://repo.spring.io/libs-spring-framework-build" }
}修改根目錄下settings.gradle文件中pluginManagement下的repositories:
pluginManagement {
repositories {
// 配置阿里云 maven 中央倉(cāng)庫(kù)
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
如某個(gè)jar包沒(méi)下載成功等,只需要重新執(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 點(diǎn)擊File --> New --> Project from Existing Sources...
然后選擇Spring源碼所在的目錄

點(diǎn)擊ok后選擇使用gradle導(dǎo)入然后點(diǎn)擊Finish
導(dǎo)入之后IDEA會(huì)自動(dòng)進(jìn)行構(gòu)建,終止自動(dòng)構(gòu)建,此時(shí)還需要進(jìn)行一些設(shè)置,Ctrl+Alt+S快捷鍵打開(kāi)IDEA設(shè)置。
點(diǎn)擊File | Settings | Build, Execution, Deployment | Compiler,修改構(gòu)建時(shí)堆內(nèi)存大?。ǜ鶕?jù)自己的電腦內(nèi)存自行分配即可,我這里里是分配了5G):

點(diǎn)擊File | Settings | Build, Execution, Deployment | Build Tools | Gradle,配置IDEA的Gradle配置:

Ctrl+Alt+Shift+S快捷鍵打開(kāi)Project Structure。修改工程的SDK,分別將Project、Modules、SDKs中的JDK設(shè)置為本地安裝的JDK(版本為1.8及以上)。
Ctrl+Alt+Shift+S快捷鍵打開(kāi)Project Structure。在Modules中排除spring-aspects:

Alt+F12快捷鍵打開(kāi)Terminal,使用gradlew.bat命令進(jìn)行編譯
$ gradlew.bat
編譯成功后會(huì)出現(xiàn)BUILD SUCCESSFUL的提示。如果有報(bào)錯(cuò)根據(jù)報(bào)錯(cuò)信息進(jìn)行處理,多編譯幾次即可。
編譯成功后整個(gè)工程如下圖所示:

使用shift+shift快捷鍵輸入ApplicationContext類(lèi),使用Ctrl+Shift+Alt+U如果能夠成功打開(kāi)類(lèi)圖,也證明Spring源碼構(gòu)建成功:

# 8、創(chuàng)建Spring源碼debug調(diào)試模塊
使用gradle創(chuàng)建一新的debug模塊。
創(chuàng)建完成之后將新建模塊的build.gradle修改為:新建的模塊名稱(chēng)。如我新建的模塊叫spring-context-debug,則將其修改為:spring-context-debug.gradle并在其中配置:
plugins {
id 'java'
}
group 'org.springframework'
version '5.3.13'
repositories {
// 配置本地 maven 倉(cāng)庫(kù)
mavenLocal()
// 配置阿里云 maven 倉(cāng)庫(kù)
maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter' }
// 配置 maven 中央倉(cāng)庫(kù)
mavenCentral()
dependencies {
// 測(cè)試需要依賴(lài)
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工廠(chǎng)
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依賴(lài)。配置完成之后刷新gradle。如下圖所示,gradle中新建模塊的runtimeClasspath中已經(jīng)新增project spring-conetxt和project spring-instrument代表以來(lái)成功。

創(chuàng)建一個(gè)TestBean實(shí)現(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無(wú)參構(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;
}添加測(cè)試代碼,使用注解模式聲明一個(gè)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;
}
}添加測(cè)試代碼:
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模式啟動(dòng)main方法報(bào)錯(cuò),嘗試修改:File | Settings | Build, Execution, Deployment | Debugger | Data Views | Kotlin在Disable coroutine agent前勾選后保存。
啟動(dòng)成功后控制臺(tái)會(huì)輸出如下日志:
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無(wú)參構(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
推薦一款I(lǐng)ntelliJ IDEA提示快捷鍵的Key Promoter X插件
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA提示快捷鍵的Key Promoter X插件,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10
java獲取機(jī)器碼簡(jiǎn)單實(shí)現(xiàn)demo
這篇文章主要為大家介紹了java獲取機(jī)器碼的簡(jiǎn)單實(shí)現(xiàn)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
SpringBoot+RabbitMQ+Redis實(shí)現(xiàn)商品秒殺的示例代碼
本文主要介紹了SpringBoot+RabbitMQ+Redis實(shí)現(xiàn)商品秒殺,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
Java數(shù)據(jù)類(lèi)型之細(xì)講char類(lèi)型與編碼關(guān)系
這幾天一直在復(fù)習(xí)Java基礎(chǔ)知識(shí),特地寫(xiě)了一篇文章來(lái)做一下筆記,文中有非常詳細(xì)的圖文示例,對(duì)正在學(xué)習(xí)java的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05
java如何實(shí)現(xiàn)基于opencv全景圖合成實(shí)例代碼
全景圖相信大家應(yīng)該都不陌生,下面這篇文章主要給大家介紹了關(guān)于java如何實(shí)現(xiàn)基于opencv全景圖合成的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
Servlet從入門(mén)到精通(超級(jí)詳細(xì)!)
在JavaWeb項(xiàng)目中,處理請(qǐng)求和發(fā)送響應(yīng)的過(guò)程是由一種叫做Servlet 的程序來(lái)完成的,并且 Servlet 是為了解決實(shí)現(xiàn)動(dòng)態(tài)頁(yè)面而衍生的東西,下面這篇文章主要給大家介紹了關(guān)于Servlet從入門(mén)到精通的相關(guān)資料,需要的朋友可以參考下2022-03-03
詳解在Spring3中使用注解(@Scheduled)創(chuàng)建計(jì)劃任務(wù)
本篇文章主要介紹了詳解在Spring3中使用注解(@Scheduled)創(chuàng)建計(jì)劃任務(wù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03

