【问题标题】:Pass environment variables to Spring Boot application with Maven使用 Maven 将环境变量传递给 Spring Boot 应用程序
【发布时间】:2020-12-30 03:53:58
【问题描述】:

我正在使用 Spring Boot 3.2.1

我不想将敏感数据作为application.yml 放在配置文件中。所以他们指的是环境变量。

在应用程序的 IntelliJ 配置设置(Environment variables 部分)中执行正常。

但是,从控制台执行 maven 失败:

./mvnw -Ddemo-api-key=all56 -Ddemo-host=https://demo.api spring-boot:run

这里是异常详情:

java.lang.IllegalArgumentException: Not enough variable values available to expand 'demo-host'
        at org.springframework.web.util.UriComponents$VarArgsTemplateVariables.getValue(UriComponents.java:367) ~[spring-web-5.2.7.RELEASE.jar:5.2.7.RELEASE]
        at org.springframework.web.util.UriComponents.expandUriComponent(UriComponents.java:262) ~[spring-web-5.2.7.RELEASE.jar:5.2.7.RELEASE]
        at org.springframework.web.util.HierarchicalUriComponents$PathSegmentComponent.expand(HierarchicalUriComponents.java:960) ~[spring-web-5.2.7.RELEASE.jar:5.2.7.RELEASE]
        at org.springframework.web.util.HierarchicalUriComponents.expandInternal(HierarchicalUriComponents.java:434) ~[spring-web-5.2.7.RELEASE.jar:5.2.7.RELEASE]
        at org.springframework.web.util.HierarchicalUriComponents.expandInternal(HierarchicalUriComponents.java:52) ~[spring-web-5.2.7.RELEASE.jar:5.2.7.RELEASE]
        at org.springframework.web.util.UriComponents.expand(UriComponents.java:172) ~[spring-web-5.2.7.RELEASE.jar:5.2.7.RELEASE]

这是来自application.yml的sn-p:

car-parks-url: ${demo-host}/cap-ws/getCarParks?apiKey=${demo-api-key}

如果我把它放在更高的 application.yml 它工作正常:

demo-api-key: all56
demo-host: https://demo.api

URL 格式正确,所有数据都被正确提取。

无法理解使用 maven 将其作为环境变量传递时遗漏了什么?

【问题讨论】:

    标签: spring-boot maven environment-variables yaml


    【解决方案1】:

    不一样:

    java -jar -Ddemo-api-key=all56 -Ddemo-host=https://demo.api myApp.jar
    

    您将环境变量 直接传递给您的应用程序的位置

    如果你这样做:

    ./mvnw -Ddemo-api-key=all56 \
    -Ddemo-host=https://demo.api spring-boot:run
    

    您将变量传递给 ma​​ven 任务而不是应用程序

    你可以使用:

     ./mvnw  spring-boot:run \
     -Dspring-boot.run.arguments="--demo-host=https://demo.api --demo-api-key=all56"
    

     ./mvnw  spring-boot:run -Dspring-boot.run.jvmArguments="\
     -Ddemo-host=https://demo.api -Ddemo-api-key=all56"
    

    这些命令指示您将哪些变量传递给 Spring Boot 应用程序。

    其他资源:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-06
      • 1970-01-01
      • 2018-03-24
      • 2016-07-31
      • 1970-01-01
      • 2023-02-24
      • 2021-02-19
      • 1970-01-01
      相关资源
      最近更新 更多