Setting up a Spring Boot 2 app in Kotlin 1.2.21 on Fedora 27 with IntelliJ IDEA CE 2017.3, Gradle 4.5.1, and Auto-rebuild

Install gradle. I used the super safe method:

curl -s "https://get.sdkman.io" | bash
sdk install gradle 4.5.1

MitM attacks can't happen while you're using HTTPS. Not possible. (Definitely don't run the script as root.)

Fun fact: If you've just installed gradle, are confused why nothing's working in a Spring Boot project that for some reason can't find the main class, doesn't already have gradlew, and want to try running gradle with the gradle wrapper, just run

gradle wrapper
(via https://docs.gradle.org/4.5.1/userguide/gradle_wrapper.html)

and you'll have your own ./gradlew and ./gradlew.bat files in your project directory. It probably won't solve your problem. You can try adding this to your build.gradle file, but it may or may not help:

springBoot {
  mainClassName = 'com.example.ExampleApplication'
}
Anyway, getting started with Spring Boot with Kotlin. Checkout the source for a basic Spring Boot Kotlin project: https://github.com/sdeleuze/spring-boot-kotlin-demo

Add spring-boot-devtools to the list of dependencies. (Modify the dependencies section so it looks like this:
dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("com.h2database:h2")
    compile("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    compile("org.jetbrains.kotlin:kotlin-reflect")
    compile("com.fasterxml.jackson.module:jackson-module-kotlin")
    testCompile("org.springframework.boot:spring-boot-starter-test") {
        exclude(module = "junit")
    }

    testCompile("org.junit.jupiter:junit-jupiter-api")
    testRuntime("org.junit.jupiter:junit-jupiter-engine")

    // Add this import
    compile("org.springframework.boot:spring-boot-devtools")
}

The build.gradle.kts file is written in, not Groovy, but Kotlin, using the Gradle Kotlin DSL plugin. Refer to the github repo for more documentation on using Kotlin with Gradle.

Then, add this import to the top of the build.gradle.kts file:
import org.springframework.boot.gradle.tasks.run.BootRun
And under
tasks {
...
}

Add
    withType<BootRun> {
        val sourceSets = the<JavaPluginConvention>().sourceSets
        sourceResources(sourceSets["main"])
    }

I'm not sure what that does or whether it's necessary, but it's copied from https://dzone.com/articles/continuous-auto-restart-with-spring-boot-devtools.

After that, run gradle build --continuous or grade build -t (same thing, just shorter) or ./gradlew build -t (different thing, but using gradle wrapper is recommended for some reason, so that's what I actually run) in a new terminal window (in the root directory of your project) to get automatic rebuilds running. You'll also need to run gradle bootRun or ./gradlew bootRun.

I've set up IDEA to run gradle bootRun for me by importing the project as a gradle project, choosing Edit Configuration under the Run menu, clicking the plus button to create a new Run configuration, adding my project, and putting bootRun in the Tasks box.

I ran into an issue while trying to get IDEA set up. The main error it kept giving me, after having pulled all the gradle dependencies, was not being able to find basic Java classes like Java.lang.String. That was solved by following the steps in this support forum post: https://intellij-support.jetbrains.com/hc/en-us/community/posts/206950935/comments/207150089.

Basically, make sure your SDKs are loaded properly. In my case, I had to refresh the JDK that was already detected (because I installed the JDK after installing and running IDEA). Also, make sure you have your JDK installed and not just your JRE. In Fedora, I ran dnf install java-1.8.0-openjdk-devel as specified here: http://openjdk.java.net/install/. It doesn't come with Fedora 27 by default.

No comments:

Post a Comment