Hey, long time not tried Kotlin scripts, do we sti...
# scripting
r
Hey, long time not tried Kotlin scripts, do we still need kscript for basic scripts with dependencies?
m
You can do most of the things with
*.main.kts
and
@file:DependsOn
. Kscript is still useful to package your scripts if you want to distribute them
☝️ 1
r
Oh, nice. Can you define a custom maven repository? Where is the current implementation documented?
v
Copy code
@file:Repository
r
This is not up to date, nothing about *.main.kts
v
But it shows you the repository annotation
r
It still talks about making a whole gradle project, which isn’t scripting to me, a script is a self contained file with a shebang and nothing else, if I want to do a whole gradle project I’m doing a normal kotlin app, kscript was good and the *.main.kts thing seems great in that regard
v
The annotation is the same though 😉
r
I just need to see how to put credentials in my repository definition and I should be set, thanks 😄
👍 1
It does not seem to be able to find the jvm dependency of a multiplatform lib by itself but other than that, looks good to me!
Found the way to put credentials in a youtrack issue though. This needs proper documentation, but I guess it’s experimental
👌 1
v
Mind sharing the way here for people discovering this thread by search?
Ah, looking in the source it is probably just including them in the url like usual
r
Nope, it doesn’t work in the URL (for me). It relies on the normal Maven/Gradle env though, so you should probably just define the credentials in
~/.m2/settings.xml
as usual, but if you want to do the bad thing of putting them right there in the script like me, you can use the
options
parameter of the annotation, as described here: https://youtrack.jetbrains.com/issue/KT-27701#focus=Comments-27-5323682.0-0 (you can use
options
once for
Repository
annotation, don’t need to use it on each dependency if you have multiple) I recommend reading this entire issue comments thread, it’s interesting.
v
Ah, I see.
I looked at the wrong
Repostory
class
r
Right now it’s missing direct multiplatform dependency support and maybe global suspend, but other than that it seems to work great. I have an executable
.kts
file and I just need to have
kotlin
installed on my machine for it to run, nice
👌 2
m
missing direct multiplatform dependency support
I filed this issue some time ago. Doesn't look like it's coming any time soon. But adding
-jvm
usually works so at least there's an easy workaround
e
as a short-term workaround, the library can modify its POM to point to the JVM artifact so that non-GMM consumers get it (since all non-JVM consumers are going to be relying on GMM anyway, this should be safe)
something along the lines of
Copy code
publishing {
    publications {
        val jvm = getByName<MavenPublication>("jvm")
        getByName<MavenPublication>("kotlinMultiplatform") {
            pom.withXml {
                val root = asNode()
                val dependencies = ((root["dependencies"] as NodeList).firstOrNull() as Node?)?.apply {
                    for (child in children().toList()) remove(child as Node)
                } ?: root.appendNode("dependencies")
                dependencies.appendNode("dependency").apply {
                    appendNode("groupId", jvm.groupId)
                    appendNode("artifactId", jvm.artifactId)
                    appendNode("version", jvm.version)
                    appendNode("scope", "compile")
                }
            }
        }
    }
}
in its
build.gradle.kts
👍 1
m
Would be nice for the Kotlin Gradle plugin to do this actually...