gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 09:09:59 25 4
gpt4 key购买 nike

我正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com