Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin DSL: Import a versions.gradle.kts into another build.gradle.kts

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...

like image 627
Deglans Dalpasso Avatar asked Sep 08 '18 14:09

Deglans Dalpasso


People also ask

How to convert Gradle file to Kotlin script?

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.

What is the difference between Kotlin and groovy DSL?

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.

What is the default warning for the Kotlin Gradle plugin?

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:

How to fix unresolved references to extensions generated by Gradle Kotlin DSL?

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.


1 Answers

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.

  1. Create a buildSrc folder in the root of your project (same level as build.gradle.kts)
  2. Add a 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()
}
  1. Create a Kotlin file where you define your dependencies in src/main/kotlin inside the buildSrcfolder. 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.

like image 108
Wyko Avatar answered Oct 16 '22 19:10

Wyko