Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
262 views
in Technique[技术] by (71.8m points)

java - Setting SPRING_PROFILES_ACTIVE inside of Spring Boot custom embedded launch.script

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.

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.

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

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

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.

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.

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


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>

Create my.script in the the same directory of the project's pom.xml

export SPRING_PROFILES_ACTIVE=production

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...