voddan
voddan

Reputation: 33829

.kts script in a Gradle project

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"

Upvotes: 7

Views: 5469

Answers (2)

voddan
voddan

Reputation: 33829

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:

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

Upvotes: 2

Adam Arold
Adam Arold

Reputation: 30568

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

Upvotes: 2

Related Questions