Spring Tool Suite and Kotlin

The Kotlin team have created an Eclipse plugin for working with Kotlin projects in eclipse. It is hosted at github: https://github.com/JetBrains/kotlin-eclipse. This page describes how to work with Spring Boot apps written in Kotlin in Spring Tool Suite. Currently there are a few rough edges and this should help you work through them and understand the current limitations - it is a work in progress. The instructions below should work with STS 3.7.2 and later.

Installing

The nightly builds of the Kotlin Eclipse plugin are available from this update site:
https://teamcity.jetbrains.com/guestAuth/repository/download/Kotlin_EclipsePlugin/.lastSuccessful/


Note:Currently (on Feb-18-2016) the nightly builds include some important fixes that make the STS experience smoother, so you may want to install from this update site.


The latest stable release update site is here:
https://dl.bintray.com/jetbrains/kotlin/eclipse-plugin/last/


It is also on the eclipse marketplace: https://marketplace.eclipse.org/content/kotlin-plugin-eclipse


Just install like any other plugin using the standing install dialog available via Help > Install New Software...

Creating a sample project

Spring Starter Project Wizard

With the plugin installed you can immediately start creating Kotlin projects but if you want to get going really quickly with a Spring Boot application, you might want to use the Spring Starter wizard: File > New Spring Starter Project. As you can see below it is already enabled with a Kotlin language choice:


Let's try using it now. Open the wizard, change the language to Kotlin and then press Next, on the subsequent page let's add the Web starter and press Finish:


When importing finishes, you may be left with an error on the maven pom:


Plugin execution not covered by lifecycle configuration ...

This is kotlin issue 8401. As of Feb-18-2016 it has been fixed in nightly builds (v0.6.0.v20160218) but the fix hasn't made it to the stable releases yet. You can update to a nightly build or use a quick fix for now (either the quick fix to ignore in preferences or ignore in pom would be fine, although ignoring in pom may need you to do a Maven > Update Project action on your project). With that gone the project should be building cleanly.

What about Gradle

You can select the gradle build option on the Spring Starter wizard (either the STS or Buildship Gradle), but once the project is imported you will need to manually add the kotlin nature. Select the project, right click Configure Kotlin > Add Kotlin Nature. Once that is done you will be able to work on it and run it as shown below.

Adding code

Let's create a new data class called com.example.Greeting. Use the Kotlin Class wizard accessible through File > New > Other..:


And then let's define the data class:


Use the wizard to also create a com.example.GreetingController class:


Running the app

The simplest way to run the app is to select the DemoApplication.kt file in the package explorer, right click Run As > Kotlin Application. It should then be accessible via your browser:


It is important to note that you cannot directly the use the Run As > Spring Boot App currently. That action does not have insight into the kotlin source to see the main method. But you can manually define a Spring Boot App launch configuration for the project. Just open the launch configurations dialog via the pulldown menu available next to the run button on the toolbar. Then create a new Spring Boot App launch configuration as shown below, set the project name and manually enter the Main Type - the Search button will not find it. BE AWARE that the type for our sample here is com.example.DemoApplicationKt - note that Kt suffix. If you only put com.example.DemoApplication it will not work. Although the main method is in the DemoApplication source file it is not in the DemoApplication class, it is separate and Kotlin generates a holding class for it which is the name of the file with a Kt suffix.


You can then use that launch config just like any other.



And Cloud Foundry?

It is possible to deploy from the UI to Cloud Foundry but it isn't currently as streamlined as we would like. Here is a process that works right now:


Useful Links

Download STS: https://spring.io/tools/sts/all

STS Issue tracker: https://issuetracker.springsource.com/browse/STS