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

时间:2020-05-26 16:53:47

标签: java kotlin gradle gradle-kotlin-dsl

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

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

build.gradle.kts

但是,我想在其中添加对Spring Boot Kotlin子项目的支持。我使用了仅来自Kotlin的项目中的一个非常简单的示例项目,该项目在其中很好地构建。对我的根* What went wrong: Execution failed for task ':kotlin-sample-project:bootJar'. > Main class name has not been configured and it could not be resolved 文件没有任何更改,我当前的构建错误是:

build.gradle.kts

我没有为任何Java子项目配置主类,也没有在仅Kotlin的其他项目中配置我的主类。

我在kotlin-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 } 插件,但是由于不知道它是什么,构建立即失败。

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

错误:

export class SearchService {

  constructor() { }

  search(): Observable<SearchService.TestSearch> {

    let index: Index<SearchService.TestSearch> = FlexSearch.create<SearchService.TestSearch>();
    index.init({
      doc: {
        id: 'id',
        field: ['title', 'value']
      }
    });

    for (let i=0;i<5;i++) {
      index.add(new SearchService.TestSearch(i, `Title ${i}`, `Value ${i}`));
    }

    let stringIndex: Index<SearchService.TestSearch> = FlexSearch.create<SearchService.TestSearch>();
    stringIndex.init({
      doc: {
        id: 'id',
        field: ['title', 'value']
      }
    });

    for (let i=0;i<5;i++) {
      stringIndex.add(i, JSON.stringify(new SearchService.TestSearch(i, `Title ${i}`, `Value ${i}`)));
    }

    console.log(index.info());
    console.log(stringIndex.info());

    console.log(index.search("Title 1"));
    console.log(stringIndex.search("Title 1"));

    return services;
  }
}
export namespace SearchService {
  export class TestSearch {
    constructor(
      id: number,
      title: string,
      value: string) { }
  }
}

1 个答案:

答案 0 :(得分:1)

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

应该是:

build.gradle.kts

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

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

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