1

我有一个以 Kotlin 作为语言,Gradle 作为构建系统支持的 Spring Boot 应用程序。所以基本上我试图从应用程序源和依赖项中构建一个胖 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)
}

不知道我到底在哪里做错了什么。

4

1 回答 1

0

Application.kt中,您没有package声明。从文档

源文件的所有内容(例如类和函数)都包含在声明的包中。所以,在上面的例子中,全名baz()foo.bar.baz,全名Goofoo.bar.Goo

如果未指定包,则此类文件的内容属于没有名称的“默认”包。

您可以查看该build/classes目录以检查这些类被编译到哪些包中。

要解决此问题,请添加package app.mainApplication.kt文件顶部。

于 2018-03-26T14:24:51.077 回答