I have created a versions.gradle.kts
just like that:
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...
Just rename .gradle file to .gradle.kts file extension and Gradle will consider it as a Kotlin script. Please note that following the steps below in sequence is highly important to avoid intermediate Gradle issues.
Groovy DSL script files use the .gradle file name extension. Kotlin DSL script files use the .gradle.kts file name extension. To use the Kotlin DSL, simply name your files build.gradle.kts instead of build.gradle. The settings file, settings.gradle, can also be renamed settings.gradle.kts.
warning – the default value; the Kotlin Gradle plugin will print a warning message. error – the plugin will fail the build. ignore – the plugin will skip the check and won't produce any messages. Setting jdkHome option directly is deprecated. You can set the JDK home in the following ways:
If you apply them with apply { plugin (...) } instead, you may encounter unresolved references to the extensions generated by Gradle Kotlin DSL. To resolve that, you can comment out the erroneous usages, run the Gradle task kotlinDslAccessorsSnapshot, then uncomment the usages back and rerun the build or reimport the project into the IDE.
While I think it should be possible to import another gradle.kts
file, I couldn't get it to work properly.
However, I did manage to define my dependencies in a separate Kotlin file in the buildSrc
directory.
buildSrc
folder in the root of your project (same level as build.gradle.kts
)build.gradle.kts
in that buildSrc
folder. Here, you need to define the kotlin-dsl
plugin. You also need to define the repository where to get the plugin.plugins {
`kotlin-dsl`
}
repositories {
mavenCentral()
}
src/main/kotlin
inside the buildSrc
folder. You need to create a normal Kotlin .kt
file, not a 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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With