This article list the configurations need to do in order to create spring and hibernate project based on gralde with kotlin support.

apply kotlin-spring and kotlin-jpa plugin

 dependencies {
     //...
     classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
     classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlin_version"
 }
 
 apply plugin: 'kotlin-spring'
 apply plugin: 'kotlin-jpa'

kotlin-spring plugin will add spring support, and kotlin-jpa will generate no arguments constructor for kotlin data class.

apply spring boot plugin

plugins {
    id 'org.springframework.boot' version '1.5.9.RELEASE'
}

add library dependencies

dependencies {
    // ...
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile 'org.springframework.boot:spring-boot-starter-data-jpa'
    compile 'mysql:mysql-connector-java:6.0.6'
    // ...
}

with spring boot plugin, we can omit the version number when add library dependency.

build as flat jar to deploy

Specify the main class and package all dependencies to a single jar file.

jar {
    manifest {
        attributes 'Main-Class': 'com.example.ApplicationKt'
    }

    from {
        configurations.compile.collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }
}

Here the main function is defined in file Application.kt, we specify the main class is com.example.ApplicationKt which is generated by compiler.

That is all, engjoy coding with kotlin!

Reference

  1. article from spring official blog
  2. spring boot plugin in gradle
  3. tutorial from kotlinlang.org
  4. flat jar in gradle