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

Gradle 7.4.1 Groovy转Kotlin DSL:grafanaImplementation等价写法

Gradle Kotlin DSL中自定义SourceSet的依赖配置写法

当你在Gradle Kotlin DSL中创建自定义SourceSet后,无法像Groovy DSL那样直接使用grafanaImplementation这类动态属性,需要通过以下两种方式实现等价功能:

方式一:通过SourceSet对象直接添加依赖

利用创建好的grafanaSourceSet的implementation和runtimeOnly方法,直接关联对应依赖:

// 先创建自定义SourceSet(Gradle会自动生成对应的grafanaImplementation等配置)
val grafanaSourceSet = sourceSets.create("grafana")

dependencies {
    grafanaSourceSet.implementation("org.slf4j:slf4j-simple:1.7.36")
    grafanaSourceSet.implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    grafanaSourceSet.runtimeOnly("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
}

方式二:显式获取对应的配置对象

通过configurations获取自动生成的grafanaImplementation和grafanaRuntimeOnly配置,再添加依赖:

// 创建自定义SourceSet
val grafanaSourceSet = sourceSets.create("grafana")

dependencies {
    val grafanaImplementation by configurations
    val grafanaRuntimeOnly by configurations
    
    grafanaImplementation("org.slf4j:slf4j-simple:1.7.36")
    grafanaImplementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    grafanaRuntimeOnly("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
}

说明

Groovy DSL支持动态属性调用,允许直接使用grafanaImplementation这类命名;而Kotlin DSL是强类型语言,必须通过SourceSet的API或显式引用配置对象来操作。创建名为grafana的SourceSet时,Gradle会自动生成一系列关联配置(如grafanaImplementation、grafanaRuntimeOnly),无需手动创建这些配置。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 01:09:28