Tomirio
Tomirio

Reputation: 149

Import of external class in Kotlin Gradle Script not found

In my build.gradle.kts, I want to write a function that uses an external class: StrSubstitutor from Apach Commons Text. However, the import is not found, although I can see the library when I run ./gradlew dependencies.

The build.gradle.kts file is as follows:

import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

import org.apache.commons.text.StringSubstitutor // Import not found

plugins {
    val kotlinVersion = "1.3.61"
    kotlin("jvm") version "$kotlinVersion"
    kotlin("kapt") version "$kotlinVersion"
}

group = "com.example"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.apache.commons:commons-text:1.8")

    // SourceSets
    sourceSets.main {
        withConvention(KotlinSourceSet::class) {
            kotlin.srcDirs("src/main/kotlin")
        }
    }
    sourceSets.test {
        withConvention(KotlinSourceSet::class) {
            kotlin.srcDirs("src/main/kotlin")
        }
    }

}

tasks.withType<Test> {
    useJUnitPlatform()
    testLogging {
        events("passed", "skipped", "failed")
    }
    systemProperty("spring.profiles.active", "test")
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget = "11"
    }
}

// Function that uses the import
fun getProperty(properties: Properties, propertyKey: String): String {
    // Use the import "StrSubstitutor"
    return ""
}

Is this possible with Kotlin, and if so: how?

Upvotes: 2

Views: 5280

Answers (1)

ysakhno
ysakhno

Reputation: 863

Yes, it is possible. The reason it does not work as written is because you put the dependency on the Apache Commons Text into implementation configuration of the project, not into the classpath of the build script itself. So, you basically need to introduce a buildscript block to your build.gradle.kts file. Below is an example1:

import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

import org.apache.commons.text.StringSubstitutor

// TL DR: Add this block to your build script to make the import above work
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.apache.commons:commons-text:1.8")
    }
}

tasks.register("hello") {
    doLast {
        println(StringSubstitutor.replaceSystemProperties(
            "You are running with Java \${java.version} on OS \${os.name}."))
    }
}

plugins {
    val kotlinVersion = "1.3.61"
    kotlin("jvm") version "$kotlinVersion"
    kotlin("kapt") version "$kotlinVersion"
}

group = "com.example"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11

repositories {
    mavenCentral()
}

dependencies {
    // You probably do not need this for your project, so I commented it out
//    implementation("org.apache.commons:commons-text:1.8")

    // SourceSets
    sourceSets.main {
        withConvention(KotlinSourceSet::class) {
            kotlin.srcDirs("src/main/kotlin")
        }
    }
    sourceSets.test {
        withConvention(KotlinSourceSet::class) {
            kotlin.srcDirs("src/main/kotlin")
        }
    }

}

tasks.withType<Test> {
    useJUnitPlatform()
    testLogging {
        events("passed", "skipped", "failed")
    }
    systemProperty("spring.profiles.active", "test")
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget = "11"
    }
}

Run this script with ./gradlew -q hello to check whether it works or not.

1 New task hello exists there just to demonstrate that the import works, it is not needed in the final build script that you would use in your project.

Upvotes: 5

Related Questions