Integrating Kotlin into Java/Scala project

Hello,

I was wondering if anyone tried using kotlin-maven-plugin in a mixed Java/Scala project? The docs only provide instructions for the Kotlin/Java case, which of course is understandable, but my use-case is a bit more complex.

I’d like to be able to call all three languages from each other, but I am not sure if this is at all possible? My current setup allows for all use-cases except calling Kotlin from Scala, because the compilation is Scala → Kotlin → Java. I haven’t explicitly tried that, but I think if I reorder Kotlin and Scala, Kotlin wouldn’t be able to call Scala.

Kotlin’s Java interop works due to the fact that Kotlin incorporates a parser for Java source code, which allows to reference declarations which haven’t been compiled yet. Kotlin doesn’t incorporate a Scala parser, so it can only invoke Scala code which has been compiled; the reverse is also true. Thus I don’t think that full three-way interop could be possible without a lot of extra work.

Thanks for confirming my expectations. I think this might be a good use-case for Kotlin though, migrating from Java/Scala or Scala only codebases.

An example of scala, java and kotlin project can be found here - lagom-openapi GitHub - taymyr/lagom-openapi: OpenAPI/Swagger module for Lagom which works on gradle and providing intelliJ tooling e.g. refactoring across all three of them.

How is this working ? or is there something I am missing here ?

This works a bit differently and is not exactly the same as described in the original question.
Here Scala is in a separate module, that means scala can call the kotlin code without problems, since it’s just using a compiled library. Kotlin on the other hand can’t use the scala code, because it’s not yet compiled and you can’t add a dependency on the scala code without getting a circular dependency structure.
The original question is whether you can use scala, kotlin and java in the same module as you can do with only kotlin and java combined. This is not possible as yole explained.

Okay sure

@skvithalani.1 Thanks a lot for the example. Strangely I found that scalatest was used for some of the testing, yet gradle scalatest plugin wasn’t used, so the gradle test task is only capable of running everything without filter. Is it a deliberate design?

There are two projects exposing Spark to Kotlin

The interop with Scala looks great.

1 Like