I wanted to achieve the same result using a Micronaut project with Gradle and Kotlin, and after extensive searching, this was the only solution I found. Now, I'd like to share my solution with others who may encounter the same problem while working with Gradle.
Define a new configuration in your build.gradle file:
configurations {
create("localRuntime")
}
Add the Micronaut Netty runtime dependency to your dependencies block:
dependencies {
...
localRuntime("io.micronaut:micronaut-http-server-netty:$micronautVersion")
}
Make sure to replace $micronautVersion with the appropriate version of Micronaut.
Create a new Gradle task to run the project locally:
tasks.register('runLocal', JavaExec) {
print(configurations.getByName("localRuntime"))
classpath = configurations.getByName("localRuntime") + sourceSets.main.runtimeClasspath
mainClass = "com.example.ApplicationKt"
args("--micronaut.runtime=netty")
}
Run the project locally using the runLocal
task:
gradle runLocal
My Application.kt
:
package com.example
import io.micronaut.runtime.Micronaut.run
fun main(args: Array<String>) {
run(*args)
}
Controller:
package com.example
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
@Controller
class ExampleController {
@Get("/hello")
fun helloWorld() = mapOf("message" to "Hello World")
}