基于Gradle的Spring源码下载及构建技巧

简介: 首先你的JDK需要升级到1.8以上版本。从Spring 3.0开始,Spring源码采用GitHub托管,不再提供官网下载链接。这里不做过多赘述,大家可自行去GitHub网站下载,我使用的版本下载链接为 https://github.com/spring-projects/spring-framework/archive/v5.0.2.RELEASE.zip ,下载完成后,解压源码包会看到如下图所示的文件目录。

本文节选自《Spring 5核心原理》

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 ,下载完成后,解压源码包会看到如下图所示的文件目录。

3101de53b4adb14abe2e1da64b336bb9.png

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操作系统为例。

第一步,配置环境变量,如下图所示。

dfbcfefb5eb1ca17d8ae58bb1fc947d7.png

第二步,添加环境变量“%GRADLE_HOME%\bin”,如下图所示。

d22fc1ea44c5ac09d944be779fab705c.png

第三步,检测环境,输入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”按钮,如下三图所示。

3c3e61e69e0723f7cd138d61e22a9785.png

e9d4b2e0ecbc72e409f5cdc3607c070e.png

f36111451850d1db9e84091b4db6a442.png

第八步,等待构建完成,在网络良好的情况下大约需要10分钟便可自动构建完成,你会看到如下图所示界面。

b1e93ec487f96a9dca9e0ab96e38504b.png

第九步,在IDEA中,如果能找到ApplicationContext类,按Ctrl+Shift+Alt+U键,出现如下图所示类图界面说明构建成功。

b22d9d0d0aadeef3c1ff1088d4ea08dc.png

3 Gradle构建过程中的坑

如果项目环境一直无法构建成功,类图无法自动生成,那么你一定是踩到了一个坑。

第一步,单击View→Tool Windows→Gradle命令,如下图所示。

39f8a2c64e91f39e039051a9f8ccd1ae.png

第二步,单击Gradle视图中的刷新按钮,如下图所示。

424a87e53cfd7f38fc102dac11522aa0.png

这时会出现如下图所示的错误。

c70e0bfe067b5fa9152d1373043cb186.png

第三步,出现错误显然跟Gradle没有任何关系,解决办法:

(1)关闭IDEA,打开任务管理器,结束跟Java有关的所有进程。

(2)找到JAVA_HOME→jre→lib目录,将tools.jar重命名为tools.jar.bak。

(3)重启IDEA,再次单击刷新按钮,等待构建完成。


【推荐】Tom弹架构:收藏本文,相当于收藏一本“设计模式”的书


本文为“Tom弹架构”原创,转载请注明出处。技术在于分享,我分享我快乐!

如果本文对您有帮助,欢迎关注和点赞;如果您有任何建议也可留言评论或私信,您的支持是我坚持创作的动力。

相关文章
|
17小时前
|
Java API 项目管理
Java一分钟之-Gradle插件开发:自定义构建逻辑
【6月更文挑战第5天】Gradle插件开发详解:从入门到发布。文章介绍如何创建自定义插件,强调依赖管理、任务命名和配置阶段的理解。示例代码展示插件实现及避免常见问题的方法。最后,讨论插件的发布与共享,助你提升构建效率并贡献于开发者社区。动手实践,打造强大Gradle插件!
16 3
|
2天前
|
Java Spring
【JavaEE进阶】 Spring AOP源码简单剖析
【JavaEE进阶】 Spring AOP源码简单剖析
|
2天前
|
人工智能 Java Spring
使用 Spring Cloud Alibaba AI 构建 RAG 应用
本文介绍了RAG(Retrieval Augmented Generation)技术,它结合了检索和生成模型以提供更准确的AI响应。示例中,数据集(包含啤酒信息)被加载到Redis矢量数据库,Spring Cloud Alibaba AI Starter用于构建一个Spring项目,演示如何在接收到用户查询时检索相关文档并生成回答。代码示例展示了数据加载到Redis以及RAG应用的工作流程,用户可以通过Web API接口进行交互。
|
8天前
|
前端开发 IDE Java
构建一个基于React和Spring Boot的简易聊天室应用
构建一个基于React和Spring Boot的简易聊天室应用
27 0
|
10天前
|
Java API 网络架构
利用Java Spring Boot构建微服务架构的实践探索
随着业务复杂性的增长和互联网技术的飞速发展,微服务架构已成为现代软件开发中不可或缺的一部分。本文旨在探讨如何利用Java Spring Boot框架构建微服务架构,包括微服务的定义、优势,以及通过实际案例展示如何设计、开发和部署微服务。我们将关注服务拆分、服务间通信、数据一致性、服务治理等核心问题,并探讨如何结合Spring Cloud生态中的组件来实现高效、可靠的微服务架构。
|
14天前
|
Java Maven Docker
Docker化Spring Boot3应用:从镜像构建到部署
本文介绍了如何在Linux上通过命令行构建和运行Spring Boot 3服务的Docker镜像。首先,基于Ubuntu创建包含JDK 21的基础镜像,然后使用Maven打包Spring Boot应用。接着,构建服务镜像,将应用和依赖添加到镜像中,并设置工作目录和暴露端口。最后,利用docker-compose部署服务,挂载宿主机目录以方便更新静态文件。Docker简化了应用部署,确保了不同环境的一致性。
78 2
Docker化Spring Boot3应用:从镜像构建到部署
|
15天前
|
小程序 JavaScript Java
小程序商城|基于Spring Boot的智能小程序商城的设计与实现(源码+数据库+文档)
小程序商城|基于Spring Boot的智能小程序商城的设计与实现(源码+数据库+文档)
23 0
小程序商城|基于Spring Boot的智能小程序商城的设计与实现(源码+数据库+文档)
|
15天前
|
小程序 JavaScript Java
高校宿舍信息|基于Spring Boot的高校宿舍信息管理系统的设计与实现(源码+数据库+文档)
高校宿舍信息|基于Spring Boot的高校宿舍信息管理系统的设计与实现(源码+数据库+文档)
22 0
|
15天前
|
安全 JavaScript Java
在线问卷调查|基于Spring Boot的在线问卷调查系统的设计与实现(源码+数据库+文档)
在线问卷调查|基于Spring Boot的在线问卷调查系统的设计与实现(源码+数据库+文档)
26 0
|
Java Spring 容器
SPRING01_基于gradle6.8.2和JDK15搭建Spring源码坏境(三)
SPRING01_基于gradle6.8.2和JDK15搭建Spring源码坏境(三)
116 0
SPRING01_基于gradle6.8.2和JDK15搭建Spring源码坏境(三)