将自定义Kotlin Gradle插件引入Spring项目时遇类型转换错误求助
Gradle插件引入Spring项目构建报错问题解决
问题背景
- 两个关联项目:
- Kotlin Gradle插件项目:用于生成代码
- Spring Kotlin项目:需要引入上述插件,在构建阶段自动运行
- 操作流程:
- 执行
./gradlew clean publishToMavenLocal将插件发布到本地Maven仓库 - 在Spring项目的
settings.gradle.kts中配置插件仓库:pluginManagement { repositories { mavenLocal() gradlePluginPortal() } } - 执行
./gradlew clean build时触发错误:Unable to determine constructor argument #1: value org.gradle.api.tasks.Copy.() -> kotlin.Unit not assignable to type ObjectFactory.
- 执行
插件项目配置(build.gradle.kts)
plugins { id("org.jetbrains.kotlin.jvm") version "1.9.23" id("maven-publish") id("java-gradle-plugin") } group = "com.example" version = "1.0.0" repositories { mavenCentral() } dependencies { compileOnly(gradleApi()) implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.23") implementation("org.openapitools:openapi-generator-gradle-plugin:7.4.0") } kotlin { jvmToolchain(21) } gradlePlugin { plugins { create("interfaceGeneratorOpenApi") { id = "interface-generator-open-api-test" implementationClass = "com.example.plugin.openapi.OpenApiGeneratorPlugin" } } }
Spring项目配置(build.gradle.kts)
plugins { kotlin("jvm") version "1.9.23" kotlin("plugin.spring") version "1.9.23" id("interface-generator-open-api-test") version "1.0.0" id("org.springframework.boot") version "3.2.4" id("io.spring.dependency-management") version "1.1.4" } group = "com.example.be" version = "1.0.0-SNAPSHOT" java { sourceCompatibility = JavaVersion.VERSION_21 } repositories { mavenCentral() mavenLocal() } dependencies { implementation("org.springframework.boot:spring-boot-starter-web") implementation("org.springframework.boot:spring-boot-starter-validation") implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.5.0") implementation("com.fasterxml.jackson.module:jackson-module-kotlin") implementation("org.jetbrains.kotlin:kotlin-reflect") testImplementation("org.springframework.boot:spring-boot-starter-test") } tasks.withType<KotlinCompile> { kotlinOptions { freeCompilerArgs += "-Xjsr305=strict" jvmTarget = "21" } } tasks.withType<Test> { useJUnitPlatform() }
错误原因分析
这个错误核心是插件依赖冲突或任务构造逻辑错误,和重复引入Kotlin插件无关:
- 插件依赖使用
implementation会将依赖打包进插件JAR,和Spring项目中的Gradle API、其他插件类路径冲突 openapi-generator-gradle-plugin版本和项目Gradle版本可能存在兼容性问题- 插件代码中手动实例化任务时,构造函数参数传递错误(比如把Lambda传给需要
ObjectFactory的参数)
解决方案
1. 修正插件项目依赖配置
将插件项目的依赖改为compileOnly,避免打包第三方插件到自身JAR:
dependencies { compileOnly(gradleApi()) compileOnly("org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.23") compileOnly("org.openapitools:openapi-generator-gradle-plugin:7.4.0") }
2. 检查插件任务创建逻辑
确保插件中使用Gradle标准API注册任务,避免手动实例化。比如创建Copy任务的正确方式:
// 在OpenApiGeneratorPlugin中 override fun apply(project: Project) { val copyGeneratedFiles = project.tasks.register<Copy>("copyGeneratedFiles") { from("build/generated-sources/openapi") into("src/main/kotlin/com/example/api") } // 绑定到构建生命周期 project.tasks.named("compileKotlin") { dependsOn(copyGeneratedFiles) } }
3. 统一Gradle版本
确保插件项目和Spring项目使用相同的Gradle版本,修改两个项目的gradle/wrapper/gradle-wrapper.properties:
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
4. 重新发布并测试
- 执行
./gradlew clean publishToMavenLocal重新发布插件 - 在Spring项目中执行
./gradlew clean build验证问题是否解决
内容的提问来源于stack exchange,提问作者KurdTt-
相关产品推荐
相关产品推荐

