gradle - 使用 Kotlin DSL 进行集成测试的单独 Gradle 源集

标签 gradle kotlin gradle-kotlin-dsl

我正在开发一个在 Kotlin 中实现的 Spring Boot 应用程序,并希望迁移 Gradle 构建以使用 Gradle Kotlin DSL .
我无法弄清楚的一件事是如何设置 separate source set和我的集成测试任务。
我的源代码树如下所示:

src
├── integrationTest
│   ├── kotlin
│   └── resources
├── main
│   ├── kotlin
│   └── resources
└── test
    ├── kotlin
    └── resources
使用 Gradle 的 Groovy DSL 设置源集和任务如下:
// build.gradle
sourceSets {
    integrationTest {
        kotlin {
            compileClasspath += sourceSets.main.output + configurations.testRuntimeClasspath
            runtimeClasspath += output + compileClasspath
        }
    }
}

configurations {
    integrationTestCompile.extendsFrom testCompile
    integrationTestRuntime.extendsFrom testRuntime
}

task integrationTest(type: Test, dependsOn: []) {
    testClassesDirs = sourceSets.integrationTest.output.classesDirs
    classpath = sourceSets.integrationTest.runtimeClasspath
}
我找到了许多使用 Gradle Kotlin DSL 和其他源集的示例 - 但没有任何用于组合的示例。
任何人都可以帮忙吗?

最佳答案

以下是将 Groovy 脚本转换为 Kotlin DSL 的方法:

java {
    sourceSets {
        val integrationTest by creating {
            kotlin.apply {
                compileClasspath += sourceSets["main"].output + configurations.testRuntimeClasspath
                runtimeClasspath += output + compileClasspath
            }
        }
    }
}

configurations["integrationTestCompile"].extendsFrom(configurations["testCompile"])
configurations["integrationTestRuntime"].extendsFrom(configurations["testRuntime"])

val integrationTest by tasks.creating(Test::class) {
    val integrationTestSourceSet = java.sourceSets["integrationTest"]
    testClassesDirs = integrationTestSourceSet.output.classesDirs
    classpath = integrationTestSourceSet.runtimeClasspath
}

另见:Migrating build logic from Groovy to Kotlin Gradle 指南

关于gradle - 使用 Kotlin DSL 进行集成测试的单独 Gradle 源集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51876992/

相关文章:

java - 图像和视频捕捉,如 Snapchat/Instagram 故事

适用于Groovy DSL编译器的Gradle Kotlin DSL等效于compileJava的Args

java - 使用 Gradle Kotlin DSL 包含脚本

gradle - 如何在gradle中复制文件?

android - 使用 Eclipse Luna 和 Gradle 构建 Android 项目时发生版本冲突(2.1/2.3 每晚)

java - 使用Java OpenCV库的java.lang.UnsatisfiedLinkError `GOMP_4.0' Travis CI

android - 房间数据库在 macbook pro m1 中不起作用

android - 错误 :Conflict with dependency 'com. android.support : when update from 23. 1.1 到 23.4.0

generics - Kotlin 和 Mockito : Type mismatch when using vararg on the begin of the function

android - 插件 [id : 'dagger.hilt.android.plugin' ] was not found in any of the following sources