spring - 在docker中使用gradle设置事件的 Spring 配置文件没有任何效果

标签 spring spring-boot docker gradle docker-compose

我有一个带有以下文件的spring boot项目:

  • application.yml(默认)
  • application-dev.yml(显然是我的开发配置)
  • Dockerfile-dev
  • docker-compose-dev.yml

  • 我的Dockerfile-dev包含以下setp:
    FROM gradle:5.2.1-jre11 AS BUILDSTAGE
    
    USER root
    WORKDIR /output
    
    COPY . .
    
    RUN gradle application:build -Dspring.profiles.active=dev --no-daemon
    RUN find /output/application/build/libs
    
    FROM openjdk:11-jre
    
    WORKDIR /app
    
    COPY application/wait-for-it.sh ./wait-for-it.sh
    COPY --from=BUILDSTAGE /output/application/build/libs/application-*.jar ./application.jar
    
    RUN chmod +x wait-for-it.sh
    
    ENTRYPOINT ["./wait-for-it.sh", "db:3306", "--", "java", "-jar", "application.jar"]
    

    请不要对wait-for-it.sh感到困惑,这是一个简单的shell脚本,它可以确保在启动数据库后启动Spring Boot。

    如您所见,我是使用-Dspring.profiles.active=dev构建的,因此,根据我的理解,这是将 Activity 的 Spring 配置文件设置为dev,这应确保application-dev.yml配置得到使用,而不是application.yml

    我的build.gradle包含:
    bootRun {
        systemProperties = System.properties
    }
    

    我的docker-compose文件正在使用Dockerfile-dev:
    version: "3"
    
    services:
    
      db:
        image: mysql:8.0.18
        environment:
          MYSQL_DATABASE: "test"
          MYSQL_USER: "test"
          MYSQL_PASSWORD: "test"
          MYSQL_ROOT_PASSWORD: "mysql"
          MYSQL_ROOT_HOST: "%"
        ports:
          - "3306:3306"
    
      app:
        depends_on:
          - db
        build:
          context: ..
          dockerfile: application/Dockerfile-dev
        ports:
          - "8080:8080"
          - "5005:5005"
    

    当我执行docker-compose -f application/docker-compose-dev.yml up --force-recreate --build -V --remove-orphans时,得到以下输出:
    Building app
    Step 1/12 : FROM gradle:5.2.1-jre11 AS BUILDSTAGE
     ---> d57eadc2e4d3
    Step 2/12 : USER root
     ---> Using cache
     ---> 72aa645c307e
    Step 3/12 : WORKDIR /output
     ---> Using cache
     ---> 327fd32c6c3d
    Step 4/12 : COPY . .
     ---> 055d0c0c09e9
    Step 5/12 : RUN gradle application:build -Dspring.profiles.active=dev --no-daemon
     ---> Running in c2a23d7fc2b3
    

    因此,这似乎很好。但是稍后在spring应用程序启动时的日志中:
    app_1  |
    app_1  |   .   ____          _            __ _ _
    app_1  |  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
    app_1  | ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
    app_1  |  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
    app_1  |   '  |____| .__|_| |_|_| |_\__, | / / / /
    app_1  |  =========|_|==============|___/=/_/_/_/
    app_1  |  :: Spring Boot ::        (v2.1.4.RELEASE)
    app_1  |
    app_1  | 2019-11-27 09:47:15,388 INFO  [main] [d.f.backend.ApplicationKt] - Starting ApplicationKt on 5f5fc7f3dddf with PID 1 (/app/application.jar started by root in /app)
    app_1  | 2019-11-27 09:47:15,403 INFO  [main] [d.f.backend.ApplicationKt] - No active profile set, falling back to default profiles: default
    

    如您所见No active profile set,所以它将使用默认值application.yml
    所以似乎我有一个误会/配置错误,如何在我的设置中设置 Activity 的 Spring 轮廓。我该如何设置?

    最佳答案

    您在构建应用程序时设置配置文件,而在运行应用程序时(通过入口点)需要设置配置文件:

    ENTRYPOINT ["./wait-for-it.sh", "db:3306", "--", "java", "-Dspring.profiles.active=dev", "-jar", "application.jar"
    

    关于spring - 在docker中使用gradle设置事件的 Spring 配置文件没有任何效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59087151/

    相关文章:

    java - 无法访问 Spring boot application.properties

    java - Apache Camel - 使用 Spring Security 进行 Camel 路由的基本身份验证

    docker - 从dockerfile无法安装pip

    jenkins - 将Docker镜像推送到Jenkinsfile中的注册表时出错

    symfony - 无法连接到主机,elasticsearch 关闭? Symfony 4 elastica/rulfin 客户端。 docker

    java - 模拟restTemplate getForObject

    java - 使用 JavaConfig 示例的 Spring Security Digest Auth

    java - Cucumber 测试一个 Spring Boot 应用程序

    json - 无法写入 JSON : Infinite recursion (StackOverflowError); nested exception spring boot

    java - 出现错误 "No converter found capable of converting from type java.lang.String to type org.springframework.security.core.GrantedAuthority"