在 Kotlin Gradle 中导入外部 class 找不到脚本

Import of external class in Kotlin Gradle Script not found

在我的 build.gradle.kts 中,我想编写一个使用外部 class 的函数:来自 Apach Commons Text 的 StrSubstitutor。但是,找不到导入,尽管我 运行 ./gradlew dependencies.

时可以看到库

build.gradle.kts文件如下:

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 ""
}

这在 Kotlin 中可行吗?如果可行:如何实现?

是的,这是可能的。它不能像写的那样工作的原因是因为你将对 Apache Commons Text 的依赖放入 项目 implementation 配置中,而不是 classpathclasspath 构建脚本 本身。因此,您基本上需要在 build.gradle.kts 文件中引入一个 buildscript 块。下面是一个例子1:

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"
    }
}

运行 这个脚本 ./gradlew -q hello 来检查它是否有效。

1 存在新任务 hello 只是为了证明导入有效,在项目中使用的最终构建脚本中不需要它。