Idea中使用gradle编译spring源码


废话少说,直接进入正题!

一、下载及预编译

首先git clone源码到本地:

https://github.com/spring-projects/spring-framework.git 

代码clone到本地之后进入到文件件中cd spring-framework,这个时候需要关注import-into-idea.md这个文件,这是用idea编译源码的说明文件。根据里面的说明,在编译整个源码前需要预先编译spring-oxmspring-core这两个模块,依次运行命令./gradlew :spring-core:compileTestJava./gradlew :spring-oxm:compileTestJava 。编译过程如下,当然首先是安装好gradle:

$ ./gradlew :spring-oxm:compileTestJava

Welcome to Gradle 6.7.1!

Here are the highlights of this release:
 - File system watching is ready for production use
 - Declare the version of Java your build requires
 - Java 15 support

For more details see https://docs.gradle.org/6.7.1/release-notes.html

Starting a Gradle Daemon, 1 busy Daemon could not be reused, use --status for details

> Task :spring-oxm:genJaxb
[ant:javac] : warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.7.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 37s
40 actionable tasks: 24 executed, 16 from cache

# pengchengbai @ Pengcheng-Bai in ~/IdeaProjects/spring-framework on git:master o [11:42:36]
$ ./gradlew :spring-core:compileTestJava

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.7.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 7s
17 actionable tasks: 2 from cache, 15 up-to-date

二、导入Idea并编译

  1. 使用Idea导入源码:File -> New -> Project from Existing Souces…然后选择我们的spring-framework目录就行了 注意这里是选择New 而不是Open;

  2. 然后在Import Project 面板中选择gradle,点击Finish;

  3. 可以发现IDEA会自动开始构建编译spring源码。

三、验证

编译好源码之后怎么验证构建和编译是否成功呢?我们可以自己写一段代码,然后依赖我们构建的源码,如果能够正常运行,则说明构建成功。

  1. 在spring-framework项目下新建一个module,在新建的时候gradle构建(当然maven也可以,这里只是为了跟源码保持统一),然后Module SDK选择Java8;

  2. 然后在路径src/main/java中创建包和源代码,我这里创建了一个配置类:

package site.pengcheng;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * @author pengchengbai
 * @description
 * @date 2020/12/18 11:50 上午
 */
@Configuration
@ComponentScan("site.pengcheng")
public class AppConfig {
} 

然后再创建一个普通的spring Bean

package site.pengcheng.pojo;

import org.springframework.stereotype.Component;

/**
 * @author pengchengbai
 * @description
 * @date 2020/12/18 11:51 上午
 */
@Component
public class Hello {
  public void hello(){
    System.out.println("Hello, world");
  }
} 

最后创建一个测试类:

package site.pengcheng;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import site.pengcheng.pojo.Hello;

/**
 * @author pengchengbai
 * @description
 * @date 2020/12/18 11:52 上午
 */
public class TestHello {
  public static void main(String[] args) {
    AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
    Hello bean = ac.getBean(Hello.class);
    bean.hello();
  }
}

这里使用AnnotationConfigApplicationContext作为spring上线文去获取bean,并运行bean中的方法。运行main方法,如果输出如下信息则表示成功:

3:04:34 下午: Executing task 'TestHello.main()'...

Starting Gradle Daemon...
Gradle Daemon started in 1 s 577 ms
> Task :buildSrc:compileJava UP-TO-DATE
> Task :buildSrc:compileGroovy NO-SOURCE
> Task :buildSrc:pluginDescriptors UP-TO-DATE
...// 省略
> Task :spring-context:jar UP-TO-DATE
> Task :spring-study:compileJava
> Task :spring-study:classes

> Task :spring-study:TestHello.main()
Hello, world

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.7.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 16s
35 actionable tasks: 2 executed, 33 up-to-date
3:04:51 下午: Task execution finished 'TestHello.main()'.
  1. 最后就可以在spring的源码海洋中恣意畅游啦!

四、后记

在这个过程中可能会遇到各种各样的报错,要么是idea版本问题,要么是gradle版本问题,要么是kotlin版本问题,还有的报错根本不知道什么问题。我这里只是记录了成功编译的那次的情况,仅做参考,遇到具体的问题还得具体解决,如果不想知道错误原因就卸载、删除、推翻重来!总会成功的!


文章作者: 木白
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 木白 !
评论
  目录