搭建本地的spring initializr服务

文章目录
  1. 准备源码
  2. 编译
  3. 运行
  4. 问题
  5. 解决方案

参考文档:

准备源码

在github上下载源码或者git clone:

1
2
git clone https://github.com/spring-io/initializr
git clone https://github.com/spring-io/start.spring.io

注意:

1、网上的老教程基本都是只下载编译 https://github.com/spring-io/initializr 上的源码,然后启动initializr-service,但是现在这个服务在0.6版之后被移除了。0.7版本后被独立成一个新项目:start.spring.io

2、如果github上下载或者clone速度太慢,可以使用gitee:https://gitee.com/czweicc/start.spring.io

编译

1、编译 initializer:

1
2
3
cd initializr-master
mvn spring-javaformat:apply
mvn clean install -Dmaven.test.skip=true

2、编译 start.spring.io:

1
2
3
cd start.spring.io-master
mvn spring-javaformat:apply
mvn clean package -Dmaven.test.skip=true

注意:

1、如果是直接下载源码编译可能会遇到报错说没有.git目录,可以在pom.xml中添加如下配置:

1
2
3
4
5
6
7
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>

2、正常编译完后可以在.m2目录下看到 initializer 项目生成的一系列 jar 包,在 start.spring.io-master 目录下能看到打包的 start-site-exec.jar 文件。

运行

1、执行 start-site-exec.jar:

1
2
cd start.spring.io-master
java -Dserver.port=9000 -jar start-site\target\start-site-exec.jar

2、在浏览器中访问: http://localhost:9000 应该可以看到 spring initializer 熟悉的界面了。

问题

1、界面上所有可选项都是灰色,console 中显示如下异常:

1
2
2021-03-06 21:41:54.785  INFO 10876 --- [nio-9000-exec-5] .s.SaganInitializrMetadataUpdateStrategy : Fetching Spring Boot metadata from https://spring.io/project_metadata/spring-boot
2021-03-06 21:42:17.686 WARN 10876 --- [nio-9000-exec-1] .s.SaganInitializrMetadataUpdateStrategy : Failed to fetch Spring Boot metadata

2、原因在于 https://spring.io/project_metadata/spring-boot 性能不稳定,有时能访问有时不能访问。如果正常访问,响应内容可能如下(依据不同版本会有变化)

1
{"id":"spring-boot","name":"Spring Boot","projectReleases":[{"version":"2.5.0-SNAPSHOT","versionDisplayName":"2.5.0-SNAPSHOT","current":false,"releaseStatus":"SNAPSHOT","snapshot":true},{"version":"2.5.0-M2","versionDisplayName":"2.5.0-M2","current":false,"releaseStatus":"PRERELEASE","snapshot":false},{"version":"2.4.4-SNAPSHOT","versionDisplayName":"2.4.4-SNAPSHOT","current":false,"releaseStatus":"SNAPSHOT","snapshot":true},{"version":"2.4.3","versionDisplayName":"2.4.3","current":true,"releaseStatus":"GENERAL_AVAILABILITY","snapshot":false},{"version":"2.3.10.BUILD-SNAPSHOT","versionDisplayName":"2.3.10.BUILD-SNAPSHOT","current":false,"releaseStatus":"SNAPSHOT","snapshot":true},{"version":"2.3.9.RELEASE","versionDisplayName":"2.3.9.RELEASE","current":false,"releaseStatus":"GENERAL_AVAILABILITY","snapshot":false},{"version":"2.2.13.RELEASE","versionDisplayName":"2.2.13.RELEASE","current":false,"releaseStatus":"GENERAL_AVAILABILITY","snapshot":false},{"version":"2.1.18.RELEASE","versionDisplayName":"2.1.18.RELEASE","current":false,"releaseStatus":"GENERAL_AVAILABILITY","snapshot":false}]}

原本自己搭建服务就是不希望受到 spring.io 的网络制约,结果服务是在本地了。meta data数据还要依赖远程……

解决方案

1、打开IDE,引入刚才下载的 initializr 项目源码。

2、进入 initializer-web 项目,修改 io.spring.initializr.web.support 包下的 类 SpringBootMetadataReader的构造函数如下:

1
2
3
4
5
6
SpringBootMetadataReader(ObjectMapper objectMapper, RestTemplate restTemplate, String url) throws IOException {
// this.content = objectMapper.readTree(restTemplate.getForObject(url,String.class));
// 直接返回响应值,不然每次都要去 https://spring.io/project_metadata/spring-boot 下载,会导致超时失败。
String json = "{\"id\":\"spring-boot\",\"name\":\"Spring Boot\",\"projectReleases\":[{\"version\":\"2.5.0-SNAPSHOT\",\"versionDisplayName\":\"2.5.0-SNAPSHOT\",\"current\":false,\"releaseStatus\":\"SNAPSHOT\",\"snapshot\":true},{\"version\":\"2.5.0-M2\",\"versionDisplayName\":\"2.5.0-M2\",\"current\":false,\"releaseStatus\":\"PRERELEASE\",\"snapshot\":false},{\"version\":\"2.4.4-SNAPSHOT\",\"versionDisplayName\":\"2.4.4-SNAPSHOT\",\"current\":false,\"releaseStatus\":\"SNAPSHOT\",\"snapshot\":true},{\"version\":\"2.4.3\",\"versionDisplayName\":\"2.4.3\",\"current\":true,\"releaseStatus\":\"GENERAL_AVAILABILITY\",\"snapshot\":false},{\"version\":\"2.3.10.BUILD-SNAPSHOT\",\"versionDisplayName\":\"2.3.10.BUILD-SNAPSHOT\",\"current\":false,\"releaseStatus\":\"SNAPSHOT\",\"snapshot\":true},{\"version\":\"2.3.9.RELEASE\",\"versionDisplayName\":\"2.3.9.RELEASE\",\"current\":false,\"releaseStatus\":\"GENERAL_AVAILABILITY\",\"snapshot\":false},{\"version\":\"2.2.13.RELEASE\",\"versionDisplayName\":\"2.2.13.RELEASE\",\"current\":false,\"releaseStatus\":\"GENERAL_AVAILABILITY\",\"snapshot\":false},{\"version\":\"2.1.18.RELEASE\",\"versionDisplayName\":\"2.1.18.RELEASE\",\"current\":false,\"releaseStatus\":\"GENERAL_AVAILABILITY\",\"snapshot\":false}]}";
this.content = objectMapper.readTree(json);
}

3、尝试再访问,速度如飞。