Spring Framework 5.3.29 source code compilation environment construction


Preparation

  • git
    • Add the directory where git.exe is located to Path
  • gradle
    • Add the environment variable GRADLE_USER_HOME and specify it to a custom directory for storing Gradle's local warehouse and Gradle Wrapper's cache files
      • By default, the $USER_HOME/.gradle directory is created and stores these contents
    • Add the environment variable M2_HOME to point to the Maven installation directory
      • Gradle's strategy priority for finding Maven local warehouses is as follows. Confirm the search path and ensure that the warehouse mavenLocal() is valid.
      • USER_HOME/.m2/settings.xml >> M2_HOME/conf/settings.xml >> USER_HOME/.m2/repository
  • Idea, File | Settings | Build, Execution, Deployment | Build Tools | Gradle, modify the value of Gradle user home to be the same as the value of the environment variable GRADLE_USER_HOME
  • JDK 8 Update 60 or above can build Spring 5.3.29
    • Select the appropriate SDK and appropriate Language Level in the Project Structure
    • Select Project SDK in Gradle JVM in File | Settings | Build, Execution, Deployment | Build Tools | Gradle
    • Select JDK 11 and above. Warnings prompted during compilation will interrupt the compilation process. Usually warnings should not interrupt. I don’t know where to configure it.

Check out code

GtiHub Spring Framework

As of 20230821, the latest official version of Spring 5 is 5.3.29

Use Git to pull the code from [email protected]:spring-projects/spring-framework.git . The folder name is spring . Use the command git checkout -b 5.3.29 v5.3.29to check out Tag v5.3.29 as branch 5.3.29.

Modify the content of build.gradle in the root directory and add the Maven local warehouse and Alibaba Cloud warehouse to the repositories node. The build speed may be faster.

		repositories {
			mavenLocal()
			maven {url 'https://maven.aliyun.com/repository/public'}
			mavenCentral()
			maven { url "https://repo.spring.io/libs-spring-framework-build" }
		}

Import wizard

The source code comes with a description file for importing Idea, import-into-idea.md . The general meaning is as follows

  • Go to the source code root directory
  • Execute gradlew :spring-oxm:compileTestJavacommand to precompile spring-oxmmodule
  • 源码导入 Idea, File -> New -> Project from Existing Sources -> Navigate to directory -> Select build.gradle
  • Exclude spring-aspectsmodules, File-> Project Structure -> Modules
  • Gone

Precompiled

Open the terminal in the source code folder and execute gradlew :spring-oxm:compileTestJava. It will automatically download gradle-xxx-bin.zip from https://services.gradle.org/distributions/gradle-xxx-bin.zip and some dependencies. If the network is not available, An error may be reported. If everything goes well, the following content will be displayed after the build is successful.

Insert image description here

Import Idea

First open a project, then click File -> New -> Project from Existing Sources -> find the build.gradle file , click OK, Idea will automatically start Gradle Build, download many dependencies, and wait for it to complete. If everything goes well, the build will be successful. The following content will probably be displayed after

Starting Gradle Daemon...
Gradle Daemon started in 2 s 357 ms
> Task :buildSrc:compileJava UP-TO-DATE
> Task :buildSrc:compileGroovy NO-SOURCE
> Task :buildSrc:pluginDescriptors UP-TO-DATE
> Task :buildSrc:processResources UP-TO-DATE
> Task :buildSrc:classes UP-TO-DATE
> Task :buildSrc:jar UP-TO-DATE
> Task :buildSrc:assemble UP-TO-DATE
> Task :buildSrc:pluginUnderTestMetadata UP-TO-DATE
> Task :buildSrc:compileTestJava NO-SOURCE
> Task :buildSrc:compileTestGroovy NO-SOURCE
> Task :buildSrc:processTestResources NO-SOURCE
> Task :buildSrc:testClasses UP-TO-DATE
> Task :buildSrc:test NO-SOURCE
> Task :buildSrc:validatePlugins UP-TO-DATE
> Task :buildSrc:check UP-TO-DATE
> Task :buildSrc:build UP-TO-DATE
> Task :prepareKotlinBuildScriptModel UP-TO-DATE

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.5.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 34s
6 actionable tasks: 6 up-to-date

A build scan was not published as you have not authenticated with server 'ge.spring.io'.
For more information, please see https://gradle.com/help/gradle-authenticating-with-gradle-enterprise.
> Task :prepareKotlinBuildScriptModel UP-TO-DATE

BUILD SUCCESSFUL in 1s

If something goes wrong, you can reload the Gradle project just like refreshing Maven, and view the build output in the Build tool window.

After the build is completed, click File - Project Structure - Modules and remove the submodule spring-aspects

New module

Click on the project to create a new Gradle module for testing and debugging

Insert image description here

Idea will automatically create a Main class. Clicking Run will automatically start the Gradle build, and then it will run successfully and output Hello World!

package com.coder;

public class Main {
    
    
	public static void main(String[] args) {
    
    
		System.out.println("Hello world!");
	}
}

In order to use Spring's related functions, you need to add a reference to spring-context in the dependencies of build.gradle / build.gradle.kts in this module , and then refresh the Gradle configurationimplementation(project(":spring-context"))

Add the following configuration class. Clicking Run will automatically start the Gradle build. It will then run successfully and the demo will be output , indicating that the Spring function is introduced normally.

@Configuration
public class Application {
    
    

	@Bean
	public String string() {
    
    
		return "demo";
	}

	public static void main(String[] args) {
    
    
		AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(Application.class);
		Object bean = ac.getBean("string");
		System.out.println(bean);
	}

}

At this point, all the operations in the import-into-idea.md description are successful, and you can happily start your Spring source code journey.

It's just a bit annoying. Every time you run your own code, you have to execute a lot of Gradle's built-in Tasks. When it goes well, it takes a few seconds, but when it doesn't go well, it may take dozens of seconds. It kind of affects your mood.

Problem statement

If an error is reported during Gradle compilation and the Chinese version of the console is displayed as ������, you can click Help - Edit Custom VM Options… , add -Dfile.encoding=UTF-8, and then restart Idea.

Insert image description here

The mystery of IDEA using gradle garbled code

The following errors may occur during this period. You may not be able to tell the cause from the results of the last layer. You can look up (green box) and the problem may be clearly displayed.

Insert image description here
Insert image description here

Gradle Tasks

By default, Idea's Gradle project will use Gradle to build projects and run tasks. In this way, when building and running our own modules, we always have to wait for a bunch of Gradle tasks to be executed before it is our turn to write our own code , Gradle tasks usually take a few seconds, and occasionally they take longer, like ten or twenty seconds.

You can skip a bunch of tasks in Gradle and directly build and run the modules we created by using the following methods:

Click File - Settings - Build, Execution, Deployment - Build Tools - Gradle, Build and run usingand Run tests usingselect both optionsIntelliJ IDEA

After setting the above options, there will be a lot of strange problems when building and running the code. Some of these problems are easy to solve, and some are very troublesome. You may not be able to find them online, and you may not be able to find them. It works, but if you don’t tell something unclear, it will make the project a mess.

If you can accept that every time you build and run your own code, you have to run a bunch of Gradle tasks for a few seconds, then you don’t need to set this option, and you don’t need to solve various problems. I personally compromised, anyway, Gradle The task does not take more than a few seconds

In addition, when building using IntelliJ IDEA , you need to download some dependencies to USER_HOME/.m2 . The download process can be seen in the Build tool window. Two of the Jar packages are very large and download very slowly. You can search and download them in the Maven warehouse . Download (Thunder download is very fast), then throw it to the corresponding folder of the .m2 directory, then restart Idea and rebuild it

The following are several errors when building directly using IntelliJ IDEA. I have not solved all of these problems. These problems will not occur if you use Gradle to build.

1

C:\mrathena\develop\workspace\idea\mrathena\spring\spring-core\src\main\java\org\springframework\core\metrics\jfr\FlightRecorderStartupEvent.java:19:15
java: 程序包jdk.jfr不存在

After searching online for a long time, they all said that I need to change the version of Gradle VM to JDK 11 in Settings - Gradle, but it didn't work for me. After searching for a long time, I found an article saying that I need to change the version of Java Compiler to 11. , after I tried it, it is indeed effective. It can be seen that even if the problems seem to be the same, the actual causes are not necessarily the same. It may also be solved by others by mistake. The cause of the problem and the solution principle are definitely not clear. unclear

2

C:\mrathena\develop\workspace\idea\mrathena\spring\spring-context\src\main\java\org\springframework\context\weaving\DefaultContextLoadTimeWeaver.java:26:38
java: 找不到符号
  符号:InstrumentationSavingAgent
  位置: 程序包 org.springframework.instrument

3

Because I have decided not to configure Gradle's Build and Run, I will not continue. However, I have solved the source code problem of Spring 5.2.13 before, so you can take a look.

Spring 5.2.13 source code compilation environment construction

Submit code

When learning source code, you must make your own comments in the source code. You need to host the source code in your own Git repository.

Create a project such as spring-source-5.3.29 on the code hosting platform and get the corresponding warehouse link

Open .git\config in the source code root directory, modify the url under remote to be the warehouse address and save it

Execute the commit and push commands and push the source code to the remote warehouse.

Guess you like

Origin blog.csdn.net/mrathena/article/details/132412678