View on GitHub
// The contract a vertical offers and the code generated from it once (monorepo guide, step 4): a gRPC service in
// src/main/proto (Protobuf contracts guide, step 1), a REST API in src/main/openapi/<vertical>.yaml, or both. The jar
// carries the .proto files next to the generated classes, so a consumer imports them without compiling them again.
// An api project depends on libraries only, so it never hands a vertical's code to its consumers.
import net.ltgt.gradle.errorprone.errorprone
plugins {
id("platform.java")
`java-library`
id("com.google.protobuf")
id("org.openapi.generator")
}
val libs = the<VersionCatalogsExtension>().named("libs")
fun lib(alias: String) = libs.findLibrary(alias).get()
fun version(alias: String) = libs.findVersion(alias).get().requiredVersion
val vertical = project.name.removeSuffix("-api")
val proto = layout.projectDirectory.dir("src/main/proto")
val openapi = layout.projectDirectory.file("src/main/openapi/$vertical.yaml")
val contractPackage = "${project.group}.api.${vertical.replace("-", "")}"
dependencies {
// A consumer gets what the generated code needs, at the versions Spring Boot manages.
"api"(platform(lib("spring-boot-dependencies")))
if (proto.asFile.isDirectory) {
"api"(lib("grpc-protobuf"))
"api"(lib("grpc-stub"))
}
if (openapi.asFile.isFile) {
"api"(lib("spring-web"))
"api"(lib("spring-context"))
"api"(lib("jakarta-validation-api"))
"api"(lib("jackson-annotations"))
// The generated code marks itself with @Generated, which only the compiler reads.
"compileOnly"(lib("jakarta-annotation-api"))
}
}
configurations.configureEach {
dependencies.withType<ProjectDependency>().configureEach {
check(path == project.path || path.startsWith(":lib-")) {
"${project.path} must not depend on $path: a contract uses libraries only (monorepo guide, step 3)"
}
}
}
// protoc and the gRPC generator at the versions of the libraries, which are the ones Spring Boot manages.
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:${version("protobuf")}"
}
plugins {
create("grpc") {
artifact = "io.grpc:protoc-gen-grpc-java:${version("grpc")}"
}
}
generateProtoTasks {
all().configureEach {
plugins {
create("grpc") {
option("@generated=omit")
}
}
}
}
}
// The classes protoc writes are not @NullMarked: the build checks handwritten code and the OpenAPI models only.
tasks.withType<JavaCompile>().configureEach {
options.errorprone.excludedPaths = ".*/build/generated/sources?/proto/.*"
}
if (openapi.asFile.isFile) {
openApiValidate {
inputSpec = openapi
}
openApiGenerate {
generatorName = "spring"
library = "spring-http-interface"
inputSpec = openapi
outputDir = layout.buildDirectory.dir("generated/openapi")
apiPackage = contractPackage
modelPackage = "$contractPackage.model"
// The interfaces, the models and their package-info.java; no Spring configuration class.
globalProperties = mapOf("apis" to "", "models" to "", "supportingFiles" to "package-info.java")
configOptions = mapOf(
"useSpringBoot4" to "true",
"useTags" to "true",
"useBeanValidation" to "true",
"openApiNullable" to "false",
"useJspecify" to "true",
"documentationProvider" to "none",
"annotationLibrary" to "none",
"hideGenerationTimestamp" to "true",
"sourceFolder" to "",
)
}
sourceSets.main {
java.srcDir(tasks.openApiGenerate.map { it.outputDir.get() })
}
tasks.compileJava {
dependsOn(tasks.openApiGenerate)
}
tasks.check {
dependsOn(tasks.openApiValidate)
}
}