簡體   English   中英

Kotlin Gradle 構建未運行

[英]Kotlin Gradle build is not running

我有一個 Spring 啟動應用程序,支持 Kotlin 作為語言,Gradle 作為構建系統。 所以基本上我試圖從應用程序源和依賴項中構建一個胖子 jar,它可以使用 Java 命令行工具運行。

Gradle 構建腳本:

apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: "kotlin-spring"
apply plugin: 'org.springframework.boot'


group 'myapp'
version '1.0'

buildscript {
    ext.kotlin_version = '1.2.21' // Required for Kotlin integration
    ext.spring_boot_version = '1.5.4.RELEASE'
    repositories {
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // Required for Kotlin integration
        classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version" // See https://kotlinlang.org/docs/reference/compiler-plugins.html#kotlin-spring-compiler-plugin
        classpath "org.springframework.boot:spring-boot-gradle-plugin:$spring_boot_version"
    }
}

sourceSets {
    main.java.srcDirs += 'src/main/kotlin/'
    test.java.srcDirs += 'src/test/kotlin/'
}


jar {
    zip64 true
    manifest {
        attributes 'Main-Class': 'app.main.ApplicationKt'
    }
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}


sourceCompatibility = 1.8

repositories {
    mavenCentral()
    jcenter()
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
    compile("org.jetbrains.kotlin:kotlin-reflect")
    compile group: 'com.typesafe', name: 'config', version: '1.3.2'
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile group: 'org.apache.camel', name: 'camel-spring-boot-starter', version: '2.20.2'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-redis', version: '2.0.0.RELEASE'

    compile group: 'org.apache.camel', name: 'camel-quartz2', version: '2.20.2'
    compile group: 'org.apache.camel', name: 'camel-http4', version: '2.20.2'
    compile group: 'org.apache.camel', name: 'camel-docker', version: '2.20.2'
    compile group: 'org.apache.camel', name: 'camel-aws', version: '2.20.2'

    compile group: 'com.jayway.jsonpath', name: 'json-path', version: '2.4.0'
    compile group: 'redis.clients', name: 'jedis', version: '2.9.0'
    compile group: 'joda-time', name: 'joda-time', version: '2.9.9'
    compile group: 'net.gpedro.integrations.slack', name: 'slack-webhook', version: '1.4.0'
    compile group: 'org.json', name: 'json', version: '20180130'
    compile group: 'org.jfree', name: 'jfreechart', version: '1.5.0'

    testCompile group: 'junit', name: 'junit', version: '4.12'
}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

如果我使用 gradle 命令運行該項目,那么它運行良好。 但是當我構建 jar 並嘗試運行它時,它會抱怨以下錯誤:-

Error: Could not find or load main class app.main.ApplicationKt

應用程序.kt:-

@SpringBootApplication
@ImportResource("classpath*:camel-context.xml")
@ComponentScan("app")
class Application

fun main(args:Array<String>){
    SpringApplication.run(Application::class.java, *args)
}

不確定我到底在哪里做錯了。

Application.kt ,您沒有package聲明。 文檔中

源文件的所有內容(例如類和函數)都包含在聲明的包中。 因此,在上面的示例中, baz()的全名是foo.bar.baz ,而Goo的全名是foo.bar.Goo

如果未指定包,則該文件的內容屬於沒有名稱的“默認”包。

您可以查看build/classes目錄以檢查將這些類編譯到哪些包中。

若要解決此問題,將package app.main添加到Application.kt文件的頂部。

出於某種原因,我遇到了同樣的錯誤,但我的 package 被聲明了。 我復制了 package 刪除行並粘貼、保存、重新加載項目,然后應用程序運行了。 我對此沒有合乎邏輯的解釋,但這就是我解決此錯誤的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM