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

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

问题描述

我正在尝试设置gradle以启动启用了各种弹簧配置文件的bootRun进程.

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

我当前的bootRun配置如下:

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

要运行dev:

./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 profile运行bootRun的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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