Search code examples
gradlekotlingradle-kotlin-dsl

Separate Gradle source set for integration tests using Kotlin DSL


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?


Solution

  • 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