Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save reaperes/7b596a9c6032866f8837a00524a3fe29 to your computer and use it in GitHub Desktop.
Save reaperes/7b596a9c6032866f8837a00524a3fe29 to your computer and use it in GitHub Desktop.
buildscript {
repositories {
mavenCentral()
}
dependencies {
"classpath"(group = "org.yaml", name = "snakeyaml", version = "1.28")
}
}
flyway {
val yaml = org.yaml.snakeyaml.Yaml()
val resourceDir = sourceSets.main.get().output.resourcesDir!!
val appYaml = resourceDir.resolve("application.yaml")
val documents = yaml.loadAll(appYaml.inputStream())
val localProp = getDocumentByProfile("local", documents) as? Map<String, *>
checkNotNull(localProp) { "Can not find local profile properties in application.yaml" }
val datasourceProp = ((localProp["spring"] as? Map<String, *>)?.get("datasource") as Map<String, *>)
url = datasourceProp["url"] as String
user = datasourceProp["username"] as String
password = datasourceProp["password"] as String
}
fun getDocumentByProfile(profile: String, documents: Iterable<Any>): Any? {
return documents.firstOrNull { doc ->
val map = doc as Map<String, *>
val docProfile = (map["spring"] as? Map<String, *>)?.get("config.activate.on-profile")
docProfile == profile
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment