Running kts-script from Maven

Hello,

I would like to run kts-script from Maven.

The obvious way would be to download kotlin-compiler.x.x.x.jar and run

 kotlinc -script my-script.kts

from maven. But then I would have to locally install and keep up-to-date kotlin compiler on all the machines…

Instead it would be great to use kotlin jar from the Maven repository and call kotlin compiler using exec-maven-plugin:java maven plugin. But unfortunately, I can’t get it to work. Please see https://stackoverflow.com/questions/64871438/run-kotlib-kts-script-from-maven .

Are there other ways of achieving this goal?

Ahy help is very appreciated :slight_smile:. Thank you!

Cheers,
Boris Brodski

Perhaps you could use the script goal of the Kotlin Maven plugin, like in this example: kotlin/pom.xml at 1.4.0 · JetBrains/kotlin · GitHub
The script can be provided inline or in a file beside: kotlin/pom.xml at 1.4.0 · JetBrains/kotlin · GitHub

Thank you! This is exactly, what I have been looking for (y)

It works but it throws a WARNING with the stacktrace below. Is there any possibility to prevent the stacktrace from been printed?

Thank you!

java.lang.RuntimeException: Could not find installation home path. Please make sure bin/idea.properties is present in the installation directory.

        at com.intellij.openapi.application.PathManager.getHomePath(PathManager.java:111)
        at com.intellij.openapi.application.PathManager.getBinDirectories(PathManager.java:129)
        at com.intellij.openapi.application.PathManager.findBinFile(PathManager.java:195)
        at com.intellij.util.loader.NativeLibraryLoader.loadPlatformLibrary(NativeLibraryLoader.java:17)
        at com.intellij.openapi.util.io.win32.IdeaWin32.<clinit>(IdeaWin32.java:47)
        at com.intellij.openapi.util.io.FileSystemUtil.getMediator(FileSystemUtil.java:51)
        at com.intellij.openapi.util.io.FileSystemUtil.<clinit>(FileSystemUtil.java:46)
        at com.intellij.openapi.vfs.impl.ZipHandler.setFileAttributes(ZipHandler.java:62)
        at com.intellij.openapi.vfs.impl.ZipHandler$1.createAccessor(ZipHandler.java:44)
        at com.intellij.openapi.vfs.impl.ZipHandler$1.createAccessor(ZipHandler.java:39)
        at com.intellij.util.io.FileAccessorCache.createHandle(FileAccessorCache.java:61)
        at com.intellij.util.io.FileAccessorCache.get(FileAccessorCache.java:54)
        at com.intellij.openapi.vfs.impl.ZipHandler.getCachedZipFileHandle(ZipHandler.java:84)
        at com.intellij.openapi.vfs.impl.ZipHandler.acquireZipHandle(ZipHandler.java:131)
        at com.intellij.openapi.vfs.impl.ZipHandlerBase.createEntriesMap(ZipHandlerBase.java:47)
        at com.intellij.openapi.vfs.impl.ArchiveHandler.getEntriesMap(ArchiveHandler.java:184)
        at com.intellij.openapi.vfs.impl.jar.CoreJarHandler.<init>(CoreJarHandler.java:42)
        at com.intellij.openapi.vfs.impl.jar.CoreJarFileSystem.lambda$new$0(CoreJarFileSystem.java:33)
        at com.intellij.util.containers.ConcurrentFactoryMap$2.create(ConcurrentFactoryMap.java:176)
        at com.intellij.util.containers.ConcurrentFactoryMap.get(ConcurrentFactoryMap.java:40)
        at com.intellij.openapi.vfs.impl.jar.CoreJarFileSystem.findFileByPath(CoreJarFileSystem.java:44)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment.findJarRoot(KotlinCoreEnvironment.kt:384)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment.contentRootToVirtualFile(KotlinCoreEnvironment.kt:363)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment.access$contentRootToVirtualFile(KotlinCoreEnvironment.kt:109)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment$3.invoke(KotlinCoreEnvironment.kt:206)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment$3.invoke(KotlinCoreEnvironment.kt:109)
        at org.jetbrains.kotlin.cli.jvm.compiler.ClasspathRootsResolver.convertClasspathRoots(ClasspathRootsResolver.kt:70)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment.<init>(KotlinCoreEnvironment.kt:213)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment.<init>(KotlinCoreEnvironment.kt:109)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment$Companion.createForProduction(KotlinCoreEnvironment.kt:421)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment.createForProduction(KotlinCoreEnvironment.kt)
        at org.jetbrains.kotlin.maven.ExecuteKotlinScriptMojo.executeScriptFile(ExecuteKotlinScriptMojo.java:195)
        at org.jetbrains.kotlin.maven.ExecuteKotlinScriptMojo.executeScriptInline(ExecuteKotlinScriptMojo.java:147)
        at org.jetbrains.kotlin.maven.ExecuteKotlinScriptMojo.execute(ExecuteKotlinScriptMojo.java:129)
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
        at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)

If you have a stable reproducer, please report it to https://kotl.in/issue.

Sometimes setting idea.io.use.nio2=true system property helps, so you can try to pass it to the mvn command with -Didea.io.use.nio2=true command line parameter

This problem still happens today with Kotlin 1.7.20, along with another warning. I reported it as https://youtrack.jetbrains.com/issue/KT-54461/Warnings-and-stack-traces-when-executing-scripts-via-kotlin-maven-plugin, including a reproducer project.

export MAVEN_OPTS=-Didea.io.use.nio2=true does prevent the original exception. Thank you for the suggestion @ilya.gorbunov !

1 Like