4

I'm working on a Spring Boot application implemented in Kotlin, and would like to migrate the Gradle build to use the Gradle Kotlin DSL.

The one thing I cannot figure out is how to set up a separate source set and task for my integration tests.

My source tree looks like this:

src
├── integrationTest
│   ├── kotlin
│   └── resources
├── main
│   ├── kotlin
│   └── resources
└── test
    ├── kotlin
    └── resources

And the source set and task are set up like this with Gradle's 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
}

I've found many examples for using the Gradle Kotlin DSL, and for additional source sets - but nothing for the combination.

Can anyone help?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
otto.poellath
  • 4,129
  • 6
  • 45
  • 60
  • Possible duplicate of [Integration tests with Gradle Kotlin DSL](https://stackoverflow.com/questions/52904603/integration-tests-with-gradle-kotlin-dsl) – jenglert Feb 14 '19 at 17:06

1 Answers1

3

Here's how you can translate the Groovy script to the 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
}

Also see: the Migrating build logic from Groovy to Kotlin guide by Gradle

hotkey
  • 140,743
  • 39
  • 371
  • 326