文章目录
使用Gitee从GitHub导入Spring源码到自己仓库
Spring在github的仓库地址:https://github.com/spring-projects/spring-framework
使用Idea导入项目
导入Spring项目
配置gradle
下载高版本的JDK及在gradle中的配置方式
http://www.codebaoku.com/jdk/jdk-index.html
注意:
后续报运行测试环境搭建是否成功,报 错误: 无效的源发行版:17
所以这里是下载JDK17版本
后续的jdk15的版本操作都用jdk17代替
直接下载压缩包,解压缩
添加jdk为刚解压的目录
等待下载相关jar包
下载失败的情况说明
下载时有时会提示在仓库找不到相关jar,下载失败
这时关闭项目,删除项目中的.idea文件,重新导入项目
环境搭建完成
测试Spring源码是否搭建成功
测试
新建自己的测试module
引入spring-context
参照其他原有工程引入方式
在my-spring-source-test工程下的build.gradle文件
dependencies {
//引入Spring核心容器
api(project(":spring-context"))
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
}
选中自己的测试module刷新
引入其他jar依赖
引入lombok
https://mvnrepository.com/artifact/org.projectlombok/lombok/1.18.20
my-spring-source-test下build.gradle配置
dependencies {
//引入Spring核心容器
api(project(":spring-context"))
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
// https://mvnrepository.com/artifact/org.projectlombok/lombok
compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.20'
}
使用
代码测试
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="com.myspring.sourcetest.bean.Person" id="person"></bean>
</beans>
运行
报错说明
No candidates found for method call api. 引入依赖报错
没有pai方法
解决:
https://docs.gradle.org/current/userguide/java_library_plugin.html#sec:java_library_separation
将build.gradle配置
plugins {
id 'java'
}
改为
plugins {
id 'java-library'
}
仓库地址修改为https
选中自己的测试module刷新
找不到相应jar包问题
阿里云仓库找不到
https://docs.gradle.org/current/userguide/declaring_repositories.html
移除D:\File\development_data\gradle\gradle-7.4.2\init.d下的init.gradle下载加速配置
选中自己的测试module刷新
maven仓库无法下载
引入lombok时报错
上面已经将阿里仓库配置移除了,所以从maven仓库拉取
解决:
在本module的build.gradle配置
repositories {
// mavenCentral()
maven {
url "https://maven.aliyun.com/repository/public"
}
}
选中自己的测试module刷新
综合仓库配置方案
目前情况
根据最初的init.gradle文件配置
有相关jar包找不到
Could not find netty-codec-http2-4.1.77.Final.jar (io.netty:netty-codec-http2:4.1.77.Final).
Searched in the following locations:
https://maven.aliyun.com/repository/jcenter/io/netty/netty-codec-http2/4.1.77.Final/netty-codec-http2-4.1.77.Final.jar
Possible solution:
- Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html
在init.d下排除init.gradle配置后,从maven仓库拉取(速度慢,报无法下载)
所以在moudule的build.gradle中单独配置仓库地址
repositories {
// mavenCentral()
maven {
url "https://maven.aliyun.com/repository/public"
}
}
存在问题:
在导入其他项目源码时,由于在init.d下排除了init.gradle配置,会走maven仓库下载
尝试的解决方案:
修改init.gradle配置
项目工程重新加载
提示警告
此版本中使用了不推荐的Gradle功能,使其与Gradle 8.0不兼容。
Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
See https://docs.gradle.org/7.4.2/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 4m 40s
7 actionable tasks: 7 up-to-date
A build scan was not published as you have not authenticated with server 'ge.spring.io'.
Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
See https://docs.gradle.org/7.4.2/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 1s
错误: 无效的源发行版:17
运行测试时报错
使用jdk17版本
设置
File-Settings
1
2
File-Project Structure
报错:
错误: 加载主类 com.myspring.sourcetest.BeanTest 时出现 LinkageError
java.lang.UnsupportedClassVersionError: com/myspring/sourcetest/BeanTest has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 59.0
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':my-spring-source-test:BeanTest.main()'.
> Process 'command 'D:/File/development_data/jdk/jdk-15.0.2/bin/java.exe'' finished with non-zero exit value 1
1
2