You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在buildSrc的Gradle预编译脚本插件中复用另一个插件?

问题描述

buildSrc目录结构

└── buildSrc
    ├── build.gradle.kts
    ├── settings.gradle.kts
    └── src
       └── main
          └── kotlin
             ├── base-kotlin-project-convention.gradle.kts
             └── spring-boot-dependencies-convention.gradle.kts

脚本代码

spring-boot-dependencies-convention.gradle.kts

plugins {
    id("io.spring.dependency-management")
}

dependencyManagement {
    imports {
        mavenBom(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES)
    }
}

base-kotlin-project-convention.gradle.kts

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    kotlin("jvm")
    `spring-boot-dependencies-convention`
}


dependencies {
    testImplementation("org.junit.jupiter:junit-jupiter")
    testImplementation("io.kotest:kotest-assertions-core-jvm:5.3.2")
    testImplementation("org.mockito.kotlin:mockito-kotlin:4.0.0")
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget = "17"
    }
}

java.sourceCompatibility = JavaVersion.VERSION_17

tasks.withType<Test> {
    useJUnitPlatform()
}

报错信息

> Task :buildSrc:compilePluginsBlocks FAILED
e: /Users/user/Documents/my-project/buildSrc/build/kotlin-dsl/plugins-blocks/extracted/base-kotlin-project-convention.gradle.kts:5:5: Unresolved reference: `spring-boot-dependencies-convention`

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':buildSrc:compilePluginsBlocks'.
> Script compilation error:

    Line 5:     `spring-boot-dependencies-convention`
                ^ Unresolved reference: `spring-boot-dependencies-convention`

  1 error

疑问

是否可以在一个Gradle预编译脚本插件中复用另一个预编译脚本插件?希望模块配置时只需引用单个base-kotlin-project-convention插件,而非多个插件。


解决方案

可以实现预编译脚本插件之间的复用,但不能直接在plugins块中引用自定义的预编译脚本插件——因为plugins块仅支持声明核心插件、外部仓库提供的插件,而自定义预编译插件属于buildSrc内部产物,编译阶段无法被plugins块识别。

修改base-kotlin-project-convention.gradle.kts,将对自定义插件的引用从plugins块移到外部,用apply(plugin = "插件ID")方式应用(插件ID为脚本文件名去掉.gradle.kts后缀):

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    kotlin("jvm")
}

// 应用自定义的预编译脚本插件
apply(plugin = "spring-boot-dependencies-convention")

dependencies {
    testImplementation("org.junit.jupiter:junit-jupiter")
    testImplementation("io.kotest:kotest-assertions-core-jvm:5.3.2")
    testImplementation("org.mockito.kotlin:mockito-kotlin:4.0.0")
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget = "17"
    }
}

java.sourceCompatibility = JavaVersion.VERSION_17

tasks.withType<Test> {
    useJUnitPlatform()
}

修改后,spring-boot-dependencies-convention中的依赖管理配置会被自动应用到base-kotlin-project-convention中,后续业务模块只需引用base-kotlin-project-convention这一个插件即可获得所有配置。


内容的提问来源于stack exchange,提问作者Kirill

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 00:18:22