编译spring-framework源码

编译spring-framework源码

会写这个博客的原因来自我自己想学习 spring-framework 源码, 在导入源码的过程中遇到了一些问题, 明白了这个大项目导入IDE并不是一个简单的事情, 就此写下笔记

步骤

  1. 下载源码
  2. 修改gradle配置
  3. 导入IDEA
  4. 编写自己的代码

下载源码

spring-framework 的源码还是比较好找的, github 上一搜就是第一个
这里提供地址给懒得搜的同学:

https://github.com/spring-projects/spring-framework

使用git客户端可以很容易把源码下载到本地硬盘

git clone https://github.com/spring-projects/spring-framework.git

但 github 毕竟在外网, 有时候因为网络问题并不能很顺利下载下来, 我使用 码云 把项目导入到国内, 再把项目下载下来, 就快很多

这里提供码云地址给需要的同学

https://gitee.com/Sod-Momas/spring-framework

克隆命令

git clone https://gitee.com/Sod-Momas/spring-framework.git

不过导入的项目有个缺点, 就是无法及时同步到 github 上的源码更新, 需要定时去点一下同步, 如果有同学注意到代码过旧了, 可以在评论区提醒我一下, 我去点一下同步, 或者读者自己建个仓库导入也行.

修改 gradle 配置

修改 gradle 配置有两点

  1. 临时去除 spring-aspects 模块
  2. 修改 maven 仓库地址

修改的文件如下

  • ./settings.gradle 第1行

    pluginManagement {
        repositories {
    //        添加阿里云仓库
            maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
            maven { url 'https://maven.aliyun.com/repository/spring' }
    //           gradlePluginPortal()
    //           maven { url 'https://repo.spring.io/plugins-release' }
        }
    }
    
    apply from: "$rootDir/gradle/build-cache-settings.gradle"
    
    include "spring-aop"
    // 去除 aspects 模块
    //include "spring-aspects"
     include "spring-beans"
    include "spring-context"
    include "spring-context-support"
    include "spring-context-indexer"
    
  • ./build.gradle 第 296 行

            repositories {
                maven { url 'https://maven.aliyun.com/repository/central' }
                maven { url 'https://maven.aliyun.com/repository/spring' }
    //           mavenCentral()
    //           maven { url "https://repo.spring.io/libs-spring-framework-build" }
                maven { url "https://repo.spring.io/snapshot" }
            }
    
  • buildSrc/build.gradle 第 5 行

repositories {
	maven { url 'https://maven.aliyun.com/repository/central' }
	maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
	// mavenCentral()
	// gradlePluginPortal()
}

导入 IntelliJ IDEA

  1. 使用 Import Project 导入项目
  2. 选择 Gradle 项目模型
  3. 编译 spring-oxm 项目
  4. 编译 加入 spring-aspects 模块编译
  5. 编译整个 spring-framework

导入的方法如图

导入项目

导入项目后,选择Gradle项目模型

编译 spring-oxm 项目, 使用 IDEA 里的命令行工具执行 ./gradlew :spring-oxm:compileTestJava

编译成功如图

编译 spring-oxm 模块成功

这一步编译成功的话, 说明项目整体已经没有问题了, 可以把刚刚在 settings.gradle 里注释的 include 'spring-aspects' 加回来, 让该模块加入编译

最后, 执行整个项目的编译
可以使用命令行

./gradlew build

也可以使用 IDEA 带的 build 工具栏刷新一下

gradle 工具栏 build 成功

因为我是第二次编译所以很快, 第一次编译有可能会花上一个小时的时间, 请注意

编写自己的代码

这一步的话, 我目前还没有完美解决, 因为 spring 对 gradle 做了很多配置, 导致加入gradle 子模块需要修改很多东西, 我也懒得去折腾了, 就直接在一个子模块里写自己的代码了

如图

编写hello world

教程到这里就结束了, 如果有什么不明白可以在评论区里向我提出, 我会尽量添加到博客里来

为了方便大家学习, 我把编译过的 spring-framework 源码打包了一份传到网盘里, 需要的同学可以下载使用

参考资料

  • AliRepo 博客里使用的阿里云仓库地址均可从该页面找到
  • 莫弹弹 / spring-framework 由作者导入到码云的 spring-framework 项目源码, 目前是 5.1.x 分支版本
  • spring-framework/import-into-idea.md 这个是官方导入教程, 本文基本就是按照该文档步骤导入的. 这个文档存在于源码根目录, 读者可以自行打开阅读
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值