在现有的Java多项目Gradle构建中增加对Kotlin源代码的支持。

问题描述 投票:1回答:1

我有一个使用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项目配置主类

我的 build.gradle.ktskotlin-sample-project 是非常简单的。

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
}

错了,我有一个Spring Boot Java项目,使用Gradle(v6.2.2)进行构建。

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个回答
1
投票

我试着添加了kotlin插件,但构建瞬间就失败了,不知道它是什么。

应该是

build.gradle.kts:

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

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

Bot Kotlin JVM插件应该被应用,Kotlin stdlib应该存在于编译classpath中。

© www.soinside.com 2019 - 2024. All rights reserved.