Spring 引导配置文件同步 maven 配置文件



我有一个Spring Boot应用程序。我需要将 spring 启动配置文件连接到 maven 配置文件,以便比调用命令时更合适

mvn clean install -Pdev 

mvn clean install -Pprod

它应该调用 Spring boot 来加载 application-dev.yml 或 application-prod.yml。当我打电话

mvn clean install

它应该在启动之前调用应用程序开发.yml 文件。因此,需要从 2 个命令调用 Spring 引导开发配置文件。每次切换配置文件时,我都遇到问题,它从默认配置文件构建应用程序。所以你能帮我连接 maven 和弹簧启动配置文件吗? 这是我的绒球.xml

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
<profiles>${spring-profiles}</profiles>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>dev</id>
<properties>
<spring-profiles>dev</spring-profiles>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<spring-profiles>prod</spring-profiles>
</properties>
</profile>
</profiles>

这是application.yml的例子。

spring:
devtools:
restart :
enabled: true
livereload:
enabled: false  
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
database: MYSQL
show_sql: true
properties:
hibernate.cache.use_second_level_cache: true
hibernate.cache.use_query_cache: false
hibernate.generate_statistics: true
hibernate.cache.region.factory_class: org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
hibernate:
ddl-auto: update

server:
port: 8080
compression:
enabled: true
mime-types : application/json,application/xml,text/html,text/xml,text/plain,application/javascript,text/css,text/html
session :
timeout : 540000
mail:
smtp:
starttls.enable: true
auth: true
port: 587
activation:
expiration.hours: 24
template: /mails/activationEmail.html
change-password:
template: /mails/passwordResetEmail.html

application-dev.yml和application-prod.yml之间的区别仅在于端口。这样我只在部署前更改 mvn 配置文件时更改端口。

我不明白你想要的一切,但你有两种选择可以让一些东西工作。

首先,在您的一个 maven 配置文件中,您可以默认设置一个:

<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>

因此,无需为mvn clean install提供maven配置文件

另一种选择是为配置文件设置变量,并在构建应用程序时将这些值获取到application.properties文件中。

例如,对于您的端口,您可以这样做:

<profile>
<id>dev</id>
<properties>
<serverPort>9999</serverPort>
</properties>
</profile>

在您的 application.properties 文件中,您可以使用@serverPort来获取值:

server.port=@serverPort@

如果您现在使用任何配置文件构建应用程序,您将获得在 maven 配置文件中设置的值。

最新更新