【问题标题】:Adding support for Kotlin sources to existing Java multi-project Gradle build向现有 Java 多项目 Gradle 构建添加对 Kotlin 源代码的支持
【发布时间】:2020-05-26 16:53:47
【问题描述】:

我有一个使用 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 子项目配置主类,我也没有在我的仅 Kotlin 的其他项目中。

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

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

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

我的主课看起来像:

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

【问题讨论】:

    标签: java kotlin gradle gradle-kotlin-dsl


    【解决方案1】:

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

    应该是:

    build.gradle.kts

    plugins {
        kotlin("jvm").version("1.3.72")
    }
    
    dependencies {
        implementation(kotlin("stdlib-jdk8"))
    }
    
    

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

    【讨论】:

    • 太好了,非常感谢@madhead。我想知道,在使用 JDK 11 时是否有理由使用 stdlib-jdk8?我看到没有stdlib-jdk9/10/11
    • 这就是答案。 stdlib-jdk8 是目前 Kotlin 标准库中的最高版本。他们还没有任何“更新”的东西。所以,如果你运行 Java 8+ - 使用它。
    猜你喜欢
    • 2015-12-27
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-22
    相关资源
    最近更新 更多