4

How can I run Kotlin-Script (*.kts) files from within Gradle?

From the command line, I can call: kotlinc -script foo.kts

How can I do this from gradle (build.gradle)?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 1
    Both current answers to this question miss the point completely. The question is about executing .kts scripts from within gradle. Both answers are about authoring a gradle build using kotlin format. – codeape May 03 '22 at 21:57
  • See my answer here: https://stackoverflow.com/a/76505455/4161471 – aSemy Jun 19 '23 at 09:48

2 Answers2

-1

You have two options here to run a gradle build file named build.gradle.kts.

  1. Change the name of your build file in settings.gradle to build.gradle.kts: rootProject.buildFileName = 'build.gradle.kts' and run gradle <tasknames>.
  2. Run gradle -b build.gradle.kts <tasknames>.
Rene Groeschke
  • 27,999
  • 10
  • 69
  • 78
-2

Note that with version gradle 4.21 (current at the time of writing this), you do not need any "buildFileName" entry, or any other entry in settings.gradle. If there is a build.gradle.kts and no build.gradle, gradle will use build.gradle.kts by default.

However having the setting for buildFileName does allow build.gradle to be ignored. Without the setting, you have to remove (or rename) build.gradle in order to run build.gradle.kts.

If you have both a build.gradle and a build.gradle.kts you can switch between the two by commenting and un-commenting the buildFileName setting.

innov8
  • 2,093
  • 2
  • 24
  • 31