Source code learning the first step, Spring source code compilation

The reason for writing such an article is because the group of friends in the compilation of source code encountered problems, plus the author is preparing to do a annotated version of the source code, just need to recompile a code, as for why to compile the source code to the local don’t need to say more?

For example, you can add arbitrary comments, take notes as you read the source code, even modify the source code, debug the program better, and so on. But if you insist on importing dependencies or introducing JAR packages to learn the source code, I think it is ok, there is no best, only the most suitable for their own!

The main purpose of this article is to help those who were discouraged from source code compilation at the beginning of their study to regain confidence!

Without further ado, let’s get down to business

Refer to official documentation:

https://github.com/spring-projects/spring-framework/wiki/Build-from-Source

https://github.com/spring-projects/spring-framework/blob/master/import-into-idea.md

preparation

  1. Make sure the unit is properly installedgit
  2. jdkThe version is 1.8
Snipaste_2020-07-03_10-22-10
  1. Gradle, do not need to install at present, in accordance with the source code prompts in accordance with the corresponding version of the timeGradleCan be
  2. IDEA, the version I use is as follows:
image-20200703162142721

1. Get the Spring source code

Here, I recommend using clone to pull the source code locally. The biggest advantage is that you can use IDEA to directly compare the differences between versions, for example

Insert a picture description here

In the figure above, I compiled version 5.0 of the code locally, so I’m comparing the implementation of the populateBean method in version 5.0 to version 5.1.

Next we start pulling Spring source code, you can follow the following steps

  1. Create a folder in any disk path. The name of the folder is optionalSpringFramWork
  2. Enter theSpringFramWorkFolder, opengitCommand line, enter the following command
git clone https://github.com/spring-projects/spring-framework.git
Copy the code

Then wait for the warehouse clone completed, this process may take a long time, if it is not possible, we can directly put the source code compression package down.

clone666

If you’re pulling code like me, switch to version 5.2.x and run the command from the command line:

Git checkout origin / 5.2 xCopy the code

2. Add Ali Cloud Image

In the process of compiling, Spring will automatically download some dependent packages. By default, we will use the official image, which is slow to download. Therefore, we will add the domestic image in the first place, paste the following code into the build.gradle file under the repository node.

// Add an Aliyun imagemaven { url "http://maven.aliyun.com/nexus/content/groups/public" }
Copy the code

As shown in the figure below

3. Pre-compile the Spring-OXM module

Open a command line window, switch to the source folder, and run the following command

gradlew :spring-oxm:compileTestJava
Copy the code

If BUILD SUCCESS is displayed, the BUILD is successful, as shown in the following figure

bulid

4. Download and install Gradle from the compiled source code

After compiling the Spring-OXm module, a. Gradle folder is generated in the current directory. You can open it to view the corresponding gradle version.

Snipaste_2020-07-03_15-11-3

Double-click to open the gradle folder to see the gradle version number you need

Snipaste_2020-07-03_15-15-

You directly in the website version of the corresponding need to: https://gradle.org/releases/, choose the binary – only

After installing Gradle, remember to configure Gradle environment variables

  • newGRADLE_HOMEEnvironment variable pointing to the Gradle unzip directory
  • Configuring the Path environment variable: Added%GRADLE_HOME%\bin

Run the gradle -v command on the command line to check whether the version is correct.

image-20200703152417552

Configure the domestic image for the installed Gradle

Go to the Gradle installation directory, create a new init. Gradle file in the init.d directory and add the following contents:

allprojects{
    repositories {
        def REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/groups/public/'
        all { ArtifactRepository repo ->
            def url = repo.url.toString()
 if ((repo instanceof MavenArtifactRepository) && (url.startsWith('https://repo1.maven.org/maven2') || url.startsWith('https://jcenter.bintray.com'))) {  project.logger.lifecycle 'Repository ${repo.url} replaced by $REPOSITORY_URL .'  remove repo  }  }  maven {  url REPOSITORY_URL  }  } } Copy the code

As shown below:

gradle-init

6. Import the code into IDEA

6.1. Open IDEA and select import project

image-20200703154502944

6.2. Select import a Gradle project

image-20200703154634013

6.3. Configure imported items

image-20200703154919932

Click Finish and wait for IDEA to Finish building the project. If you have already configured Gradle’s domestic image according to my method, this process will not take too long. Our machine only took 10 minutes to build the project

After completion of construction, the whole project structure is as follows:

image-20200703155239583

If you’re a direct-checked-out code like me, remember to branch to 5.2.x

source-checkout

7. Build the project

Choose Build > Build Project

image-20200703162734719

Possible problems

AnnotationCacheAspect could not find the symbol

In this process you may encounter the following errors:

20190328102605453

This is because AnnotationCacheAspect.aj is nota Java file that needs to be processed by a separate AspectJ, and you can follow these steps to resolve the problem

  • Download aspectj, corresponding link: https://www.eclipse.org/downloads/download.php?file=/tools/aspectj/aspectj-1.9.5.jar

  • The installationaspectj

Open the command line, CD to the folder where the AspectJ JAR package is located, run the java-jar aspectj-1.9.4.jar command, open the AspectJ installation interface, click Next, as shown below:

image-20200703164636898

Next, select the JDK installation path and proceed to Next.

image-20200703164719216

Next, select the AspectJ installation path, and then Install.

img
  • The IDEA of configurationaspectj

Make sure the following two plug-ins are active

  1. Spring AOP/@AspectJ

  2. AspectJ Support

image-20200703165117938

Change the compiler to Ajc, then set the Ajc installation directory, select AspectJTools.jar, and be sure to check the Delegate to Javac option. This proxy setting will only do Ajc compilation for the specified project, and the rest will be compiled using the default Javac compiler. If this proxy option is not checked, all projects are compiled using the Ajc compiler, which may result in compilation errors.

image-20200703165837296
  • Specify the project to compile with Ajc

Add Facets attributes for Spring-AOP and Spring-Aspects respectively.

Go to File –> Project Structure –> Facets, select Spring-aop. main, and click OK

Go to File –> Project Structure –> Facets, select Spring-Aspects. Main, and click OK

Finish adding, as shown below:

Insert a picture description here

After completing the above steps, choose Build > Build Project again to complete the compilation successfully

8. Add test modules

8.1. Right-click project name –> new –> Module

image-20200703172538821

8.2. Select Gradle and Java

image-20200703172305000

8.3. Enter the module name

image-20200703172720394

Click Next –> Finish to create the test module

Finally, to add the necessary dependencies, modify the build.gradle file in the created module to add the following three dependencies

compile(project(":spring-aop"))
compile(project(":spring-context"))
optional("org.aspectj:aspectjweaver")
Copy the code

As shown below:

Insert a picture description here

At this point, we have compiled the entire Spring and created a module to use later!

If this article helped you, give it a thumbs up! Also welcome to pay attention to my public number, wechat search: Programmer DMZ, or scan the two-dimensional code below, follow me to learn Java seriously, do a solid coder.

The public,