首页 > 解决方案 > 如何使用变量拆分基于 kotlin 的大型 Gradle 构建文件?

问题描述

我有一个大的 gradle.build.kts 文件,我正在尝试使用 apply(from = "xxx") 将它分成更小的部分(如果存在更好的解决方案,我愿意接受)。

问题是我遇到了一个问题,我的一个脚本无法处理变量。这是示例代码:

构建.gradle.kts

plugins { java }
repositories { mavenCentral() }
dependencies { testCompile("junit", "junit", "4.12") }

println("Before Apply")
apply(from = "./build.gradle.test.kts")
println("After Apply")

build.gradle.test.kts

println("Applying")

tasks.test {
    val failedTests = mutableListOf<TestDescriptor>()
}

println("Applied")

这两个文件都在根项目文件夹中。

输出...

>gradle clean build test

> Configure project :
Before Apply

FAILURE: Build failed with an exception.

* Where:
Build file 'D:\Users\jamesdmcmillian\Projects\FacilitatorLabs\Labs\gradle-dsl-script-variables\build.gradle.kts' line: 6

* What went wrong:
Could not open cp_dsl generic class cache for script 'D:\Users\jamesdmcmillian\Projects\FacilitatorLabs\Labs\gradle-dsl-script-variables\build.gradle.test.kts' (C:\Users\jamesdmcmillian\.gradle\caches\6.8\scripts\7kclhsg56vjpfm1z90j
4hwepn).
> Could not compile script 'D:\Users\jamesdmcmillian\Projects\FacilitatorLabs\Labs\gradle-dsl-script-variables\build.gradle.test.kts'.
   > startup failed:
     script 'D:\Users\jamesdmcmillian\Projects\FacilitatorLabs\Labs\gradle-dsl-script-variables\build.gradle.test.kts': 4: expecting '}', found 'failedTests' @ line 4, column 9.
            val failedTests = mutableListOf<TestDescriptor>()
                ^

     1 error


* 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

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.8/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 4s

是否可以在应用脚本中包含变量/值?
如果不是,这似乎是一个非常糟糕的限制。

这些帖子很接近,但不回答可变问题:Is there a way to split/factor out common parts of Gradle build

另外,我已经阅读了 kotlin-dsl 入门书,但没有看到任何与此问题相关的内容,或者我可能错过了它。其余代码工作正常,所以我从这个问题中删除了它。
任何帮助表示赞赏,在此先感谢。

版本信息:
Gradle 6.8
Kotlin:1.4.20
Groovy:2.5.12
Ant:Apache Ant(TM) 版本 1.10.9 编译于 2020 年 9 月 27 日
JVM:11.0.2 (Oracle Corporation 11.0.2+9)
操作系统:Windows 10 10.0 amd64

标签: variablesgradlekotlin-dsl

解决方案


推荐阅读