如何将yml propery加载到gradle中

时间:2018-01-18 09:43:16

标签: spring gradle groovy yaml

我有这样的属性的yml文件:

spring:
  application:
    name: auth module
  profiles:
    active: prod

在我的gradle.build中:

jar {
    baseName = 'auth-module-dev'
    version =  '0.1.2'
}

我想构建像auth-module-%profile_name%.jar这样的jar。我怎么能这样做?

1 个答案:

答案 0 :(得分:5)

假设您的yaml文件名为cfg.yaml

不要忘记在yaml

的开头添加---
---
spring:
  application:
    name: auth module
  profiles:
    active: prod

的build.gradle:

defaultTasks "testMe"

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath group: 'org.yaml', name: 'snakeyaml', version: '1.19'
    }
}

def cfg = new org.yaml.snakeyaml.Yaml().load( new File("cfg.yaml").newInputStream() )

task testMe( ){
    doLast {
        println "make "
        println "profile =  ${cfg.spring.profiles.active}"
        assert cfg.spring.profiles.active == "prod"
    }
}