1

I have a Spring Boot Java project that builds using Gradle (v6.2.2).

build.gradle.kts

plugins {
    base
    java
    id("org.springframework.boot") apply false
}

val gradleVersionProperty: String by project
val javaVersion: String by project
val springBootVersion: String by project
val springCloudStarterParentBomVersion: String by project

if (JavaVersion.current() != JavaVersion.VERSION_11) {
    throw GradleException("This build must be run with JDK 11")
} else {
    println("Building with JDK " + JavaVersion.current())
}

tasks.withType<Wrapper> {
    gradleVersion = gradleVersionProperty
    distributionType = Wrapper.DistributionType.ALL
}

allprojects {
    group = "com.meanwhile.in.hell"
    version = "$version"

    // Repos used in dependencyManagement section of settings.gradle
    repositories {
        mavenLocal()
        mavenCentral()
        maven("https://repo.spring.io/snapshot")
        maven("https://repo.spring.io/milestone")
    }
}

subprojects {

    if (!project.name.startsWith("platform")) {
        apply {
            plugin("java-library")
        }

        java.sourceCompatibility = JavaVersion.VERSION_11
        java.targetCompatibility = JavaVersion.VERSION_11

        // Change the default test logging settings
        tasks.withType<Test>() {
            useJUnitPlatform()
            testLogging {
                events = setOf(
                    org.gradle.api.tasks.testing.logging.TestLogEvent.FAILED,
                    org.gradle.api.tasks.testing.logging.TestLogEvent.PASSED,
                    org.gradle.api.tasks.testing.logging.TestLogEvent.SKIPPED
                )
                exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
            }
            enableAssertions = false
            ignoreFailures = gradle.startParameter.isContinueOnFailure

            maxParallelForks =
                (Runtime.getRuntime().availableProcessors() / 2).takeIf { it > 0 } ?: 1
        }

        dependencies {
            "api"(enforcedPlatform("org.springframework.boot:spring-boot-dependencies:$springBootVersion"))
            "api"(enforcedPlatform("org.springframework.cloud:spring-cloud-dependencies:$springCloudStarterParentBomVersion"))

            "implementation"(enforcedPlatform(project(":platform-dependencies")))

            "testCompile"("org.springframework.boot:spring-boot-starter-test") 
        }
    }
}

However, I would like to add support for Spring Boot Kotlin sub-projects within it. I have used a very simple sample project from a Kotlin-only project I have that builds fine within it. Without any changes to my root build.gradle.kts file, my current build error is:

* What went wrong:
Execution failed for task ':kotlin-sample-project:bootJar'.
> Main class name has not been configured and it could not be resolved

I have not configured the main class for any of the Java sub-projects and neither have I in my Kotlin-only other project.

My build.gradle.kts in kotlin-sample-project is very simple:

plugins {
    id("org.springframework.boot")
}

dependencies {
    implementation("org.springframework.cloud:spring-cloud-starter-gateway")
}

And my main class looks like:

src/main/kotlin/sample/KotlinSampleApplication.kts

package com.meanwhile.in.hell.kotlinsample

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

@SpringBootApplication
open class KotlinSampleApplication

fun main(args: Array<String>) {
    runApplication<KotlinSampleApplication>(*args)
}

I have tried to add the kotlin plugin, but the build fails instantly not knowing what it is.

plugins {
    base
    java
    kotlin
}

Error:

Line 9:   kotlin
          ^ Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
              public val <T : Any> Class<TypeVariable(T)>.kotlin: KClass<TypeVariable(T)> defined in kotlin.jvm

1 Answer 1

2

I have tried to add the kotlin plugin, but the build fails instantly not knowing what it is.

It should be:

build.gradle.kts:

plugins {
    kotlin("jvm").version("1.3.72")
}

dependencies {
    implementation(kotlin("stdlib-jdk8"))
}

Bot Kotlin JVM plugin should be applied and Kotlin stdlib be present on compile classpath.

Sign up to request clarification or add additional context in comments.

2 Comments

Great, thanks a lot @madhead. I was wondering, is there a reason to use stdlib-jdk8 while using JDK 11? I see there is no stdlib-jdk9/10/11.
And that's the answer. stdlib-jdk8 is the highest version on Kotlin's standard library currently. They don't have anything "newer" yet. So, if you run Java 8+ – use it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.