如何在 Gradle 构建脚本中加载 .yml 属性? [英] How do I load a .yml property in a Gradle build script?

查看:54
本文介绍了如何在 Gradle 构建脚本中加载 .yml 属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有具有以下属性的 .yml 文件:

I have .yml file with the following properties:

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

我的 gradle.build 脚本对 jar 任务有以下设置:

My gradle.build script has these settings for the jar task:

jar {
    baseName = 'auth-module-dev'  // `dev` should come from `spring.profiles.active`
    version =  '0.1.2'
}

我想用命名约定 auth-module-%profile_name%.jar 构建 jar 文件,其中 %profile_name%spring.profiles 的值.active.我该怎么做?

I want to build jar files with the naming convention auth-module-%profile_name%.jar where %profile_name% is the value of spring.profiles.active. How can I do this?

推荐答案

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

assume your yaml file has name cfg.yaml

不要忘记在yaml开头加上---

don't forget to add --- at the beginning of yaml

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

build.gradle:

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"
    }
}

这篇关于如何在 Gradle 构建脚本中加载 .yml 属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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