9

How can I run the Kotlin REPL in the context of my Maven project?

This works, but is ugly:

kotlinc-jvm -cp target/classes/:`ruby -e "puts Dir['target/**/*.jar'].join(':')"`

I've tried different variations on the following (after using Maven to copy the compiler JAR as a dependency), but nothing works (Error: Could not find or load main class org.jetbrains.kotlin.runner.Main):

<plugin>  
  <groupId>org.codehaus.mojo</groupId> 
  <artifactId>exec-maven-plugin</artifactId> 
  <version>1.1.1</version> 
  <executions>
    <execution>
    <goals>
      <goal>exec</goal> 
    </goals>
    </execution>
  </executions>
  <configuration>
    <executable>java</executable>
    <arguments>
      <argument>-classpath</argument>
      <classpath/>
      <argument>-classpath</argument>
      <argument>${project.basedir}/target/dependency/kotlin-compiler-1.0.0.jar</argument>
      <argument>org.jetbrains.kotlin.runner.Main</argument>
    </arguments>
  </configuration>
</plugin>
Jayson Minard
  • 84,842
  • 38
  • 184
  • 227
Jacob Brown
  • 7,221
  • 4
  • 30
  • 50

1 Answers1

5

Please try K2JVMCompiler instead, since it's currently the entrypoint for REPL in kotlin-compiler.jar:

<configuration>
    <executable>java</executable>
    <arguments>
      <argument>-classpath</argument>
      <classpath/>
      <argument>-classpath</argument>
      <argument>${project.basedir}/target/dependency/kotlin-compiler-1.0.0.jar</argument>
      <argument>org.jetbrains.kotlin.cli.jvm.K2JVMCompiler</argument>
    </arguments>
  </configuration>
Alexander Udalov
  • 31,429
  • 6
  • 80
  • 66
  • Thanks! That does let Maven find the REPL. However, the REPL can't take input and just blocks. I guess it needs to spawn a new process. [This](http://stackoverflow.com/questions/4668839/maven-and-exec-forking-a-process) might help me, but I haven't tested it yet. Alternatively, maybe there is a Kotlin-specific way to do this? I would think this would be a pretty common use case for the Kotlin REPL... – Jacob Brown Feb 24 '16 at 15:12
  • 1
    Currently not possible, but this is in the process of being worked on currently. You can expect news in a matter of weeks. I'll update the answer then. – Alexander Udalov Feb 24 '16 at 16:09
  • 1
    Was there ever any progress on this (loading REPL in Maven context)? I'm re-evaluating Kotlin for a new project after a long break and this might be a factor to consider. Thanks! – Jacob Brown Jan 04 '17 at 15:30
  • I don't think anything improved substantially since my answer, sorry for that. If you're still unable to get REPL to work, please report an issue at kotl.in/issue and we'll look into it. Thanks! – Alexander Udalov Jan 09 '17 at 09:00