Hi everybody, i have a micronaut application with ...
# server
k
Hi everybody, i have a micronaut application with kotlin and gradle kotlin dsl. I want to pass env variables, such as DB_PASS=“XXX” DB_NAME=“XXX”, such that i can access them in main function. What is the best way to do it? My initial thought was to use gradle properties (-P) but i can’t seem to get them working. Any expert who knows what to do here?
Copy code
plugins {
    id("org.jetbrains.kotlin.jvm") version "1.8.20"
    id("org.jetbrains.kotlin.plugin.allopen") version "1.8.20"
    id("com.google.devtools.ksp") version "1.8.20-1.0.10"
    id("com.github.johnrengelman.shadow") version "8.1.1"
    id("io.micronaut.application") version "4.0.0-M2"
}

version = project.properties["version"] as String? ?: "0.0.1"
group = "dk.****"

val kotlinVersion = project.properties["kotlinVersion"]
repositories {
    mavenCentral()
}

dependencies {
    implementation("io.micronaut:micronaut-jackson-databind")
    implementation("io.micronaut.kotlin:micronaut-kotlin-runtime")
    implementation("io.micronaut:micronaut-management")
    implementation("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}")
    runtimeOnly("ch.qos.logback:logback-classic")
    runtimeOnly("com.fasterxml.jackson.module:jackson-module-kotlin")
    testImplementation("io.micronaut:micronaut-http-client")
}

application {
    mainClass.set("dk.***.ApplicationKt")
}
java {
    sourceCompatibility = JavaVersion.toVersion("17")
}

tasks {
    compileKotlin {
        compilerOptions {
            jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_17)
        }
    }
    compileTestKotlin {
        compilerOptions {
            jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_17)
        }
    }
}
graalvmNative.toolchainDetection.set(false)
micronaut {
    runtime("netty")
    testRuntime("kotest5")
    processing {
        incremental(true)
        annotations("dk.***.*")
    }
}
s
System.getenv(“DB_NAME”) ?
Sorry might need more context 🙂
k
For example i compile the project using:
./gradlew assemble -Pversion="XXX" -Pdb_name="TEST" -Pdb_pass="testpass"
I pass several properties. Now i want to access these properties at runtime in code. How can i do that?
a
you need to combine two principles: 1. you can set the environment variables accessible to processes launched by Gradle Exec tasks
Copy code
// build.gradle.kts

task.test {
  environment(
    "DB_NAME" to "test-db-1"
  }
}
2. the best way (I think) to access properties in build scripts is using
project.providers.gradleProperty("db_name")
. These properties can be passed in a number of ways, including via a
-P
arg So you can combine those two:
Copy code
// build.gradle.kts

task.test {
  environment(
    "DB_NAME" to providers.gradleProperty("db_name").get()
  }
}
(
.get()
is necessary because Exec isn’t fully compatible with the provider API - you might want to replace it with
.orNull
instead, or provide some default value in the project’s
gradle.properties
)