“我报名参加金石计划1期挑战——瓜分10万奖池,这是我的第2篇文章,点击查看活动详情”
spring源码编译的过程会遇到各种各样的问题,这不是一片文章就能解决的。只要大的方向不差,基本都是能解决的。不要使用idea 2019.3 版本编译,会有问题。
环境:
Spring: spring-framework-5.3.10
Idea:IntelliJ IDEA 2021.1.3
gradle: gradle-7.2
jdk 建议15
macos 系统
1. spring源码下载地址
https://github.com/spring-projects/spring-framework
2. gradle 下载地址
https://services.gradle.org/distributions/
这个地方下载的版本要和spring源码里的gradle 版本一致 spring-framework-5.3.10 --> gradle --> wrapper -->gradle-wrapper.properties 打开这个文件gradle-wrapper.properties
3.配置环境变量
将下载的 gradle 文件拷贝到 /usr/local 下
可以使用 command+shift+g 找到/usr/local 目录
mac:
执行:open -e .bash_profile
export GRADLE_HOME=/usr/local/gradle-7.2
export PATH=${GRADLE_HOME}/bin:$PATH
执行: source ~/.bash_profile 验证: gradle -version
4.修改 build.gradle 文件
将
repositories {
mavenCentral()
maven { url "https://repo.spring.io/libs-spring-framework-build" }
}
修改为
repositories {
maven { url "https://maven.aliyun.com/nexus/content/groups/public/" }
maven { url "https://maven.aliyun.com/nexus/content/repositories/jcenter" }
mavenCentral()
maven { url "https://repo.spring.io/libs-spring-framework-build" }
}
5.打开终端命令行
输入:./gradlew :spring-oxm:compileTestJava
windows 下:gradlew :spring-oxm:compileTestJava
等待编译就行了。
出现这个就代表成功了
在这中间会遇见各种问题,我是通过idea 和 终端 来回切换 , 先终端编译 如果有问题,使用idea 配置本地gradle ,编译 ,遇见问题,就参照下面问题的解决方法 , 解决后重新编译,如果还失败,再使用终端编译。
6.新建一个模块 测试
我这里新建的是 gradle 模块
示例代码
UserService
@Service
public class UserService {
public void test(){
System.out.println(123123123);
}
}
Test
public class Test {
public static void main(String[] args) {
//ApplicationContext applicationContext = new ApplicationContext(UserService.class);
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(UserService.class);
UserService userService = (UserService)context.getBean("userService");
userService.test();
}
}
build.gradle
plugins {
id 'java'
}
group 'org.springframework'
version '5.3.10'
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
api(project(":spring-context"))
}
test {
useJUnitPlatform()
}
useService 里的test能执行成功,就说明,源码编译成功。
问题
1.项目路径不要用中文;
例如:
ClassLoader classLoader = StartApplicationContext.class.getClassLoader();
URL resource = classLoader.getResource(replace);
File file = new File(resource.getFile());
resource.getFile()是有值的,但是因为乱码 file.isDirectory()为false。
2.gradle
Cannot create service of type TaskExecuter using method ProjectExecutionServices.createTaskExecuter() as there is a problem with parameter #15 of type ReservedFileSystemLocationRegistry.
解决办法:控制台 执行 ./gradlew --stop
配置本地gradle
3. A problem occurred configuring root project 'spring'.
Could not resolve all dependencies for configuration ':classpath'.
Could not create service of type OutputFilesRepository using ExecutionGradleServices.createOutputFilesRepository().
解决办法:按图配置下 idea 中的gradle版本
4.
Unable to find method 'org.gradle.api.artifacts.result.ComponentSelectionReason.getDescription()Ljava/lang/String;'.
Possible causes for this unexpected error include:
Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)
Re-download dependencies and sync project (requires network)
The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem.
Stop Gradle build processes (requires restart)
Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.
In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.
解决办法: 使用 2021.1.3 版本以上的idea 就可以。 当然这不是根本原因,猜测应该是某插架或者是依赖的版本不对,而 21版的idea 默认会把这些配置给配好。
也可以参考这个;juejin.cn/post/684490…
5.
A problem occurred evaluating project ':spring-test-x'.
> Could not find method compile() for arguments [project ':spring-context'] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
* Try:
Run with --stacktrace option to get the stack trace. Run with --debug option to get more log output. Run with --scan to get full insights.
版本:
Spring: spring-framework-5.3.10
Idea:IntelliJ IDEA 2021.1.3
gradle: gradle-7.2
解决;7.2版本的 gradle 要用 api 引包。
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
api(project(":spring-context"))
//compile(project(":spring-context"))
}
6. 报 错误: 程序包jdk.jfr不存在
解决:使用高版本的jdk, 建议15
- Lock file
将这个路径: /Volumes/H/java_demo/spring-framework-5.3.10/buildSrc/.gradle/buildOutputCleanup/buildOutputCleanup.lock 复制出来 可以使用 command+shift+g 将路径复制进去,删除带.lock 的文件。
- 在idea 中遇见这个问题,解决后应该就能编译成功。
终端 执行 ./gradlew --stop ./gradlew :spring-oxm:compileTestJava\