定制Spring Initializr(TBC)
目录
背景#
什么是Spring Initializr#
Spring Initializr是一个用于生成Spring Boot项目的在线工具。它可以帮助开发者快速创建一个包含所需依赖和配置的Spring Boot项目骨架,从而节省了手动配置项目的时间和精力。
web界面: start.spring.io
但是每次创建项目都需要手动选择依赖和配置,比较繁琐。而且生成完成后,还需要添加git, ci/cd等配置。而这些配置对于大多数项目来说是相似的。Spring Initializr本身提供了高度的可定制化能力,可以通过定制Spring Initializr来满足特定的需求。
需求#
- 对于指定的项目类型,比如
web项目,自动添加常用的依赖,比如spring-boot-starter-web,spring-boot-starter-data-jpa等。 - 自动添加
git仓库初始化,并添加.gitignore文件。 - 自动添加
ci/cd配置文件,比如GitHub Actions的workflow文件。 - 支持多种项目类型,比如
web,cli,consumer,cronjob等。
本次探索主要针对web项目类型进行定制。
定制Spring Initializr#
生成基础项目#
访问start.spring.io,选择项目类型为Maven Project, 语言为Java, Java版本为25, Spring Boot版本为3.5.7, 项目元数据根据需要填写,选择依赖Spring Web,,点击Generate`按钮,下载生成的项目压缩包。
修改pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.spring.initializr</groupId>
<artifactId>initializr-web</artifactId>
</dependency>
<dependency>
<groupId>io.spring.initializr</groupId>
<artifactId>initializr-generator-spring</artifactId>
</dependency>
<!-- other dependencies -->
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.spring.initializr</groupId>
<artifactId>initializr-bom</artifactId>
<version>0.22.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
当前最新版本为0.22.0,可以在maven central查看最新版本。该版本并不兼容Spring Boot 4。
启动项目,访问http://localhost:8080/。
使用浏览器和命令行访问会看到不同的输出,输出内容可以对应到start.spring.io的首页内容。
$ curl http://localhost:8080
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Initializr :: http://localhost:8080
This service generates quickstart projects that can be easily customized.
Possible customizations include a project's dependencies, Java version, and
build system or build structure. See below for further details.
The services uses a HAL based hypermedia format to expose a set of resources
to interact with. If you access this root resource requesting application/json
as media type the response will contain the following links:
+-----+-------------+
| Rel | Description |
+-----+-------------+
+-----+-------------+
The URI templates take a set of parameters to customize the result of a request
to the linked resource.
+-----------------+------------------------------------------+------------------------------+
| Parameter | Description | Default value |
+-----------------+------------------------------------------+------------------------------+
| applicationName | application name | DemoApplication |
| artifactId | project coordinates (infer archive name) | demo |
| baseDir | base directory to create in the archive | no base dir |
| bootVersion | spring boot version | |
| dependencies | dependency identifiers (comma-separated) | none |
| description | project description | Demo project for Spring Boot |
| groupId | project coordinates | com.example |
| javaVersion | language level | |
| language | programming language | |
| name | project name (infer application name) | demo |
| packageName | root package | com.example.demo |
| packaging | project packaging | |
| type | project type | |
| version | project version | 0.0.1-SNAPSHOT |
+-----------------+------------------------------------------+------------------------------+
The following section has a list of supported identifiers for the comma-separated
list of "dependencies".
+----+-------------+------------------+
| Id | Description | Required version |
+----+-------------+------------------+
+----+-------------+------------------+
Examples:
To create a default demo.zip:
$ curl -G http://localhost:8080/starter.zip -o demo.zip
To create a web project using Java 11:
$ curl -G http://localhost:8080/starter.zip -d dependencies=web \
-d javaVersion=11 -o demo.zip
To create a web/data-jpa gradle project unpacked:
$ curl -G http://localhost:8080/starter.tgz -d dependencies=web,data-jpa \
-d type=gradle-project -d baseDir=my-dir | tar -xzvf -
To generate a Maven POM with war packaging:
$ curl -G http://localhost:8080/pom.xml -d packaging=war -o pom.xml
浏览器访问会输出Json格式的数据,命令行访问会输出文本格式的数据。
{
"_links": {
"dependencies": {
"href": "http://localhost:8080/dependencies{?bootVersion}",
"templated": true
}
},
"dependencies": {
"type": "hierarchical-multi-select",
"values": []
},
"type": {
"type": "action",
"values": []
},
"packaging": {
"type": "single-select",
"values": []
},
"javaVersion": {
"type": "single-select",
"values": []
},
"language": {
"type": "single-select",
"values": []
},
"bootVersion": {
"type": "single-select",
"values": []
},
"groupId": {
"type": "text",
"default": "com.example"
},
"artifactId": {
"type": "text",
"default": "demo"
},
"version": {
"type": "text",
"default": "0.0.1-SNAPSHOT"
},
"name": {
"type": "text",
"default": "demo"
},
"description": {
"type": "text",
"default": "Demo project for Spring Boot"
},
"packageName": {
"type": "text",
"default": "com.example.demo"
}
}
配置文件#
大多数配置可通过application.properties文件中以initializr为命名空间的配置项实现。可以通过override这些配置项来定制生成的项目。
由于该配置具有高度层级化特性,官方推荐使用yaml格式。
默认的配置文件为application.yml。
initializr:
boot-versions:
- id: 3.5.7
default: true
java-versions:
- id: 25
default: true
- id: 17
default: false
languages:
- id: java
default: true
group-id:
value: dev.meirong.showcase
artifact-id:
value: my-app
description:
value: Spring Boot Showcase
packagings:
- id: jar
name: Jar
default: true
types:
- id: maven-project
default: true
description: Generate a Maven based project archive
tags:
build: maven
format: project
action: /starter.zip
通过命令行访问http://localhost:8080/,可以看到配置已经生效。
The URI templates take a set of parameters to customize the result of a request
to the linked resource.
+-----------------+------------------------------------------+-----------------------------+
| Parameter | Description | Default value |
+-----------------+------------------------------------------+-----------------------------+
| applicationName | application name | DemoApplication |
| artifactId | project coordinates (infer archive name) | my-app |
| baseDir | base directory to create in the archive | no base dir |
| bootVersion | spring boot version | 3.5.7 |
| dependencies | dependency identifiers (comma-separated) | none |
| description | project description | Spring Boot Showcase |
| groupId | project coordinates | dev.meirong.showcase |
| javaVersion | language level | 25 |
| language | programming language | java |
| name | project name (infer application name) | demo |
| packageName | root package | dev.meirong.showcase.my_app |
| packaging | project packaging | jar |
| type | project type | maven-project |
| version | project version | 0.0.1-SNAPSHOT |
+-----------------+------------------------------------------+-----------------------------+
添加自定义依赖#
TODO
添加git初始化#
TODO
添加ci/cd配置文件#
TODO
添加Makefile#
TODO
生成Scaffolded项目#
curl -G http://localhost:8080/starter.zip -o demo.zip