1 Spring 5原始碼下載
首先你的JDK需要升級到1.8以上版本,從Spring 3.0開始,Spring原始碼采用GitHub托管,不再提供官網下載鏈接,這里不做過多贅述,大家可自行去GitHub網站下載,我使用的版本下載鏈接為 https://github.com/spring-projects/spring-framework/archive/v5.0.2.RELEASE.zip ,下載完成后,解壓原始碼包會看到如下圖所示的檔案目錄,

2 基于Gradle的原始碼構建技巧
由于從Spirng 5開始都采用Gradle編譯,所以構建原始碼前需要先安裝Gradle,Gradle下載地址為 https://gradle.org/releases ,我使用的是Spring 5官方推薦的版本Gradle 4.0,下載鏈接為https://gradle.org/next-steps/?version=4.0&format=bin ,下載完成后按以下步驟操作,這里以Windows作業系統為例,
第一步,配置環境變數,如下圖所示,

第二步,添加環境變數“%GRADLE_HOME%\bin”,如下圖所示,

第三步,檢測環境,輸入gradle -v命令,得到如下圖所示結果,
----------------------------------------------------------------------
Gradle 4.0
----------------------------------------------------------------------
Build time:2017-06-14 15:11:08 UTC
Revision: 316546a5fcb4e2dfe1d6aaüb73a4e09e8cecb5a5
Groovy:2. 4. 11
Ant:Apache Ant THD version 1. 9.6 compiled on June 29 2015
JVM:1.8.0_ 131 (Oracle Corporation 25.131-b11)
OS:Windows 10 10. 0 amd64
第四步,編譯原始碼,將cmd切到spring-framework-5.0.2.RELEASE目錄,運行gradlew.bat,如下圖所示,
Starting a Gradle Daemon (subeequent builds wi11 be faster)
> Task :he1p
We1come to Gradle 4.3.1.
To run a build, run gradlew <task> ...
To see a list of available tasks, run gradlew tasks
To see a list of command-line options, run gradlew –help
To see more detail about a task, run gradlew help --task <task>
DUILD SUCCESSPUL in 8s
1 actionable task: 1 executed
第五步,轉換為Eclipse專案,執行import-into-eclipse.bat命令,構建前請確保網路狀態良好,按任意鍵繼續:
----------------------------------------------------------------------
Spring Framework - Eclipse/STS project impart guide
This script will guide you through the process of importing the Spring
Framerork projects into Ec1ipse or the Spring Tool Suite STS) It is
recommended that you have a recent version of Eclipse or STS. As a bare
minimum you wi11 need Eclipse with full Java 8 support, the AspectJ
Development Tools (AJDT), and the Groovy Compiler.
If you need to download and install Eclipse or STS, please do that now
by visiting one of the following sites:
- Eclipse dommloads: http://dowload.eclipse.org/eclipse/dowloads
- STS dowmloads: http://spring.io/too1s/sts/a11
- STS nightly builds:http://dist.springsource.com/snapshot/STS/night1y-distributions.html
- ADJT: http://www.eclipse.org/ajdt/downloads/
- Groovy Eclipse: https://github.com/groovy/groovy-eclipse/wiki
Otheryise. prgss enter and we’11 begin.
請按任意鍵繼續. . .
----------------------------------------------------------------------
STEP 1: Generate subproject Eclipse metadata
The first step wil1 be to generate Eclipse project metadata for each
of the spring-* subprojects. This happens via the built-in
"Gradle wrapper" script (./gradlew in this directory). If this is your
first time using the Gradle wrapper, this step may take a fewr minutes
while a Gradle distribution is downloaded for you.
第六步,等待構建成功(若中途出現錯誤,大部分情況是由于網路中斷造成的,一般重試一下都能解決問題),構建成功后會出現如下資訊:
BUILD SUOCESSFUL in 6s
6 actionable tasks: 6 executed
----------------------------------------------------------------------
STEP 4: Import root project into Ec1ipse/STS
Follow the project inport steps listed in step 2 above to inport the
root project.
Press. enter, when complete, and move on to the final step.
請按任意鍵繼續. . .
----------------------------------------------------------------------
STEP 5: Enable Git support for all projects
- In the Ec1ipse/STS Package Exp1orer, se1ect al1 spring* projects.
- Right-click to cpen the context menu and select Team > Share Project...
- In the Share Project dialog that appears, select Git and press Next
- Check "Use or create repository in parent folder of project"
- C1ick Finish
When complete, you' 11 have Git support enabled for all projects.
You' re ready to code! Goodbye!
到此為止,已經可以將專案匯入Eclipse了,我們推薦使用的IDEA也比較智能,可以直接兼容Eclipse專案,接下來繼續看下面的步驟,
第七步,匯入IDEA,打開IntelliJ IDEA,單擊“Import Project”,在彈出的界面中選擇spring-framework-5.0.2.RELEASE檔案夾,單擊“OK”按鈕,再單擊“Finish”按鈕,如下三圖所示,



第八步,等待構建完成,在網路良好的情況下大約需要10分鐘便可自動構建完成,你會看到如下圖所示界面,

第九步,在IDEA中,如果能找到ApplicationContext類,按Ctrl+Shift+Alt+U鍵,出現如下圖所示類圖界面說明構建成功,

3 Gradle構建程序中的坑
如果專案環境一直無法構建成功,類圖無法自動生成,那么你一定是踩到了一個坑,
第一步,單擊View→Tool Windows→Gradle命令,如下圖所示,

第二步,單擊Gradle視圖中的重繪按鈕,如下圖所示,

這時會出現如下圖所示的錯誤,

第三步,出現錯誤顯然跟Gradle沒有任何關系,解決辦法:
(1)關閉IDEA,打開任務管理器,結束跟Java有關的所有行程,
(2)找到JAVA_HOME→jre→lib目錄,將tools.jar重命名為tools.jar.bak,
(3)重啟IDEA,再次單擊重繪按鈕,等待構建完成,

本文為“Tom彈架構”原創,轉載請注明出處,技術在于分享,我分享我快樂!
如果本文對您有幫助,歡迎關注和點贊;如果您有任何建議也可留言評論或私信,您的支持是我堅持創作的動力,
原創不易,堅持很酷,都看到這里了,小伙伴記得點贊、收藏、在看,一鍵三連加關注!如果你覺得內容太干,可以分享轉發給朋友滋潤滋潤!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/398391.html
標籤:Java
