文章目录
- 环境搭建
- Spring源码构建
- 问题解决
- 问题1:fatal: not a git repository (or any of the parent directories): .git
- 问题2:Using insecure protocols with repositories, without explicit opt-in, is unsupported. Switch Maven...
- 问题3:出现 CoroutinesUtils.java:74: 警告: [deprecation] AccessibleObject中的isAccessible()已过时
- 问题4:Kotlin: warnings found and -Werror specified
- 问题5:java: 找不到符号 符号: 类 InstrumentationSavingAgent 位置: 程序包 org.spring
- 问题6:缺少 spring-cglib-repack-xxx.jar 和 spring-objenesis-repack-xxx.jar 依赖
- 问题7:程序包jdk.jfr不存在 但是其实存在
环境搭建
JDK配置
笔者这边选择jdk11,具体安装过程可以看笔者这篇文章模仿【jdk环境搭建简记】
Gradle配置
这边笔者选择的是gradle7.2,具体配置步骤如下:
- 下载gradle-7.2-bin.zip,下载地址为Gradle下载地址
- 在安装目录下,新增文件夹
.gradle
用来当做仓库,笔者这边自己选择D:\WorkAndStudy\GradleRepository
该目录下文件作为仓库。 - 在gradle-7.2\init.d 中创建一个·
init.gradle
,添加以下内容
allprojects {
repositories {
maven { url 'file:///D:/WorkAndStudy/GradleRepository'} //路径不要有空格
mavenLocal()
maven { name "Alibaba" ; url "https://maven.aliyun.com/repository/public" ; allowInsecureProtocol=true}
maven { name "Bstek" ; url "http://nexus.bsdn.org/content/groups/public/" ; allowInsecureProtocol=true }
mavenCentral()
}
buildscript {
repositories {
maven { name "Alibaba" ; url 'https://maven.aliyun.com/repository/public' ; allowInsecureProtocol=true }
maven { name "Bstek" ; url 'http://nexus.bsdn.org/content/groups/public/' ; allowInsecureProtocol=true }
maven { name "M2" ; url 'https://plugins.gradle.org/m2/' ; allowInsecureProtocol=true }
}
}
}
- 环境变量
添加GRADLE_HOME
,指向Gradle解压目录D:\Program Files\Programs\Java\gradle-7.2
添加GRADLE_USER_HOME
,指向Gradle仓库D:\WorkAndStudy\GradleRepository
配置Path
环境变量;添加%GRADLE_HOME%\bin
- 测试
Spring源码构建
1、获取源码
进入Spring源码下载地址,笔者选择的是5.3.17 release版本,点击下载。
也可以fork到自己的仓库然后Git clone下来。
2、设置idea中Gradle配置
用idea打开Spring项目后,进入到File
=》Settings
=>搜索gradle
进行配置。
3、修改Build.gradle文件
搜索repositories
buildscript {
repositories {
//新增以下3个阿里云镜像
maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'https://maven.aliyun.com/repository/spring/' }
maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter' }
mavenCentral()
maven { url "https://repo.spring.io/libs-spring-framework-build" }
maven { url "https://repo.spring.io/milestone" }
maven { url "https://repo.spring.io/plugins-release" }
}
}
4、构建
然后点击立即构建
构建成功
5、建立测试用例验证
1、通过上面步骤,我们可以建立一个测试用例来验证该源码构建是否成 步骤:【File】->【New】->【Module】
2、选择Gradle
3、下一步
4、项目结构如下:
5、修改build.gradle文件
plugins {
id 'java'
}
group 'com.study'
version '5.3.17-SNAPSHOT'
repositories {
maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}
mavenCentral()
}
dependencies {
testImplementation group: 'junit', name: 'junit', version: '4.12'
implementation(project(":spring-context"))
implementation(project(":spring-instrument"))
}
TestService代码
package com.study.service;
/**
* @Author lin
* @Create 2022/5/8 19:00
*/
public interface TestService {
public String sayHello(String name);
}
TestServiceImpl
package com.study.service.impl;
import com.study.service.TestService;
import org.springframework.stereotype.Service;
/**
* @Author lin
* @Create 2022/5/8 19:01
*/
@Service(value = "TestService")
public class TestServiceImpl implements TestService {
@Override
public String sayHello(String name) {
System.out.println("Last Dance,author:"+name);
return "";
}
}
TestConfig代码
package com.study.config;
import org.springframework.context.annotation.ComponentScan;
/**
* @Author lin
* @Create 2022/5/8 19:28
*/
@ComponentScan(value = "com.study.service")
public class TestConfig {
}
Main代码
package com.study;
import com.study.config.TestConfig;
import com.study.service.TestService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @Author lin
* @Create 2022/5/8 19:28
*/
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfig.class);
TestService testService = (TestService) context.getBean("TestService");
testService.sayHello("林北");
}
}
执行结果
问题解决
问题1:fatal: not a git repository (or any of the parent directories): .git
解决方案
将id "io.spring.ge.conventions" version "0.0.9"
注释;
问题2:Using insecure protocols with repositories, without explicit opt-in, is unsupported. Switch Maven…
解决方案
- 将maven地址url由http改为https
- 添加关键字
allowInsecureProtocol=true
maven {
name "xxx" ;
url 'xxx';
allowInsecureProtocol = true
}
问题3:出现 CoroutinesUtils.java:74: 警告: [deprecation] AccessibleObject中的isAccessible()已过时
解决方案
在 org.springframework.core.CoroutinesUtils.invokeSuspendingFunction(Method method, Object target, Object… args) 方法上加 @SuppressWarnings(“deprecation”) 注解即可。
问题4:Kotlin: warnings found and -Werror specified
解决方案
这个问题是由于Kotlin将程序中的警告变更为错误导致的问题,只需要改变一下级别即可
问题解决方案:修改成如图所示即可,删掉Weeror
问题5:java: 找不到符号 符号: 类 InstrumentationSavingAgent 位置: 程序包 org.spring
解决方案
在自己测试项目的build.gradle里加上 implementation(project(“:spring-instrument”))
问题6:缺少 spring-cglib-repack-xxx.jar 和 spring-objenesis-repack-xxx.jar 依赖
解决方案
- 在源码项目根路径下执行:gradle objenesisRepackJar、gradle cglibRepackJar。
- 在IntelliJ IDEA 的侧边工具打开 gradle,分别双击 spring-core -> Tasks -> other 下的objenesisRepackJar 和 cglibRepackJar。 以上两种方法均会在项目的
spring-core\build\libs
目录下生成所需 jar 包。
问题7:程序包jdk.jfr不存在 但是其实存在
解决方案
- jar下载不完全重新编译或清理缓存invalidate Caches
- 更改jdk版本不完全,修改 gradle 模块下的 toolchains.gradle 配置文件:
将 JavaLanguageVersion.of(8) 改为 JavaLanguageVersion.of(11)。
将sourceCompatibility = JavaVersion.VERSION_1_8 改为sourceCompatibility = JavaVersion.VERSION_11。
将 jvmTarget = '1.8' 改为 jvmTarget = '11'。