需要使用gradle进行编译Spring的可以参考。研读编译脚本和网上文章经过测试现在可以编译下载的所有Spring版本源码。
写出来给大家共享,主要关注2点就可以解决编译问题。本文采用idea2020.3和idea2019.3完成编译。
0、下载源码和gradle地址
https://github.com/spring-projects/spring-framework
https://services.gradle.org/distributions/
1、gradle版本
编译的gradle版本一定跟release中gradle版本一致,再说详细点就是kotlin的版本需要一致,因为要用kotlin进行编译。
用idea和命令行的gradle可能不一样。因此需要指定idea的gradle版本跟Spring源码一致。
1.1、导入项目
导入后先停止编译过程进行配置修改
1.2、修改gradle离线下载配置
distributionUrl=file:///G:/Software/Gradle/gradle-4.10.3-bin.zip
1.3、修改gradle.properties配置
#新增如下配置,解决heap堆内存空间不够问题
org.gradle.jvmargs=-Xms2048m -Xmx4096m
org.gradle.caching=true
org.gradle.parallel=true
gradlePropertiesProp=gradlePropertiesValue
sysProp=shouldBeOverWrittenBySysProp
systemProp.system=systemValue
org.gradle.daemon=true
org.gradle.configureondemand=true
1.3、设置idea的参数
- jdk参数
- gradle参数
5.1.X版本:没研究为什么需要指定gradle-wrapper为spring自带的配置文件:spring-framework-5.1.19.RELEASE\gradle\wrapper\gradle-wrapper.properties
5.2.X版本
2、下载源地址修改
这个网上很多就不详细叙述,有个关键点要说一下,就是要覆盖所有的gradle脚本的repositories源地址(或者搜索mavenCentral)。
因为每个release版本的gradle脚本可能略有不同,release5.1.19在docs.gradle有repositories,而release5.1.13就么有。
将所有地方都拷贝上阿里云源,感谢阿里。
maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter' }
3、进行编译
4、常见问题解决
4.1、gradle参数设置错误会出现以下问题
org.jetbrains.kotlin.idea.configuration.KotlinMPPGradleProjectResolver.createModule(KotlinMPPGradleProjectResolver.kt:67)
4.2、授权问题
问题解决方案:注释掉 id 'io.spring.gradle-enterprise-conventions' version '0.0.2'
问题详细描述:
在编译release5.2.8版本发生,报错信息如下:
Build file 'D:\Workplace\Workspace\workspace-idea-xiangxue\spring-framework-5.2.8.RELEASE\build.gradle' line: 6
Error resolving plugin [id: 'io.spring.gradle-enterprise-conventions', version: '0.0.2']
> Could not resolve all dependencies for configuration 'detachedConfiguration5'.
> Could not determine artifacts for io.spring.gradle-enterprise-conventions:io.spring.gradle-enterprise-conventions.gradle.plugin:0.0.2
> Could not get resource 'https://repo.spring.io/plugins-release/io/spring/gradle-enterprise-conventions/io.spring.gradle-enterprise-conventions.gradle.plugin/0.0.2/io.spring.gradle-enterprise-conventions.gradle.plugin-0.0.2.jar'.
> Could not HEAD 'https://repo.spring.io/plugins-release/io/spring/gradle-enterprise-conventions/io.spring.gradle-enterprise-conventions.gradle.plugin/0.0.2/io.spring.gradle-enterprise-conventions.gradle.plugin-0.0.2.jar'. Received status code 401 from server: Unauthorized