如何通过 gradle 任务使用 spring 配置文件运行 bootRun [英] How to run bootRun with spring profile via gradle task

查看:33
本文介绍了如何通过 gradle 任务使用 spring 配置文件运行 bootRun的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置 gradle 以启动 bootRun 进程并启用各种 spring 配置文件.

I'm trying to set up gradle to launch the bootRun process with various spring profiles enabled.

我当前的 bootRun 配置如下:

My current bootRun configuration looks like:

bootRun {
    // pass command line options from gradle to bootRun
    // usage: gradlew bootRun "-Dspring.profiles.active=local,protractor"
    if (System.properties.containsKey('spring.profiles.active')) {
        systemProperty "spring.profiles.active", System.properties['spring.profiles.active']
    }
}

我想用 gradle 任务设置系统属性,然后执行 bootRun.

I'd like to set system properties with a gradle task, and then execute bootRun.

我的尝试看起来像这样:

My attempt looked like this:

task bootRunDev

bootRunDev  {
    System.setProperty("spring.profiles.active", "Dev")
}

几个问题:

  1. systemProperty 是 spring boot bootRun 配置的一部分吗?
  2. 是否可以在另一个任务中设置系统属性?
  3. 下一步应该做什么?我需要在 bootRun
  4. 之前获得 bootRunDev 配置
  5. 我应该研究另一种方法吗
  1. is systemProperty a part of the spring boot bootRun configuration?
  2. is it possible to set a system property in another task?
  3. What should my next step be? I need to get bootRunDev configuration to happen before bootRun
  4. Is there another approach I should look into

-埃里克

推荐答案

最简单的方法是定义默认值并允许它被覆盖.我不确定在这种情况下 systemProperty 的用途是什么.简单的参数就可以完成工作.

Simplest way would be to define default and allow it to be overridden. I am not sure what is the use of systemProperty in this case. Simple arguments will do the job.

def profiles = 'prod'

bootRun {
  args = ["--spring.profiles.active=" + profiles]
}

运行开发:

./gradlew bootRun -Pdev

要为您的任务添加依赖项,您可以执行以下操作:

To add dependencies on your task you can do something like this:

task setDevProperties(dependsOn: bootRun) << {
  doFirst {
    System.setProperty('spring.profiles.active', profiles)
  }
}

在 Gradle 中有很多方法可以实现这一点.

There are lots of ways achieving this in Gradle.

为每个环境配置单独的配置文件.

Configure separate configuration files per environment.

if (project.hasProperty('prod')) {
  apply from: 'gradle/profile_prod.gradle'
} else {
  apply from: 'gradle/profile_dev.gradle'
}

每个配置都可以覆盖任务,例如:

Each configuration can override tasks for example:

def profiles = 'prod'
bootRun {
  systemProperty "spring.profiles.active", activeProfile
}

在这种情况下通过提供 prod 标志来运行:

Run by providing prod flag in this case just like that:

./gradlew <task> -Pprod

这篇关于如何通过 gradle 任务使用 spring 配置文件运行 bootRun的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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