I am ready to learn the source code of Spring. I want to learn the source code of Spring, of course, I need to make the source environment out of the source code. I thought it should not be complicated, but I have downloaded 5 versions of Gradle in two days. As soon as you’re done, you want to share it with someone who’s also building it, so get started.

First, environmental preparation

1. JDK version selection

It is recommended that version 1.8 or above, the author’s JDK version

2. Download and install Gradle

Spring source code is built on Gradle, so of course you need to configure the Gradle environment first.

(1) the official download address: services.gradle.org/distributio…

(2) There are so many gradle versions that I can download them. This should be checked in the gradle-wrapper.properties file in the gradle-wrapper.properties directory where you downloaded spring source code.

For example, the gradle version in spring source code is 4.1.1, so download this version. For this version, you can download gradle. For this version, you can view gradle source code.

(3) Install and configure environment variables

1) After downloading and decompressing, add system environment variable GRADLE_HOME

Add path: %GRADLE_HOME%\bin

3) Verify that gradle is configured successfully. Type gradle -v

If the following figure is displayed, the installation is successful.

3. Download spring source code

(1) You can go to many code repositories to download the source code, such as Github, Gitee, Open Source China, etc.

I choose github here, for nothing else just want to see their network speed to force. Poor Internet speed can choose Gitee, open source Chinese code base.

Download source address: github.com/spring-proj…

Gradle-wrapper. properties, build.gradle, gradle-wrapper.properties, gradle-wrapper.properties, gradle-wrapper.properties, gradle-wrapper.properties, gradle-wrapper.properties, gradle-wrapper.properties, gradle-wrapper.properties, gradle-wrapper.properties, gradle-wrapper.properties, gradle-wrapper.properties, gradle-wrapper.properties So be sure to select the branch version and download it via ZIP.

4. Idea version 2020.1

Second, the configuration

1. Modify the spring source directorygradle\wrapper\gradle-wrapper.propertiesAfter modification, the configuration is as follows

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
#distributionUrl=https:/ / services.gradle.org/distributions/gradle-4.4.1-bin.zip
distributionUrl=file:/ / / D: / Gradle/Gradle - 4.4.1 - all. Zip
Copy the code

This way, every time you open a project, you don’t download gradle from the official website.

2. Add ali Cloud image to build. Gradle file in spring root directory.

Head position

buildscript {
   repositories {
      maven { url "http://maven.aliyun.com/nexus/content/groups/public/"}
      maven{ url "http://maven.aliyun.com/nexus/content/repositories/jcenter"}
      gradlePluginPortal()
      maven { url "https://repo.spring.io/plugins-release" }
   }
   dependencies {
      classpath("IO. Spring. Gradle: propdeps - plugin: 0.0.9. RELEASE")
      classpath("IO. Spring. Gradle: docbook - reference - the plugin: 0.3.1")
      classpath("Org. Asciidoctor: asciidoctorj - PDF: 1.5.0 - alpha. 16")
      classpath("Org. Asciidoctor: asciidoctorj - epub3:1.5.0 - alpha. 7")}}Copy the code

Search the repositories

repositories {
   maven { url "http://maven.aliyun.com/nexus/content/groups/public/"}
   maven{ url "http://maven.aliyun.com/nexus/content/repositories/jcenter"}
   mavenCentral()
   maven { url "https://repo.spring.io/libs-spring-framework-build"}}Copy the code

3. Open IDEA, open any project, and configure Gradle

Gradle user Home: indicates the location of the local Gradle repository. You are advised to download jar packages to disk C by default

4. Open the spring source code with Open

After opening the JDK, configure it, otherwise build.gradle will have some errors. File -> Project Structure (CTRL + Alt + Shift + S)

Change to local JDK

Download dependencies

After the above configuration is complete, you can click Refresh to download the dependency.

Because spring relies on a large number of packages, this process may be quite long. If you are capable, you can build a scientific Internet tool.

Once the download is complete, the final and most troublesome step is to compile the project.

4. Project compilation

1. Preparation

Open the spring root gradle/docs. Gradle file

Comment out dokka, Asciidoctor code blocks

Because spring source code is developed on Linux system, Linux file directory is different from Window, so you need to modify the task schemaZip code, here directly comment the original code, paste the new code.

task schemaZip(type: Zip) {
   group = "Distribution"
   baseName = "spring-framework"
   classifier = "schema"
   description = "Builds -${classifier} archive containing all " +
         "XSDs for deployment at http://springframework.org/schema."
   duplicatesStrategy 'exclude'

   // Whether the current system is a Windows flag
   def isWindows = System.properties['os.name'].toUpperCase().contains('WINDOWS')

   // Different operating systems use different symbols to represent subdirectories
   def schemaPath = isWindows ? "META-INF\spring.schemas" : "META-INF/spring.schemas"

   moduleProjects.each { subproject ->
      def Properties schemas = newProperties(); subproject.sourceSets.main.resources.find { it.path.endsWith(schemaPath) }? .withInputStream { schemas.load(it) }for (def key : schemas.keySet()) {
         def shortName = key.replaceAll(/http.*schema.(.*).spring-.*/, '$1')
         assertshortName ! = key File xsdFile = subproject.sourceSets.main.resources.find {// On Windows, replace the delimiter in the path
            isWindows ? it.path.endsWith(schemas.get(key).replaceAll('/ /'.'\ \')) : it.path.endsWith(schemas.get(key))
         }
         assertxsdFile ! =null
         into (shortName) {
            from xsdFile.path
         }
      }
   }
}
Copy the code

2. Perform compilation

In fact, spring source code provides the documentation of idea construction. If you are interested, you can open the root directory of import-into-idea.md to learn about it.

Build Spring-OXM first as described above

When you double-click complieTestJava to execute compilation, it may fail due to network problems. You can execute it again.

3. Remove the Spring-sapects module

I have removed the Spring-Sapects module, and some documentation says not to do so, but recommends it anyway.

4. Compile Spring-core

Double-click to run complieTestJava to compile

Congratulations, you’re one step closer to success.

5. Global compilation

This compilation time may be slower, don’t worry, drink some coffee, brush micro blog, patience will be successful.

If you succeed, congratulations, you have almost finished building the Spring source code. Here’s a simple test to see if you really succeed

Five, the test

Create a New Module File -> New -> Module

Select the Gradle project, then next

After the new module is created, open the build.gradle file in the new module directory and add the dependencies

dependencies {

    compile(project(":spring-beans"))
    compile(project(":spring-core"))
    compile(project(":spring-aop"))
    compile(project(":spring-context"))
    testCompile group: 'junit', name: 'junit', version: '4.12'
}
Copy the code

1) Create a new configuration class BaseConfig under the module

2) Create a new class X

3) Create a test class

Run the test class main method

Successfully output the X class constructor and hello method.

So far, congratulations on successfully building your Spring source learning environment