Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am trying to do a simple kotlin + dropwizard application.

I followed some online info including this one 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:

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:

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

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



}

The problem...

When I do gradlew run I get this error:

  * 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

my local.yml file has this:

serviceName: Kotlin Calculator

The ironic is that when I change the : for = , my application runs.

Any idea what could be wrong with this?

like image 712
jpganz18 Avatar asked Jan 21 '26 13:01

jpganz18


1 Answers

The MyConfiguration class should have an empty constructor. Make serviceName a property.

class MyConfiguration() : Configuration(){

  @NotNull
  val serviceName: String = ""
}
like image 111
LiorH Avatar answered Jan 24 '26 18:01

LiorH



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!