Introduction
A .kts file is a script written in
Kotlin: a Kotlin source file whose code can
stand at the top level and is run from the top, without a main function.
Ordinary Kotlin source files, .kt, are compiled into a program or
library first.
Software
The Kotlin command-line compiler runs a script with its -script option.
Arguments for the script follow --:
kotlinc -script report.kts -- -d /path/to/folder
Gradle uses its own kinds of Kotlin script for builds.
Structure
#!/usr/bin/env kotlin
import java.io.File
val folder = File(args.firstOrNull() ?: ".")
for (entry in folder.listFiles().orEmpty()) {
println(entry.name)
}
In the grammar of the Kotlin language specification, a script is, in order:
- an optional shebang line,
#!followed by the rest of the line; - optional file annotations;
- an optional package declaration;
- import directives;
- statements, to the end of the file.
The difference from a .kt file is the last part: a .kt
file holds only declarations at the top level, while a script holds statements, such
as the loop above. The arguments given to the script are in args.
Script kinds
What a script may use is set by a script definition, and the file name extension
selects the definition. A program can define its own kinds as annotated Kotlin
classes; the compiler finds definitions in jar files on its class path, or is told
of them with -script-templates. The Kotlin documentation calls this
customisation experimental.
kotlinc -script-templates org.example.CustomScriptDefinition -script custom.script1.kts
Gradle’s build.gradle.kts and settings.gradle.kts are
separate kinds of this sort; see Gradle build
scripts.
main.kts
The Kotlin team’s scripting proposal describes a kind named
main.kts, for scripts that bring their own libraries. It adds these
file annotations:
| Annotation | Meaning |
|---|---|
@file:DependsOn | A library the script needs: the path of a jar file, or Maven coordinates |
@file:Repository | A repository to fetch Maven coordinates from; Maven Central is used by default |
@file:Import | Another script, which runs first and whose declarations the script can use |
Not described
Known, not yet covered
- Reference: the Kotlin language itself, described by the Kotlin documentation and language specification.
- Reference: writing script definitions, the compiler’s other scripting options, and the details of
main.kts, such as how it caches compiled scripts. Themain.ktsannotations above are taken from the proposal, not from a reference manual. - Viewer: there is no viewer for Kotlin scripts. A
.ktsfile opens as plain text.
Also used for
- Gradle build script (Kotlin) – Groovy or Kotlin build scripts for Java, Kotlin and Android projects.
Links
Software
- Kotlin command-line compiler – compiles Kotlin and runs scripts (Windows, macOS, Linux)
- Gradle – build tool whose build scripts can be Kotlin scripts (Windows, macOS, Linux)
Reference
- Kotlin command-line compiler – the documentation of running scripts
- Custom scripting tutorial – how to define a script kind
- Kotlin grammar – from the language specification
- Kotlin scripting support – the design proposal, including
main.kts