构建自己的 Spring Initializr 服务
Spring Initializr 是 Spring 官方提供的一个用于快速创建和初始化 Spring 项目的在线工具。它可以让开发人员选择所需的 Spring 模块、版本、语言(Java、Kotlin 或 Groovy)和构建工具(Maven 或 Gradle),并生成一个基本的项目结构。现在大多数 IDE 都对 Spring Initializr 提供了支持!
官方的 Spring Initializr 服务(start.spring.io)部署在海外,在国内访问经常出现各种连网络接失败的问题。好在 Spring Initializr 是一个开源的项目,我们可以用它来构建自己的 Spring Initializr 服务。
Spring Initializr Github 仓库:https://github.com/spring-io/start.spring.io
构建 Spring Initializr 服务
Clone 项目
需要先在机器上安装 git
软件。
git clone https://github.com/spring-io/start.spring.io.git
该仓库下有三个工程,我们只关心其中2个:
start-client
前端工程,使用 React 开发。start-site
后端工程,是一个 Spring Boot 应用。
执行构建
项目使用 maven
构建,且提供了 mvnw
(Maven Wrapper) 脚本,它是一个用于管理和运行 Maven 项目的工具。它的作用是在没有全局安装 Maven 的情况下,通过自动下载和配置特定版本的 Maven 来确保项目的构建和运行环境一致性。也就是说 不需要在本地安装 Maven,通过此脚本即可完成构建。
进入项目根目录,执行如下命令进行自动构建:
./mvnw clean package -Dmaven.test.skip=true
构建过程会比较长,因为会下载 Maven 库、Nodejs 以及项目本身所用的第三方依赖库。
当你看到类似于如下信息,则表示构建成功:
...
[INFO] --- jar:3.3.0:jar (default-jar) @ start-site ---
[INFO] Building jar: /root/start.spring.io/start-site/target/start-site.jar
[INFO]
[INFO] --- spring-boot:3.1.3:repackage (repackage) @ start-site ---
[INFO] Attaching repackaged archive /root/start.spring.io/start-site/target/start-site-exec.jar with classifier exec
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for start.spring.io 0.0.1-SNAPSHOT:
[INFO]
[INFO] start.spring.io .................................... SUCCESS [ 0.582 s]
[INFO] start.spring.io client ............................. SUCCESS [01:37 min]
[INFO] start.spring.io website ............................ SUCCESS [01:28 min]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 03:20 min
[INFO] Finished at: 2023-09-26T10:42:26+08:00
[INFO] ------------------------------------------------------------------------
构建过程会从网络下载大量的资源,如果机器的网络环境“不太好”,可能会构建失败,你可以考虑换个“构建环境”。
或者你可以直接使用下文中介绍的国内的镜像服务。
运行程序
进入 start.spring.io/start-site/target
目录,有一个可执行 jar 文件: start-site-exec.ja
,这就是最终的可执行程序。
使用 java -jar
运行它:
java -jar start-site-exec.jar
启动日志如下:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.1.3)
2023-09-26T11:07:53.929+08:00 INFO 1829073 --- [ main] io.spring.start.site.StartApplication : Starting StartApplication v0.0.1-SNAPSHOT using Java 17.0.8 with PID 1829073 (/root/start.spring.io/start-site/target/start-site-exec.jar started by root in /root/start.spring.io/start-site/target)
2023-09-26T11:07:53.940+08:00 INFO 1829073 --- [ main] io.spring.start.site.StartApplication : No active profile set, falling back to 1 default profile: "default"
2023-09-26T11:07:55.882+08:00 INFO 1829073 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2023-09-26T11:07:55.893+08:00 INFO 1829073 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2023-09-26T11:07:55.894+08:00 INFO 1829073 --- [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.12]
2023-09-26T11:07:55.965+08:00 INFO 1829073 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2023-09-26T11:07:55.966+08:00 INFO 1829073 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1923 ms
2023-09-26T11:07:56.686+08:00 INFO 1829073 --- [ main] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page: class path resource [static/index.html]
2023-09-26T11:07:57.536+08:00 INFO 1829073 --- [ main] org.ehcache.core.EhcacheManager : Cache 'initializr.metadata' created in EhcacheManager.
2023-09-26T11:07:57.552+08:00 INFO 1829073 --- [ main] org.ehcache.jsr107.Eh107CacheManager : Registering Ehcache MBean javax.cache:type=CacheConfiguration,CacheManager=urn.X-ehcache.jsr107-default-config,Cache=initializr.metadata
2023-09-26T11:07:57.553+08:00 INFO 1829073 --- [ main] org.ehcache.jsr107.Eh107CacheManager : Registering Ehcache MBean javax.cache:type=CacheStatistics,CacheManager=urn.X-ehcache.jsr107-default-config,Cache=initializr.metadata
2023-09-26T11:07:57.557+08:00 INFO 1829073 --- [ main] org.ehcache.core.EhcacheManager : Cache 'initializr.dependency-metadata' created in EhcacheManager.
2023-09-26T11:07:57.558+08:00 INFO 1829073 --- [ main] org.ehcache.jsr107.Eh107CacheManager : Registering Ehcache MBean javax.cache:type=CacheConfiguration,CacheManager=urn.X-ehcache.jsr107-default-config,Cache=initializr.dependency-metadata
2023-09-26T11:07:57.558+08:00 INFO 1829073 --- [ main] org.ehcache.jsr107.Eh107CacheManager : Registering Ehcache MBean javax.cache:type=CacheStatistics,CacheManager=urn.X-ehcache.jsr107-default-config,Cache=initializr.dependency-metadata
2023-09-26T11:07:57.561+08:00 INFO 1829073 --- [ main] org.ehcache.core.EhcacheManager : Cache 'initializr.project-resources' created in EhcacheManager.
2023-09-26T11:07:57.562+08:00 INFO 1829073 --- [ main] org.ehcache.jsr107.Eh107CacheManager : Registering Ehcache MBean javax.cache:type=CacheConfiguration,CacheManager=urn.X-ehcache.jsr107-default-config,Cache=initializr.project-resources
2023-09-26T11:07:57.562+08:00 INFO 1829073 --- [ main] org.ehcache.jsr107.Eh107CacheManager : Registering Ehcache MBean javax.cache:type=CacheStatistics,CacheManager=urn.X-ehcache.jsr107-default-config,Cache=initializr.project-resources
2023-09-26T11:07:57.564+08:00 INFO 1829073 --- [ main] org.ehcache.core.EhcacheManager : Cache 'initializr.templates' created in EhcacheManager.
2023-09-26T11:07:57.565+08:00 INFO 1829073 --- [ main] org.ehcache.jsr107.Eh107CacheManager : Registering Ehcache MBean javax.cache:type=CacheConfiguration,CacheManager=urn.X-ehcache.jsr107-default-config,Cache=initializr.templates
2023-09-26T11:07:57.565+08:00 INFO 1829073 --- [ main] org.ehcache.jsr107.Eh107CacheManager : Registering Ehcache MBean javax.cache:type=CacheStatistics,CacheManager=urn.X-ehcache.jsr107-default-config,Cache=initializr.templates
2023-09-26T11:07:57.738+08:00 INFO 1829073 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 2 endpoint(s) beneath base path '/actuator'
2023-09-26T11:07:57.807+08:00 INFO 1829073 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2023-09-26T11:07:57.821+08:00 INFO 1829073 --- [ main] io.spring.start.site.StartApplication : Started StartApplication in 4.671 seconds (process running for 5.332)
启动成功,它默认监听 8080
端口,打开浏览器访问 localhost:8080
:
现在你可以使用它来初始化你的 Spring 应用了。
使用国内的镜像服务
如上所述,如果你尝试在国内机器上构建 Spring Initializr 极有可能会因为网络原因而构建失败。
那么你可以选择使用我们提供的镜像服务 start.springboot.io,该服务部署在香港,国内访问速度极快,且紧跟官方版本更新。