gradle - 未找到 Kotlin Gradle 脚本中导入外部类

标签 gradle kotlin gradle-kotlin-dsl

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

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 的依赖项放入项目实现配置中,而不是放入类路径构建脚本本身。因此,您基本上需要在 build.gradle.kts 文件中引入一个 buildscript block 。下面是一个示例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 的存在只是为了证明导入有效,您在项目中使用的最终构建脚本中不需要它。

关于gradle - 未找到 Kotlin Gradle 脚本中导入外部类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60459343/

相关文章:

javascript - Android Studio React Native 构建失败

gradle - 可以定义不被子项目继承的 gradle 扩展属性吗?

kotlin - Android Kotlin StringRes 数量String

android - kotlin DSL 从其他文件中检索 key

Java QueryDSL 和 Gradle Kotlin DSL

android - 在Android Studio中,我收到一条错误消息,指出 “Packages unavailable”,并且我需要的软件包不可用

java - 在gradle中使用maven-publish插件生成SHA512校验和文件

json - 使用Kotlin中的Gson在数据类中序列化Sealed类

plugins - 如何在 Intellij 15 中降级 Kotlin

kotlin - 在自定义任务的子项目中运行 gradle build(test)