maven - Spring Boot 始终使用相同的配置文件

标签 maven spring-boot maven-profiles spring-profiles spring-properties

我正在使用 spring boot 1.5.2,并使用配置文件,但我发现了一个非常奇怪的事情。

我的 Spring Boot 资源文件夹如下所示: enter image description here

application.yml 中的配置

spring:
  profiles:
    active: @profileActive@

应用程序-dev.yml

spring:
  profiles: dev
    datasource:
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/db1
      username: root
      password:
server:
  port: 8080

应用程序测试.yml

spring:
  profiles: test
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db2
    username: root
    password:

server:
  port: 8081

我的pom.xml,只包含资源部分和配置文件部分。

<!-- profile -->
<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <build.profile.id>dev</build.profile.id>
            <profileActive>dev</profileActive>
        </properties>
    </profile>
    <profile>
        <id>test</id>
        <properties>
            <build.profile.id>test</build.profile.id>
            <profileActive>test</profileActive>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <build.profile.id>prod</build.profile.id>
            <profileActive>prod</profileActive>
        </properties>
    </profile>
</profiles>


<resources>
        <resource>
            <directory>src/main/java</directory>
            <excludes>
                <exclude>**/*.java</exclude>
            </excludes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>application-${profileActive}.yml</include>
                <include>application.yml</include>
                <include>templates/*</include>
            </includes>
        </resource>

    </resources>

我现在尝试使用测试配置文件,发现一切正常,@profileActive@已替换为test

mvn clean package -Dmaven.test.skip=true -Ptest

看起来一切都很好。

enter image description here

但是当我尝试运行 jar 时,它总是使用 dev 配置文件,尽管 application.yml 显示我们现在使用 test 或 prod简介。

enter image description here

我不知道我的 yml 配置哪里出了问题。我尝试将所有配置文件配置包含在一个 application.yml 文件中。但应用程序仍在使用 dev 配置文件。

在一个 application.yml 文件中进行完全配置

spring:
  profiles:
    active: @profileActive@

---
spring:
  profiles: dev
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db1
  username: root
  password:

server:
  port: 8080

---
spring:
  profiles: test
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db2
    username: root
    password:

server:
  port: 8081

---
spring:
  profiles: prod

server:
  port: 9000

最后,我尝试使用属性文件,我的所有配置都工作正常,当我运行我的应用程序时可以使用正确的配置文件。

现在,我只想知道我的 yml 配置出了什么问题。

提前致谢!

最佳答案

终于找到原因了。

我的项目是一个多模块maven项目。

我正在使用 yml 格式的一个模块,

其他是property格式。

当我将两个模块都设为 yml 格式时,一切正常。

谢谢大家!谢谢!

关于maven - Spring Boot 始终使用相同的配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43295904/

相关文章:

java - 编译 javaFX 项目时出现 "class file has wrong version 54.0, should be 52.0"

java - Jenkins/Maven : Could not resolve default JAVA_HOME path, 构建失败

java - Maven 使用类别和配置文件运行特定测试不使用组标签

jsp - 使用 Maven 配置文件在缩小/未压缩的 JS 之间切换?

java - 在 maven java 项目运行时获取激活的配置文件名称列表

java - 使用 maven 导出的 jar 文件中包含 jar 文件和依赖项

java - 分析 SONAR 2.6 中的代码覆盖率

java - Spring 启动 : Initializing EmbeddedServletContainerFactory based on some flag

java - RestTemplate:exchange() vs postForEntity() vs execute()

java - @DiffIgnore 和 @ShallowReference 在回调中导致奇怪的行为