Tom*_*rio 2 gradle kotlin gradle-kotlin-dsl
在我的 中build.gradle.kts
,我想编写一个使用外部类的函数:StrSubstitutor
来自 Apach Commons Text。但是,尽管我在运行时可以看到该库,但找不到导入./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 ""
}
Run Code Online (Sandbox Code Playgroud)
Kotlin 可以做到这一点吗?如果可以:如何实现?
对的,这是可能的。它不能按编写的方式工作的原因是您将对 Apache Commons Text 的依赖项放入项目implementation
的配置中,而不是放入构建脚本本身的配置中。因此,您基本上需要在 build.gradle.kts 文件中引入一个块。下面是一个例子1:classpath
buildscript
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 Code Online (Sandbox Code Playgroud)
运行此脚本以./gradlew -q hello
检查其是否有效。
1存在新任务hello
只是为了证明导入有效,您在项目中使用的最终构建脚本中不需要它。
归档时间: |
|
查看次数: |
4570 次 |
最近记录: |