Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.kts script in a Gradle project

Tags:

I have a Kotlin Gradle project.

If I create a .kts file it runs in InteliJ alright except when it is in the /src/main/kotlin folder.

IDEA highlights the whole file in red. Gradle throws out compilation exception. The exception is

...src/main/kotlin/test.kts: (3, 1): Cannot access script base class 'kotlin.script.templates.standard.ScriptTemplateWithArgs'. Check your module classpath for missing or conflicting dependencies`. 

What is the problem?

My build.gradle:

plugins {
    id 'org.jetbrains.kotlin.jvm' version '1.3.0-rc-131'
}

group 'kotlin.tutorials.coroutines'
version '1.0-SNAPSHOT'

repositories {
    maven {     url 'http://dl.bintray.com/kotlin/kotlin-eap' }
    mavenCentral()
    jcenter()
    maven { url "https://dl.bintray.com/kotlin/ktor" }
}

ext.ktor_version = '1.0.0-alpha-1'
ext.coroutines_version = '0.30.2-eap13'

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
    compile "io.ktor:ktor-server-netty:$ktor_version"
    compile "ch.qos.logback:logback-classic:1.2.3"

    //KTOR features
    compile "io.ktor:ktor-jackson:$ktor_version"
    compile "io.ktor:ktor-auth:$ktor_version"
    compile "io.ktor:ktor-auth-jwt:$ktor_version"
    compile "io.ktor:ktor-freemarker:$ktor_version"
    compile "io.ktor:ktor-html-builder:$ktor_version"
}

compileKotlin.kotlinOptions.jvmTarget = "1.8"
compileTestKotlin.kotlinOptions.jvmTarget = "1.8"
like image 265
voddan Avatar asked Oct 11 '18 13:10

voddan


People also ask

How do I convert build Gradle to kts?

Because you can combine Groovy and KTS build files in a project, a simple way to start converting your project to KTS is to select a simple build file, like settings. gradle , rename it to settings. gradle. kts , and convert its contents to KTS.

What is DSL in Gradle?

Simply, it stands for 'Domain Specific Language'. IMO, in gradle context, DSL gives you a gradle specific way to form your build scripts. More precisely, it's a plugin-based build system that defines a way of setting up your build script using (mainly) building blocks defined in various plugins.

What are Gradle scripts written in?

Gradle uses Groovy language for writing scripts.

What is a Gradle script?

gradle” are scripts where one can automate the tasks. For example, the simple task to copy some files from one directory to another can be performed by the Gradle build script before the actual build process happens.


2 Answers

.kts files should go to the src/main/resources folder since src/main/kotlin is for .kt files.

Scripts are a completely different animal in this sense, and you should use something like KtsRunner to execute them.

Related question is here.

If you just want to use scripts from IDEA, then you should use Scratch files which are supported out of the box.

like image 109
Adam Arold Avatar answered Nov 15 '22 11:11

Adam Arold


The solution turned out to be very straight forward.

The compiler could not find utility classes that are usually added to any Kotlin script classpath. Adding one dependency to my build.gradle fixed it:

dependencies {
    compile "org.jetbrains.kotlin:kotlin-scripting-jvm"
}

P.S.

I created 2 tickets to improve Kotlin script support in InteliJ:

  • https://youtrack.jetbrains.com/issue/KT-27542
  • https://youtrack.jetbrains.com/issue/KT-27544

If you care about those features, please vote them up!

like image 45
voddan Avatar answered Nov 15 '22 11:11

voddan