Start Spring Boot with Gradle


This is really simple tutorial of Spring Boot, starting with gradle.

Environment

  • gradle 2.10
  • Ubuntu 16.04.1 LTS

I used Ubuntu, but it’s the same in Mac and Windows, I think.

gradle init

With the following command, create gradle project

After that, files build.gradle, gradle, gradlew, gradlew.bat, settings.gradle are created.

Spring Boot Basic

Write basic component into build.gradle, which is created just before. The sample of build.gradle is written in Spring Boot Reference Guide 10.1.2 Gradle installation. And I write the Kotlin version below.

And, write main method. Create class Application in src/main/java/com/example/myproject/Application.java, and write below contents. The sample contents is also written in Spring Boot Reference Guide.

It is the most simple form. If you want to create Deployable War, you need some modification. Here, you can execute ./gradlew bootrun and built-in tomcat will boot but nothing happen because we don’t write any meaningful codes.

We named first class “Application”, but you can change the name to whatever you like.

In Kotlin

It’s similar in Kotlin and Spring. In this case, modify build.gradle a bit.

Application クラス (src/main/java/com/example/myproject/Application.kt) は次のようにします。

There are other pattern of Application class for Spring in Kotlin, and those are referred in Kotlin document.

The code I created this time is registered to GitHub.

This time we created extremely simple Spring Boot application.