javascript
Spring源码下载及构建技巧
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 UTCRevision: 316546a5fcb4e2dfe1d6aaüb73a4e09e8cecb5a5 Groovy:2. 4. 11Ant:Apache Ant THD version 1. 9.6 compiled on June 29 2015JVM: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 8s1 actionable task: 1 executed第五步,轉換為 E clipse 項目,執行 import-into-eclipse.bat 命令,構建前請確保網絡狀態良好,按任意鍵繼續:
----------------------------------------------------------------------Spring Framework - Eclipse/STS project impart guide This script will guide you through the process of importing the SpringFramerork projects into Ec1ipse or the Spring Tool Suite STS) It isrecommended that you have a recent version of Eclipse or STS. As a bareminimum you wi11 need Eclipse with full Java 8 support, the AspectJDevelopment Tools (AJDT), and the Groovy Compiler. If you need to download and install Eclipse or STS, please do that nowby 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 eachof the spring-* subprojects. This happens via the built-in"Gradle wrapper" script (./gradlew in this directory). If this is yourfirst time using the Gradle wrapper, this step may take a fewr minuteswhile a Gradle distribution is downloaded for you.第六步,等待構建成功(若中途出現錯誤,大部分情況是由于網絡中斷造成的,一般重試一下都能解決問題),構建成功后會出現如下信息:
BUILD SUOCESSFUL in 6s6 actionable tasks: 6 executed ----------------------------------------------------------------------STEP 4: Import root project into Ec1ipse/STS Follow the project inport steps listed in step 2 above to inport theroot 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 ”按鈕,再單擊“ F inish ”按鈕,如下三圖所示。
第八步,等待構建完成,在網絡良好的情況下大約需要 10 分鐘便可自動構建完成,你會看到如下圖所示界面。
第九步,在 IDEA 中,如果能找到 ApplicationContext 類,按 Ctrl+Shift+Alt+U 鍵,出現如下圖所示類圖界面說明構建成功。
3 Gradle 構建過程中的坑
如果項目環境一直無法構建成功,類圖無法自動生成,那么你一定是踩到了一個坑。
第一步,單擊 View → Tool Windows → Gradle 命令,如下圖所示。
第二步,單擊 Gradle 視圖中的刷新按鈕,如下圖所示。
這時會出現如下圖所示的錯誤。
第三步,出現錯誤顯然跟 Gradle 沒有任何關系,解決辦法:
( 1 )關閉 IDEA ,打開任務管理器,結束跟 J ava 有關的所有進程。
( 2 )找到 JAVA_HOME → jre → lib 目錄,將 tools.jar 重命名為 tools.jar.bak 。
( 3 )重啟 IDEA ,再次單擊刷新按鈕,等待構建完成。
4 小測一下
本文分享自微信公眾號 - Tom彈架構(gh_e3be84a8ccb2)。
如有侵權,請聯系 support@oschina.cn 刪除。
本文參與“ ?OSC源創計劃? ”,歡迎正在閱讀的你也加入,一起分享。
總結
以上是生活随笔為你收集整理的Spring源码下载及构建技巧的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 焊接机器人应用现状及发展趋势
- 下一篇: 用JavaScript语言判断一个三位数