[英]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.