参考大佬文章,完美完成本地idea spring源码编译和demo测试
参考链接(spring5.3.x)
spring5.3.x源码阅读环境搭建
下面是spring6.0.x参考
idea 2022.2.4版本本地编译spring源码项目
spring6.0.x 分支
gradle-8.2 版本
jdk17 版本
File > Project Structure > Project Settings > Project
File > Project Structure > Project Settings > Modules
File > Project Structure > Platform Settings > SDKs
File > Settings > Build, Execution, Deployment > Compiler > Java Compiler
Project bytecode version: 17
File > Settings > Build, Execution, Deployment > Build Tools > Gradle
如果遇到下面的错
Could not resolve all dependencies for configuration ':spring-webflux:jmhCompileClasspath'. Failed to calculate the value of task ':spring-core:compileJava21Java' property 'javaCompiler'. No matching toolchains found for requested specification: {languageVersion=21, vendor=any, implementation=vendor-specific} for WINDOWS on x86_64. No locally installed toolchains match and toolchain download repositories have not been configured.
参看文章 spring源码编译bug
文章解决方案如下
找到项目中 spring-core文件夹中的spring-core.gradle文件中下面这行代码将21去掉即可!!!
修改前
multiRelease { targetVersions 17 , 21 }
修改后
multiRelease { targetVersions 17 }
最终编译成功,下面问题可以忽略
Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.
执行 spring-oxm 模块
执行 spring-core 模块
如何想看spring-web模板代码,如何直接点击某一个类报错,可以
先执行 spring-web 模块,如下图,双击compileTestJava,完成编译
出现下面结果,不会报错类应该不会报错了。
至此,完成代码编译。
可以新建一个模块,写测试代码,调用spring相关的类。
spring 6.0.x 选择 JDK17
spring 5.3.x选择JDK11
下面是以5.3.x代码为例(JDK11)
点击Create,
会生成build.gradle文件,可以填写以下依赖
plugins { id 'java' } group 'org.springframework' version '5.3.29' repositories { mavenCentral() } dependencies { api(project(":spring-beans")) api(project(":spring-core")) api(project(":spring-aop")) api(project(":spring-context")) api(project(":spring-expression")) testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1' } test { useJUnitPlatform() }
等结束后,新建包和类如下参考
参考目录
JavaConfiguration.java类如下
package org.springframework.learning; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class JavaConfiguration { @Bean public User user() { User user = new User(); user.setAge(11); user.setUsername("mike uli"); return user; } }
User.java
package org.springframework.learning; public class User { private String username; private Integer age; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "User{" + "username='" + username + '\'' + ", age=" + age + '}'; } }
TestMain.java
package org.springframework.learning; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class TestMain { public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(JavaConfiguration.class); User user = context.getBean("user", User.class); System.out.println(user); } }
点击执行代码,完成
输出以下结果,说明没问题了,可以bebug看代码了。
可能遇到的问题列表
## For more details on how to configure your build environment visit # http://www.gradle.org/docs/current/userguide/build_environment.html # # Specifies the JVM arguments used for the daemon process. # The setting is particularly useful for tweaking memory settings. # Default value: -Xmx1024m -XX:MaxPermSize=256m # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 # # When configured, Gradle will run in incubating parallel mode. # This option should only be used with decoupled projects. More details, visit # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects # org.gradle.parallel=true #Tue May 15 12:14:36 CST 2018 systemProp.https.proxyPort=80 systemProp.http.proxyHost=mirrors.neusoft.edu.cn systemProp.https.proxyHost=mirrors.neusoft.edu.cn systemProp.http.proxyPort=80
1.添加了阿里的maven。 2.然后在gradle\wrapper\gradle-wrapper.properties里把distributionUrl换成你本地的。
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' } maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' } maven { url 'http://maven.aliyun.com/nexus/content/repositories/google' } maven { url 'http://maven.aliyun.com/nexus/content/repositories/gradle-plugin' }
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/'} maven { url'https://maven.aliyun.com/repository/public/' } maven { url'https://maven.aliyun.com/repository/google/' } maven { url'https://maven.aliyun.com/repository/jcenter/' } maven { url'https://maven.aliyun.com/repository/central/' }
安卓开发:解决Caused by: org.gradle.api.internal.artifacts.ivyservice.DefaultLenientConfiguration等问题
4、(最重要的步骤)如果再次报错,请不厌其烦的且厚不要脸的多次点击这个小象(即重复第二步操作),正常情况下奇迹会发生的。(反正我是见证该奇迹了)
stack overflow 降低jdk版本