Kotlin Scripting (.kts) Maven Usage


#1

Feature request, maybe not possible:

Syntax for .kts files for importing a package from a maven repository or local jar.

This would make Kotlin one of the most powerful scripting languages I can think of, as you could quickly access advanced functionality from trusted sources in a completely portable manner, as the exact version of the dependency is even specified.

Something like this:

//Pulling from a common maven repository
//Trusted repositories part of the per-computer settings, including Bintray and Maven Central by default?
using maven("com.fasterxml.jackson.core:jackson-databind:2.9.4")

//Pulling from an uncommon maven repository, not expected to be found 
//Compiler should ask if you want to include it in your trusted repositories?
using maven("lk-kotlin:lk-kotlin-utils:0.0.4", canBeFoundIn="https://dl.bintray.com/lightningkite/lk-kotlin")

//Pulling from a local jar
using jar("./local.jar")

//Importing something now works!
import com.fasterxml.jackson.databind.ObjectMapper

//...

//Later usage
println(ObjectMapper().writeValueAsString(someObject))

#2

So basically you want this: Groovy Grapes implemented in Kotlin.

It’s not a bad idea, provided the IDE will recognize dependencies declared this way. I haven’t used Grapes in a while, but from what I can remember from about a year or two ago, Intellij did not recognize Grape dependencies, so none of the nice functions (like syntax checking) worked properly.

Stil, I’m all for it. It certainly makes running scripts easier if you don’t have to download all the dependencies manually and then add them to classpath.


#3

Have a look at kscript project.


#4

@camabeh this look nice, but:

  1. It’s not part of Kotlin distribution (so not a standard).
  2. It’s designed to only wokr on *nix systems (although I guess one could try using it in Cygwin).
  3. To use it in IDE, you still need to declare dependencies in build.gradle - so in essence you have to declare dependencies twice and then make sure to keep both declarations synchronised - seems like mistakes could easily be made there.

#5

@forinil2, No you don’t use gradle… kscript --idea myscript.kts will open IDEA with temporary project (gradle project with all dependencies listed in myscript.kts so you will have autocompletion)


#6

@camabeh Which is fine if I have already declared all the dependencies in the script and will not change them.

I can easily see a situation where I update a depedency in the build script (ie to use a functionality onyl available in a newer version of some library), then forget to update it in the script and spend time wondering why it’s not working.


#7

This is awesome idea! Could you create an issue in the YouTrack or KEEP proposal on github?