我们有可执行的 spring boot jar。一些环境变量是通过运行时帐户的 .bashrc 提供的,包括 SPRING_PROFILES_ACTIVE 。这是在运行时通过检查一些系统参数来设置的。
为了实现真正可移植的应用程序,我们希望在 Spring Boot 可执行启动脚本 (launch.script) 的自定义版本中设置这些变量,而不是依赖于部署在某处的自定义 .bashrc。
即启动脚本运行当前位于 bashrc 中的自定义 shell 代码。
然后,我们可以将 Spring Boot 可执行 jar 扔到我们的一台机器上,而无需部署自定义 .bashrc 。
我们已经复制了默认的 Spring Boot launch.script 并预先设置了我们的环境变量。我们使用 spring-boot-maven-plugin 将默认脚本替换为自定义脚本。使用 echo 语句,我们知道新的 launch.script 替换了默认脚本。
我们动态设置的一个环境变量是 SPRING_PROFILES_ACTIVE 。通过 echo 语句,我知道该变量在我们的自定义 launch.script 中已正确设置。但是,该值不会传播到应用程序。即应用程序启动时的 spring 配置文件始终是“默认”,而不是 L0、L1 或我们尝试设置的任何内容。
我已使用 export 导出变量并声明 -x 。
我们正在做的事情是否可行,还是在 launch.script 中设置 SPRING_PROFILES_ACTIVE 太晚了?
最佳答案
在快照版本(2.0.0.M7)中有一个新的property substitution named inlinedConfScript
,我们可以利用这个功能在应用程序启动之前设置一些环境变量。
<build>
设置在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>
<小时/>
创建my.script
与项目的pom.xml
同级目录下
export SPRING_PROFILES_ACTIVE=production
关于java - 在 Spring Boot 自定义嵌入式 launch.script 中设置 SPRING_PROFILES_ACTIVE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45551618/