写在前面
环境
win10(其他应该也可),
idea2020-2(最好保持一致),
jdk8+,
maven3.5.4(最好保持一致)
其他
最好准备翻墙软件,因为可能部分依赖无法下载。
1:下载源码
从这里fork代码到自己的仓库中,fork的目的是为了可以直接修改源代码,如果发现从git下载代码太慢,也可以将代码导入到gitlab中,然后再进行下载。导入到gitlab中大概过程可以参考以下几张图:
完毕之后就可以git clone
代码了。
2:编译
2.1:切换分支
这里我们使用tag:v2.1.14.RELEASE
来操作,命令格式git checkout -b <自定义本地分支名称> <远程分支名称>
。如下命令在本地创建对应分支:
git checkout -b v2.1.14.RELEASE-local v2.1.14.RELEASE
。
2.2:配置maven3.5.4
主要是添加阿里源,如下:
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>
2.3:下载依赖
/paht/to/your3.5.4/mvn clean install -DskipTests -Pfast
如果是失败的话,就多试几次,我也是试了若干次才成功的。最后看到BUILD SUCCESS
就是成功了。
2.4:解决格式化问题
/paht/to/your3.5.4/mvn spring-javaformat:apply
这一步我没有遇到问题,遇到问题的话,也多试几次。
2.5:转换为idea项目
/paht/to/your3.5.4/mvn idea:idea
2.6:导入到idea
直接选择项目导入即可。验证是否成功可以通过执行org.springframework.boot.tests.hibernate52.Hibernate52Application
类来进行验证,如果执行成功就说明导入完毕了。
2.7:关闭格式检测
在根pom.xml中添加如下内容:
<properties>
<revision>2.1.14.RELEASE</revision>
<main.basedir>${basedir}</main.basedir>
<disable.checks>true</disable.checks> // 新添加的
</properties>
最终idea效果如下图:
3:创建MVC项目
项目通过在spring-boot-tests
中创建子模块的方式进行。
3.1:创建pom项目spring-boot-dongshidaddy-tests
鼠标选中spring-boot-tests
然后new-module
,注意选择maven
,如下图:
创建完毕后删除src
,只保留pom.xml文件。最终我的pom.xml如下,因为录入gav信息不一样,所以不一定完全相同,仅作参考:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>spring-boot-dongshidaddy-tests</artifactId>
<groupId>org.springframework.boot</groupId>
<!-- <version>2.1.14.RELEASE</version>-->
<version>${revision}</version>
<!-- <relativePath>../../pom.xml</relativePath>-->
</parent>
<artifactId>spring-boot-dongshidaddy-mvc-tests</artifactId>
<name>Spring Boot Yunai DONGSHIDADDY tests</name>
<description>测试springboot源码导入项目</description>
<properties>
<main.basedir>${basedir}/../../..</main.basedir>
</properties>
<dependencies>
<!-- Compile -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.2:创建pom项目spring-boot-dongshidaddy-tests
方法同spring-boot-dongshidaddy-tests
,但是注意保留src
,最终如下图:
pom最终如下:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>spring-boot-dongshidaddy-tests</artifactId>
<groupId>org.springframework.boot</groupId>
<!-- <version>2.1.14.RELEASE</version>-->
<version>${revision}</version>
<!-- <relativePath>../../pom.xml</relativePath>-->
</parent>
<artifactId>spring-boot-dongshidaddy-mvc-tests</artifactId>
<name>Spring Boot Yunai DONGSHIDADDY tests</name>
<description>测试springboot源码导入项目</description>
<properties>
<main.basedir>${basedir}/../../..</main.basedir>
</properties>
<dependencies>
<!-- Compile -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.3:创建main启动类
@SpringBootApplication
public class MyMvcApplication {
public static void main(String[] args) {
SpringApplication.run(MyMvcApplication.class, args);
}
}
3.4:创建controller
@Controller
@RequestMapping("/mydemo")
public class MyDemoController {
@ResponseBody
@RequestMapping("/hello")
public String hello() {
return "hi from dongshidaddy!";
}
}
3.5:启动测试
$ curl --silent http://localhost:8080/mydemo/hello
hi from dongshidaddy!