繁体   English   中英

Spring Boot配置文件同步Maven配置文件

[英]Spring boot profile sync maven profile

我有一个Spring Boot应用程序。 我需要将Spring Boot配置文件连接到Maven配置文件,以便比调用命令时

mvn clean install -Pdev 

要么

mvn clean install -Pprod

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

mvn clean install

它应该在启动之前调用application-dev.yml文件。 因此,需要从2个命令中调用spring boot开发人员配置文件。 每次切换配置文件时,我都会从默认配置文件构建应用程序时遇到问题。 所以,请您帮我整理一下Maven和Spring Boot的配置文件。 这是我的pom.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时将变量设置为配置文件,并将这些值获取到application.properties文件。

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

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

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

server.port=@serverPort@

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM