Custom Kotlin script definitions are a powerful feature that allows developers to create specialized scripting environments with custom behavior, syntax support, and dependency management. Here's a comprehensive look at how they're created and used:
Creating Script Definitions
The foundation of custom Kotlin scripting is the script definition class. This class defines how a script type should be recognized, compiled, and executed:
@KotlinScript(
displayName = "My Script",
fileExtension = "myscr.kts",
compilationConfiguration = MyScriptCompilationConfiguration::class,
evaluationConfiguration = MyScriptEvaluationConfiguration::class
)
abstract class MyScript(project: Project, val name: String) {
fun helper1() { ... }
[@ScriptBody] abstract fun scriptBody(params...): MyReturnType
}
The @KotlinScript annotation is critical, as it provides:
- A display name for your script type
- The file extension for this script type
- References to configuration classes for compilation and evaluation[5]
The script definition also needs compilation and evaluation configurations:
object MyScriptCompilationConfiguration : ScriptCompilationConfiguration({
defaultImports("java.io.*")
providedProperties(prop1 to Int::class, prop2 to String::class)
})
object MyScriptEvaluationConfiguration : ScriptEvaluationConfiguration({
providedProperties(prop1 to 42, prop2 to "foo")
})[5]
Project Structure for Custom Scripting
A minimal Kotlin custom scripting project typically contains two parts:
- Script definition module - Contains the script definition class and configuration
- Scripting host module - Contains the application that handles script compilation and execution[1]
For the script definition module, you need these dependencies:
dependencies {
implementation("org.jetbrains.kotlin:kotlin-scripting-common")
implementation("org.jetbrains.kotlin:kotlin-scripting-jvm")
implementation("org.jetbrains.kotlin:kotlin-scripting-dependencies")
implementation("org.jetbrains.kotlin:kotlin-scripting-dependencies-maven")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.1")
}[1]
For the scripting host module:
dependencies {
implementation("org.jetbrains.kotlin:kotlin-scripting-common")
implementation("org.jetbrains.kotlin:kotlin-scripting-jvm")
implementation("org.jetbrains.kotlin:kotlin-scripting-jvm-host")
implementation(project(":script-definition")) // reference to the script definition module
}[1]
Script Discovery and Registration
For IDE and compiler recognition of your custom script definition, you need to register it through a discovery file:
- Create an empty file named after the fully qualified name of your script definition class
- Place this file in
META-INF/kotlin/script/templates/ in your JAR
- For example:
META-INF/kotlin/script/templates/org.example.MyScriptDefinition.classname[5]
This discovery mechanism enables tools like IntelliJ IDEA and the Kotlin compiler to automatically detect and use your script definition.
IDE Integration
For proper IDE support (code completion, error highlighting, etc.), several steps are required:
- Make sure the script definition JAR is in the project's classpath
- In IntelliJ IDEA, navigate to Settings → Languages & Frameworks → Kotlin → Kotlin Scripting
- Increase the priority of your custom script definition above generic .kts scripts to ensure your definition is used[2]
Sometimes IntelliJ "loses" the script definition. If this happens, rebuilding the project often makes it reappear[2].
Using Custom Scripts
Once defined, your custom scripts can be used like this:
#!/usr/bin/env kotlin
@file:Repository("https://jcenter.bintray.com")
@file:DependsOn("org.jetbrains.kotlinx:kotlinx-html-jvm:0.6.11")
import kotlinx.html.*
import my.package.*
// Custom script code here that uses both external dependencies
// and classes from your project
This script uses annotations to specify Maven repository and dependency information, allowing the script to resolve and use external libraries during execution[1].
Troubleshooting Common Issues
-
Missing IDE Recognition: If IntelliJ doesn't recognize your custom script, check that the discovery file is correctly named and placed, then try "Invalidate and Restart"[2][3]
-
Script Definition Priority: Ensure your custom script definition has higher priority than generic .kts scripts in Kotlin Scripting settings[2]
-
Classpath Issues: Make sure your script definition is on the classpath of both the IDE and the runtime environment[7]
By following these steps, you can create powerful custom scripting environments tailored to your specific needs, with full IDE support and dependency management capabilities.