因为在看spring-framework的源码,没有一个顺手的环境,所以找了一个时间专门来构建spring-framework。
获取源码
git clone https://github.com/spring-projects/spring-framework
git chekcout 5.2.2.RELEASE
git pull
构建
gradle目录
找到本地代码库的 gradle/wrapper/gradle-wrapper.properties,这个文件的意思是会下载gradle到指定目录。在mac上通常是 /Users/<your_name>/.gradle/wrapper/dists
。这个要在下一步“导入IDEA”的时候配置到gradle setting里面去。
加阿里云仓库
找到项目根目录的build.gradle文件,添加阿里云的仓库:
repositories {
mavenCentral()
maven { url "https://repo.spring.io/libs-spring-framework-build" }
maven { url "https://repo.spring.io/snapshot" } // Reactor
maven {url 'https://maven.aliyun.com/nexus/content/groups/public/'} //阿里云
maven {url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}
}
编译
编译:这个过程会耗费几十分钟,需要加代理。
可以参考官网https://github.com/spring-projects/spring-framework/wiki/Build-from-Source
cd spring-framework
./gradlew build
编完后预编译oxm:
./gradlew :spring-oxm:compileTestJava
导入idea
配置
官网在build的那个页面也有导入IDEA的手册:https://github.com/spring-projects/spring-framework/blob/master/import-into-idea.md
配置gradle到源码下载的gradle的目录:注意,这里必须跟build的时候一致,不能随意自己下另外版本的gradle,否则分分钟会报版本兼容问题。
导入
import project 为gradle项目即可,然后也是十几分钟的等待。成功后的项目:
之后把编译选项改为使用idea内置,会快一些:
新建项目
我们来建个项目以调试spring代码: File -> New -> Module,选gradle,然后在产生的module的build.gradle中加入依赖:
compile(project(":spring-context"))是新增的。
dependencies {
compile(project(":spring-context"))
testCompile group: 'junit', name: 'junit', version: '4.12'
}
然后写几个类:
@Configuration
@ComponentScan("com.shadow")
public class AppConfig {
}
@Component
public class IndexService {
}
之后写个main函数来跑即可:
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
System.out.println(ac.getBean(IndexService.class));
运行过程中如果遇到某些类找不到的错误,则找到这个类的module,然后运行test,强制其产生class文件即可。
refer:
https://blog.csdn.net/java_lyvee/article/details/107300648