简体   繁体   English

如何从build.gradle加载配置文件以用于Kotlin应用程序?

[英]how to load configuration file from build.gradle for a Kotlin application?

I am trying to do a simple kotlin + dropwizard application. 我正在尝试做一个简单的kotlin + dropwizard应用程序。

I followed some online info including this one https://dev.to/tagmg/step-by-step-guide-to-building-web-api-with-kotlin-and-dropwizard 我关注了一些在线信息,包括这一个https://dev.to/tagmg/step-by-step-guide-to-building-web-api-with-kotlin-and-dropwizard

So after some modifications I created an application with a build.gradle.kts like this: 因此,经过一些修改后,我创建了一个带有build.gradle.kts的应用程序,如下所示:

plugins {
    application
    kotlin("jvm")
    id("com.github.johnrengelman.shadow") version "4.0.3"
}

application {
    mainClassName = "path.MyApplication"
}


dependencies {
    implementation("... dependencies ...")
}

val shadowJar: ShadowJar by tasks

shadowJar.apply {
    mergeServiceFiles()
    manifest.attributes.apply {
        put("Main-Class", application.mainClassName)
    }
}

tasks.named<JavaExec>("run") {
    args("server", "local.yml")
}

At MyApplication I have something like this: 在MyApplication我有这样的事情:

class MyApplication : Application() { class MyApplication:Application(){

companion object {
    @JvmStatic fun main(args: Array<String>) {
        MyApplication().run(*args)
    }
}

override fun run(configuration: MyConfiguration, environment: Environment) {

    val myResource = MyResource() //endpoints with dropwizard
    environment.jersey().register(myResource)
    }

} 

And at MyConfiguration 在MyConfiguration

class MyConfiguration(val serviceName: String
) : Configuration(){



}

The problem... 问题...

When I do gradlew run I get this error: 当我做gradlew运行时,我收到此错误:

  * Failed to parse configuration; Cannot construct instance of `MyConfiguration` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)
 at [Source: UNKNOWN; line: -1, column: -1]

I seriously have no idea why this error or if is something on my local.yml 我真的不知道为什么这个错误或者是我的local.yml上的东西

my local.yml file has this: 我的local.yml文件有这个:

serviceName: Kotlin Calculator

The ironic is that when I change the : for = , my application runs. 具有讽刺意味的是,当我更改:for =时,我的应用程序运行。

Any idea what could be wrong with this? 知道这可能有什么问题吗?

The MyConfiguration class should have an empty constructor. MyConfiguration类应该有一个空构造函数。 Make serviceName a property. 使serviceName成为属性。

class MyConfiguration() : Configuration(){

  @NotNull
  val serviceName: String = ""
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将保护级别 build.gradle 的文件从 Gradle Groovy 迁移到 Gradle Kotlin DSL - How to migrate protect level build.gradle file from Gradle Groovy to Gradle Kotlin DSL 如何在build.gradle文件中包含其他配置文件 - How to include additional configuration file inside build.gradle file 如何创建parent build.gradle(具有通用配置)并从模块build.gradle调用它? - How to create the Parent build.gradle(having common configuration) and call it from module build.gradle? Gradle Kotlin:如何将 build.gradle 脚本的一部分从 groovy 转换为 build.gradle.kts 语法? - Gradle Kotlin : How to convert part of build.gradle script from groovy to build.gradle.kts syntaxe? 如何在本地将应用程序密钥存储在单独的文件中并从那里加载到 build.gradle 中? - How to store app keys in a separate file locally and load from there in build.gradle? 如何使用单个build.gradle配置向Kotlin多平台项目添加依赖项 - How to add dependency to kotlin multiplatform projects with single build.gradle configuration 如何从另一个 build.gradle 调用 build.gradle - How to call build.gradle from another build.gradle 我可以使用gradle连接器从build.gradle文件中获取任意配置信息吗? - Can I use the gradle connector to get arbitrary configuration information from a build.gradle file? 如何在build.gradle中更改.java文件中的某些值? - How to change some value in .java file from build.gradle? 如何从build.gradle文件中提取插件? - How to extract plugins from a build.gradle file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM