Kotlin Gradle构建未运行

时间:2018-03-26 09:04:07

标签: gradle kotlin spring-boot-gradle-plugin

我有一个以Kotlin作为语言支持的Spring Boot应用程序,以及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

Application.kt: -

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

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

不确定我到底在做错什么。

1 个答案:

答案 0 :(得分:0)

Application.kt中,您没有package声明。来自documentation

  

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

     

如果未指定包,则此类文件的内容属于&#34;默认&#34;包没有名字。

您可以查看build/classes目录以检查编译类的包。

要解决此问题,请将package app.main添加到Application.kt文件的顶部。