Khurram Malik
06/08/2023, 9:46 AMplugins {
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.***.*")
}
}
swishy
06/08/2023, 9:48 AMswishy
06/08/2023, 9:49 AMKhurram Malik
06/08/2023, 9:50 AM./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?Adam S
06/08/2023, 10:08 AM// 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:
// 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
)