为现有 Java 多项目 Gradle 构建添加对 Kotlin 源的支持

Adding support for Kotlin sources to existing Java multi-project Gradle build

我有一个使用 Gradle (v6.2.2) 构建的 Spring Boot Java 项目。

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") 
        }
    }
}

但是,我想在其中添加对 Spring Boot Kotlin 子项目的支持。我使用了一个非常简单的示例项目,该示例项目来自我拥有的仅 Kotlin 项目,它在其中构建得很好。没有对我的根 build.gradle.kts 文件进行任何更改,我当前的构建错误是:

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

我还没有为任何 Java 子项目配置主 class,我也没有在我的 Kotlin-only 其他项目中配置。

我在kotlin-sample-project中的build.gradle.kts很简单:

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

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

我的主要 class 看起来像:

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)
}

我尝试添加 kotlin 插件,但构建立即失败,不知道它是什么。

plugins {
    base
    java
    kotlin
}

错误:

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

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

应该是:

build.gradle.kts:

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

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

应该应用 Bot Kotlin JVM 插件并且 Kotlin stdlib 存在于编译类路径中。