将 API 程序从 java 重写为 kotlin。
我在使用 openapi 生成器的构建过程中从 openAPI 规范文件生成一些模型。这些模型也在其余 api 程序内部以及 querydsl 和 jpa 存储库中使用。
build.gradle
文件:import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
buildscript {
ext{
kotlinVersion = '1.9.23'
}
}
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.3'
id "org.openapi.generator" version "7.2.0"
id 'org.jetbrains.kotlin.jvm' version "$kotlinVersion"
id 'org.jetbrains.kotlin.plugin.spring' version "$kotlinVersion"
id "org.jetbrains.kotlin.kapt" version "$kotlinVersion"
id 'jacoco'
id "com.gorylenko.gradle-git-properties" version "2.4.1"
id 'com.adarshr.test-logger' version '4.0.0'
id 'org.cyclonedx.bom' version '1.8.2'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.test'
version = '0.11.1'
description = 'Restapi'
java {
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
}
repositories {
mavenCentral()
gradlePluginPortal()
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.security:spring-security-oauth2-resource-server'
implementation 'org.springframework.security:spring-security-oauth2-jose'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.kafka:spring-kafka'
implementation 'org.jetbrains.kotlin:kotlin-reflect'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0'
implementation 'org.openapitools:jackson-databind-nullable:0.2.6'
implementation 'com.querydsl:querydsl-jpa::jakarta'
implementation 'com.querydsl:querydsl-apt'
implementation 'org.liquibase:liquibase-core'
implementation "com.google.guava:guava:32.1.2-jre"
implementation 'org.apache.tika:tika-core:2.9.0'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
annotationProcessor "com.querydsl:querydsl-apt::jakarta"
kapt "com.querydsl:querydsl-apt::jakarta"
annotationProcessor "jakarta.persistence:jakarta.persistence-api"
runtimeOnly 'org.postgresql:postgresql'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.boot:spring-boot-starter-webflux'
testImplementation 'org.springframework.security:spring-security-test'
testImplementation 'org.springframework.kafka:spring-kafka-test'
testImplementation 'org.springframework.boot:spring-boot-testcontainers'
testImplementation 'org.testcontainers:junit-jupiter'
testImplementation 'org.testcontainers:postgresql'
testImplementation 'org.testcontainers:kafka'
}
springBoot {
buildInfo {
properties {
name = 'Restella'
additional = [
'description': 'REST API project , aka the brain of extope.',
'contact.about': 'Contact Platform Development Department, Hitachi High-Tech Corporation, Tokyo',
'contact.tel': '(03)3504-7111'
]
}
}
}
tasks.withType(KotlinCompile) {
kotlinOptions {
freeCompilerArgs += '-Xjsr305=strict'
jvmTarget = '21'
}
}
openApiGenerate {
generatorName = "kotlin-spring"
library = "spring-boot"
groupId = "${project.group}"
inputSpec = "$rootDir/api/restella-openapi.yaml"
apiPackage = "${project.group}.${project.name}.generated.api"
modelPackage = "${project.group}.${project.name}.generated.model"
ignoreFileOverride = "$rootDir/.openapi-generator-ignore"
configOptions = [
interfaceOnly: "true",
useTags: "true",
documentationProvider: "springdoc",
useSpringBoot3: "true",
enumPropertyNaming: "UPPERCASE",
generatedConstructorWithRequiredArgs : "false",
additionalModelTypeAnnotations: "@lombok.AllArgsConstructor;@lombok.NoArgsConstructor"
]
}
openApiGenerators {
include = [
'stable'
]
}
sourceSets {
main {
kotlin {
srcDirs += "$buildDir/generate-resources/main/src/main/kotlin"
}
}
}
compileJava.dependsOn tasks.openApiGenerate, tasks.compileKotlin
compileKotlin.dependsOn tasks.openApiGenerate
kapt {
javacOptions {
option("querydsl.entityAccessors", true)
}
arguments {
arg("plugin", "com.querydsl.apt.jpa.JPAAnnotationProcessor")
}
}
tasks.named('test') {
useJUnitPlatform()
}
运行时出现以下错误
gradle build
> Task :openApiGenerate FAILED
FAILURE: Build failed with an exception.
* What went wrong:
A problem was found with the configuration of task ':openApiGenerate' (type 'GenerateTask').
- Gradle detected a problem with the following location: '/workspaces/Restella/build/generate-resources/main'.
Reason: Task ':kaptGenerateStubsKotlin' uses this output of task ':openApiGenerate' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed.
Possible solutions:
1. Declare task ':openApiGenerate' as an input of ':kaptGenerateStubsKotlin'.
2. Declare an explicit dependency on ':openApiGenerate' from ':kaptGenerateStubsKotlin' using Task#dependsOn.
3. Declare an explicit dependency on ':openApiGenerate' from ':kaptGenerateStubsKotlin' using Task#mustRunAfter.
For more information, please refer to https://docs.gradle.org/8.5/userguide/validation_problems.html#implicit_dependency in the Gradle documentation.
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.
BUILD FAILED in 1s
5 actionable tasks: 5 executed
我尝试添加 kaptGenerateStubsKotlin 的依赖项:
tasks.named('kaptGenerateStubsKotlin').configure {
dependsOn 'openApiGenerate'
}
但是我收到错误:
FAILURE: Build completed with 2 failures.
1: Task failed with an exception.
-----------
* Where:
Build file '/workspaces/restapi/build.gradle' line: 134
* What went wrong:
A problem occurred evaluating root project 'restella'.
> Task with name 'kaptGenerateStubsKotlin' not found in root project 'restella'.
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.
==============================================================================
2: Task failed with an exception.
-----------
* Where:
Build file '/workspaces/restapi/build.gradle' line: 134
* What went wrong:
A problem occurred evaluating root project 'restella'.
> Task with name 'kaptGenerateStubsKotlin' not found in root project 'restella'.
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.
==============================================================================
BUILD FAILED in 567ms
这里出了什么问题?我怎样才能更新build.gradle以使用openapi生成,querydsl,就像我使用java一样?
我承认这对我来说是个谜,为什么你的显式任务依赖性不起作用。
您可以尝试以下方法:
import org.jetbrains.kotlin.gradle.internal.KaptGenerateStubsTask
tasks.withType(KaptGenerateStubsTask).configureEach {
dependsOn 'openApiGenerate'
}
当任何此类 Kapt 任务添加到项目中时,这都会添加依赖项,因此如果找不到特定名称的 Kapt 任务也不会失败。