【问题标题】:Difficulty moving Gradle configuration to an external script难以将 Gradle 配置移动到外部脚本
【发布时间】:2022-11-28 02:57:01
【问题描述】:

我正在尝试将我的 Gradle 构建脚本的某些部分移动到可以在项目之间共享的外部配置文件。这是一个例子检测插入:

当前代码

构建.gradle.kts(仅浓缩为相关部分)

plugins{
    id("io.gitlab.arturbosch.detekt").version("1.19.0-RC1")
}
...
detekt{
   ...
}

我想做什么

构建.gradle.kts

apply(File("common.gradle.kts"))

通用的.gradle.kts

plugins {
    id("io.gitlab.arturbosch.detekt").version("1.19.0-RC1")
}

detekt{
   ...
}

但是当我这样做时,我得到了这个错误:

<my_project>\common.gradle.kts:7:1: Unresolved reference: detekt

所以 plugin 部分似乎没有做任何事情。需要说明的是,此插件不需要dependencies 部分中的任何内容,它在build.gradle.kts 中工作正常,仅需plugin 声明。

为什么这不起作用?

【问题讨论】:

    标签: kotlin gradle


    【解决方案1】:

    我将它与普通的 Groovy Gradle 脚本一起使用,如下所示:

    # detekt.gradle
    apply plugin: 'io.gitlab.arturbosch.detekt'
    
    tasks.detekt.jvmTarget = "1.8"
    
    detekt {
        buildUponDefaultConfig = true
        input = files("${rootProject.projectDir}/app/src")
        config = files("${rootProject.projectDir}/detekt.yml")
    
        reports {
            html.enabled = true
            xml.enabled = true
        }
    }
    
    # build.gradle
    buildscript {
        repositories {
            google()
            mavenCentral()
        }
    
        dependencies {
            classpath libs.android.gradle
            classpath libs.kotlin.gradle
            classpath libs.detekt
        }
    }
    
    allprojects {
        repositories {
            google()
            mavenCentral()
        }
    }
    
    subprojects {
        apply from: rootProject.file('detekt.gradle')
        apply from: rootProject.file('ktlint.gradle')
        apply from: rootProject.file('spotless.gradle')
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 2016-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-31
      • 1970-01-01
      • 1970-01-01
      • 2013-09-24
      相关资源
      最近更新 更多