【问题标题】:Import of external class in Kotlin Gradle Script not found未找到 Kotlin Gradle 脚本中外部类的导入
【发布时间】:2020-02-29 06:38:42
【问题描述】:

在我的build.gradle.kts 中,我想编写一个使用外部类的函数:来自 Apache 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 中是否可行,如果可以:如何实现?

【问题讨论】:

    标签: gradle kotlin gradle-kotlin-dsl


    【解决方案1】:

    是的,这是可能的。它无法按所写的那样工作的原因是因为您将 Apache Commons Text 的依赖项放入 projectimplementation 配置中,而不是放入 构建脚本classpath 中em>本身。所以,你基本上需要在你的 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 的存在只是为了证明导入工作正常,您将在项目中使用的最终构建脚本中不需要它。

    【讨论】:

      猜你喜欢
      • 2018-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-16
      • 2021-11-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多