编译之前
-
Gradle安装
这里我用的是Mac,比较简单:
brew install gradle
测试是否安装完成:
gradle -v
-
拉取Spring源码
访问GitHub的Spring框架页面,获取git链接(这里使用Gitee的镜像)
git clone https://gitee.com/mirrors/Spring-Framework.git
拉取完成后,进入目录,首先康康里面的导入文档
我决定使用IDEA,所以打开import-into-idea.md
编译
已知的问题
-
spring-core
andspring-oxm
should be pre-compiled due to repackaged dependencies.
See*RepackJar
tasks in the build and https://youtrack.jetbrains.com/issue/IDEA-160605). -
spring-aspects
does not compile due to references to aspect types unknown to
IntelliJ IDEA. See https://youtrack.jetbrains.com/issue/IDEA-64446 for details. In the meantime, the 'spring-aspects' can be excluded from the project to avoid compilation errors. - While JUnit tests pass from the command line with Gradle, some may fail when run from
IntelliJ IDEA. Resolving this is a work in progress. If attempting to run all JUnit tests from within IntelliJ IDEA, you will likely need to set the following VM options to avoid out of memory errors:
-XX:MaxPermSize=2048m -Xmx2048m -XX:MaxHeapSize=2048m - If you invoke "Rebuild Project" in the IDE, you'll have to generate some test
resources of thespring-oxm
module again (./gradlew :spring-oxm:compileTestJava
)
步骤:
-
修改部分配置
早就知道Spring借助Gradle编译因为网络的原因需要非常长的时间。于是打开Spring项目根目录的gradle.build,在默认的Maven仓库URL前加入阿里的仓库
repositories { maven { url 'https://maven.aliyun.com/repository/gradle-plugin' } maven { url 'https://maven.aliyun.com/repository/google' } maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' } maven { url 'https://maven.aliyun.com/repository/jcenter'} maven { url "http://repo.springsource.org/plugins-release" } mavenCentral() maven { url "https://repo.spring.io/libs-spring-framework-build" } maven { url "https://repo.spring.io/snapshot" } // Reactor maven { url "https://oss.jfrog.org/artifactory/oss-snapshot-local" } // RSocket }
并且,看到有博客说过高的JDK版本可能不利于Spring的编译,于是我特别准备了JDK8:
-
导入IDEA
导入即开始构建,于是报错:

注释部分代码:

再次尝试编译:

行吧,换种思路,不用IDEA进行编译。
在命令行预编译spring-oxm
q@qs-MacBook-Pro Spring-Framework % ./gradlew :spring-oxm:compileTestJava


-
再次尝试IDEA编译
从报错的语句可以看出应该是从阿里的Gradle插件仓库下载的文件有问题,所以尝试将阿里Gradle插件仓库移除:
(注释掉即可)
再次尝试:
十几分钟之后完成了构建。
玄学,玄学
-
测试
在Spring源码根目录下新建一个spring-mycode模块:右击项目 New->Module->Gradle
首先,为mycode模块导入Spring的context模块。编辑mycode模块的build.gradle:
在mycode下新建一个MyConfig作为配置类,User作为一个普通的实体类,MyApplication作为应用启动入口。
MyConfig:
@Configuration public class MyConfig { @Bean public User user(){ return new User("靓仔"); } }
User:
public class User { private String name; public User(String name) { this.name = name; } @Override public String toString() { return "User{" + "name='" + name + '\'' + '}'; } }
MyApplication:
public class MyApplication { public static void main(String[] args) { AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(MyConfig.class); User bean = annotationConfigApplicationContext.getBean(User.class); System.out.println(bean); } }
运行结果:
靓仔
完美,靓仔的名字被打印在了屏幕上。
快乐
网友评论