在Spring Boot自定义嵌入式launch.script中设置SPRING_PROFILES_ACTIVE [英] Setting SPRING_PROFILES_ACTIVE inside of Spring Boot custom embedded launch.script

查看:221
本文介绍了在Spring Boot自定义嵌入式launch.script中设置SPRING_PROFILES_ACTIVE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有可执行的Spring Boot Jar.通过运行时帐户的.bashrc提供了一些环境变量,包括SPRING_PROFILES_ACTIVE.这是在运行时通过检查一些系统参数设置的.

We have executable spring boot jars. Some environment variables are provided via the runtime account's .bashrc , including SPRING_PROFILES_ACTIVE . This is set at runtime through checking some system parameters.

为了启用真正可移植的应用程序,我们想在Spring Boot可执行启动脚本的自定义版本(launch.script)中设置这些变量,而不是依赖于在某个地方部署的自定义.bashrc.

In order to enable a truly portable application, we would like to set these variables inside a custom version of the Spring Boot executable launch script (launch.script) instead of relying upon a custom .bashrc being deployed somewhere.

即启动脚本将运行当前在bashrc中的自定义外壳程序代码.

i.e. the launch script runs the custom shell code that is currently in our bashrc .

然后我们可以将Spring Boot可执行jar放在我们的一个盒子上,而不必部署自定义.bashrc.

We could then throw the Spring Boot executable jar on one of our boxes without having to deploy the custom .bashrc .

我们已经复制了默认的Spring Boot launch.script并预先设置了环境变量.我们通过使用spring-boot-maven-plugin将默认脚本替换为自定义脚本.使用echo语句,我们知道新的launch.script替换了默认的launch.script.

We have copied the default Spring Boot launch.script and prepended setting our environment variables. We replace the default script with our custom one by using the spring-boot-maven-plugin. Using echo statements, we know that the new launch.script replaces the default one.

我们动态设置的一个环境变量是SPRING_PROFILES_ACTIVE.通过echo语句,我知道在我们的自定义launch.script中正确设置了变量.但是,该值不会传播到应用程序.也就是说,应用程序启动时的弹簧配置文件始终为默认",而不是L0,L1或我们尝试设置的任何设置.

One environment variable we are setting dynamically is SPRING_PROFILES_ACTIVE . Through an echo statement, I know that the variable is being set correctly inside of our custom launch.script. However, that value is not being propagated to the application. i.e. the spring profile at application startup is always 'default', versus L0, L1 or whatever we're trying to set.

我已经使用export导出了变量并声明了-x.

I have exported the variable using export and declare -x .

我们所做的一切是否可行,还是在launch.script中设置SPRING_PROFILES_ACTIVE为时已晚?

Is what we are doing at all feasible, or is setting SPRING_PROFILES_ACTIVE in the launch.script too late ?

推荐答案

在快照版本(2.0.0.M7)中,有一个新的

In the snapshot version(2.0.0.M7) there is a new property substitution named inlinedConfScript, we can use this feature to set some environment variables before application startup.

pom.xml

The <build> setting in pom.xml

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <executable>true</executable>
                <embeddedLaunchScriptProperties>
                    <inlinedConfScript>${project.basedir}/my.script</inlinedConfScript>
                </embeddedLaunchScriptProperties>
            </configuration>
        </plugin>
    </plugins>
</build>


在项目 pom.xml

export SPRING_PROFILES_ACTIVE=production

这篇关于在Spring Boot自定义嵌入式launch.script中设置SPRING_PROFILES_ACTIVE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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