Created
April 22, 2020 15:33
-
-
Save qianyan/f1351ecc4b025fd0feb8578b9c09badd to your computer and use it in GitHub Desktop.
Replace the placeholders of config file in yaml with environment variables in gradle files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.text.SimpleTemplateEngine | |
import org.yaml.snakeyaml.Yaml | |
buildscript { | |
repositories { | |
mavenLocal() | |
mavenCentral() | |
} | |
dependencies { | |
classpath "org.yaml:snakeyaml:1.25" | |
} | |
} | |
def environment = hasProperty('env') ? env : 'local' | |
println "Current Environment: " + environment | |
def engine = new SimpleTemplateEngine() | |
def binding = [ | |
DB_USERNAME: System.getenv('DB_USERNAME') ?: "", | |
DB_URL : System.getenv('DB_URL') ?: "", | |
DB_PASSWORD: System.getenv('DB_PASSWORD') ?: "", | |
] | |
def config = new File("$projectDir/src/main/resources/application-${environment}.yml") | |
def template = engine.createTemplate(config).make(binding).toString() | |
project.ext.environment = new Yaml().load(template) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment