如何加载yml propery到gradle [英] How to load yml propery to gradle

查看:805
本文介绍了如何加载yml propery到gradle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  spring:
应用程序:
名称:auth模块
配置文件:
active:prod

在我的gradle.build中:

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

我想构建像auth-module-%profile_name%.jar这样的jar。假设你的yaml文件有名称 cfg.yaml $ c
$ b

不要忘记在yaml的开头添加 ---

  --- 
spring:
应用程序:
名称:auth模块
配置文件:
active:prod

build.gradle:

  defaultTaskstestMe

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath组:'org.yaml',名称:'snakeyaml',版本:'1.19'
}
}

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

task testMe(){
doLast {
printlnmake
printlnprofile = $ {cfg.spring.profiles.active}
assert cfg.spring.profiles.active ==prod
}
}


I have yml file with such property:

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

In my gradle.build:

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

I want to build jars like auth-module-%profile_name%.jar. How can I do this?

解决方案

assume your yaml file has name cfg.yaml

don't forget to add --- at the beginning of 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"
    }
}

这篇关于如何加载yml propery到gradle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆