[英]Kotlin DSL: Import a versions.gradle.kts into another build.gradle.kts
I have created a versions.gradle.kts
just like that: 我已经创建了一个
versions.gradle.kts
:
object Defines {
const val kotlinVersion = "1.2.61"
const val junitVersion = "5.3.0"
}
Now I want to import and use that files like that: 现在我想导入并使用那样的文件:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
group = "io.github.deglans"
version = "0.0.1-SNAPSHOT"
plugins {
application
kotlin("jvm") version Defines.kotlinVersion
}
application {
mainClassName = "io.github.deglans.polishnotation.MainKt"
}
dependencies {
compile(kotlin("stdlib-jdk8"))
testCompile("org.junit.jupiter", "junit-jupiter-api", Defines.junitVersion)
testRuntime("org.junit.jupiter", "junit-jupiter-engine", Defines.junitVersion)
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
How can I do that? 我怎样才能做到这一点?
NOTE: I have already seen this post but it is not exactly that I search... 注意:我已经看过这篇文章,但这并不是我搜索...
While I think it should be possible to import another gradle.kts
file, I couldn't get it to work properly. 虽然我认为应该可以导入另一个
gradle.kts
文件,但我无法让它正常工作。
However, I did manage to define my dependencies in a separate Kotlin file in the buildSrc
directory. 但是,我确实设法在
buildSrc
目录中的单独Kotlin文件中定义我的依赖buildSrc
。
buildSrc
folder in the root of your project (same level as build.gradle.kts
) buildSrc
文件夹(与build.gradle.kts
相同) build.gradle.kts
in that buildSrc
folder. build.gradle.kts
在buildSrc
文件夹中。 Here, you need to define the kotlin-dsl
plugin. kotlin-dsl
插件。 You also need to define the repository where to get the plugin. plugins {
`kotlin-dsl`
}
repositories {
mavenCentral()
}
src/main/kotlin
inside the buildSrc
folder. buildSrc
文件夹中src/main/kotlin
的buildSrc
。 You need to create a normal Kotlin .kt
file, not a gradle.kts
. .kt
文件,而不是gradle.kts
。 Reimport your Gradle config and you can now use the variables you defined in your Kotlin file created in step #3 in your build.gradle.kts
. 重新导入Gradle配置,您现在可以使用在
build.gradle.kts
中步骤3中创建的Kotlin文件中定义的变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.