🤗 ApiHug × {Postman|Swagger|Api...} = 快↑ 准√ 省↓
- GitHub - apihug/apihug.com: All abou the Apihug
- apihug.com: 有爱,有温度,有质量,有信任
- ApiHug - API design Copilot - IntelliJ IDEs Plugin | Marketplace
ApiHug 整个工具链基于 Gradle, 使用 ApiHug 准备工作最先需要学习的就是 gradle. 工欲善其事,必先利其器
Spring 早在2020 将整个框架编译平台从maven 迁移到gradle: Migrating Spring Boot's Build to Gradleopen in new window; 来看一看spring 两个关键的项目,是如何组织自己的项目的:
- spring frameworkopen in new window spring framework 源码
- spring bootopen in new window spring boot 源码
- spring scanopen in new window 包含所有spring 编译的scan
#Spring Framework
spring scanopen in new window 找一个编译成功的项目开始分析。
项目结构:
buildSrc
framework-bom
integration-tests
spring-aop
spring-aspects
spring-beans
spring-context
spring-context-indexer
spring-context-support
spring-core
spring-core-test
spring-expression
spring-instrument
spring-jcl
spring-jdbc
spring-jms
spring-messaging
spring-orm
spring-oxm
spring-r2dbc
spring-test
spring-tx
spring-web
spring-webflux
spring-webmvc
spring-websocket
#gradle.properties
配置
org.gradle.jvmargs=-Xmx2048m
org.gradle.caching=true
org.gradle.parallel=true
#settings.gradle
配置
异构了 build.gradle
到每个项目名字 gradle 比如 spring-websocket.gradle
rootProject.name = "spring"
rootProject.children.each {project ->
project.buildFileName = "${project.name}.gradle"
}
#build.gradle
主入口
里面内容比价多, 异议分解下
- 预定义很多的 plugin,大部分 apply false, 表示根项目暂时不apply这些插件, 子项目可以引入,可以从根项目控制版本-Applying external plugins with same version to subprojectsopen in new window。
- ext 项目基本的归类,
framework-bom
比较特别, 看名字就知道她是哥bom 类库管理项目。moduleProjects
模块项目: 所有的spring-
开头项目javaProjects
java项目:剔除framework-bom
的项目
- 各个项目的配置
- 所有依赖
io.spring.dependency-management
来管理依赖 - 根项目 + java 项目的插件配置,主要是测试依赖部分配置
- java
- check style
- tool chain
- text fixture
- 所有依赖
- 模块项目, 单独引入
spring-module.gradle
配置 - jar 配置
- doc 配置
- jmh 配置
- repository 推送配置
- framework-bom
- java-platform 插件, 不需要java 插件,否则出错
- publications.gradle, 引入里面的任务
- 将所有的
moduleProjects
项目加入到 BOM 中
#Spring Boot
spring scanopen in new window 找一个编译成功的项目开始分析。
spring boot 结构相对复杂, 有193 个项目(主要在smoke test),但是一级项目没有那么多:
spring-boot-build +4
spring-boot-project +14
spring-boot
spring-boot-actuator
spring-boot-actuator-autoconfigure
spring-boot-autoconfigure
spring-boot-cli
spring-boot-dependencies
spring-boot-devtools
spring-boot-docs
spring-boot-parent
spring-boot-properties-migrator
spring-boot-starters +54
spring-boot-test
spring-boot-test-autoconfigure
spring-boot-tools +12
spring-boot-system-tests
spring-boot-deployment-tests
spring-boot-image-tests
spring-boot-tests
由于 spring boot 嵌套的项目比较深, settings.gradle
有些技巧:
file("${rootDir}/spring-boot-project/spring-boot-starters").eachDirMatch(~/spring-boot-starter.*/) {
include "spring-boot-project:spring-boot-starters:${it.name}"
}
file("${rootDir}/spring-boot-tests/spring-boot-smoke-tests").eachDirMatch(~/spring-boot-smoke-test.*/) {
include "spring-boot-tests:spring-boot-smoke-tests:${it.name}"
}
gradle.properties 配置
org.gradle.caching=true
org.gradle.parallel=true
org.gradle.jvmargs=-Xmx2g -Dfile.encoding=UTF-8
build.gradle 配置简单, 仅仅给每个项目:
- base plugin
allprojects {
group "org.springframework.boot"
}
#spring-boot-dependencies
两个非常重要的插件open in new window & 参考: BOM 版本open in new window + 版本字段open in new window 。
pugin | 文档 | 源码 | 备注 |
---|---|---|---|
org.springframework.boot | 2.7.0 | DOCopen in new window | 源码open in new window |
io.spring.dependency-management | 1.0.11.REALEASE | DOCopen in new window | 源码open in new window |
spring-boot-parent 又是一个 bom 项目:
bom{
....
}
dependencies {
api(enforcedPlatform(project(":spring-boot-project:spring-boot-dependencies")))
}
各个starter 其实是 spring-framework api bom 依赖, 比如 spring-boot-starter-data-jdbc
:
plugins {
id "org.springframework.boot.starter"
}
description = "Starter for using Spring Data JDBC"
dependencies {
api(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-jdbc"))
api("org.springframework.data:spring-data-jdbc")
}
根目录的 spring-boot-starter
:
plugins {
id "org.springframework.boot.starter"
}
description = "Core starter, including auto-configuration support, logging and YAML"
dependencies {
api(project(":spring-boot-project:spring-boot"))
api(project(":spring-boot-project:spring-boot-autoconfigure"))
api(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-logging"))
api("jakarta.annotation:jakarta.annotation-api")
api("org.springframework:spring-core")
api("org.yaml:snakeyaml")
}