【问题标题】:Spring boot profile sync maven profileSpring Boot 配置文件同步 Maven 配置文件
【发布时间】:2017-12-21 21:35:22
【问题描述】:

我有一个 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 文件。所以spring boot dev profile需要从2个命令中调用。每次我切换配置文件时都会遇到问题,它会从默认配置文件构建应用程序。所以你能帮我连接 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 配置文件时才更改端口。

【问题讨论】:

    标签: java spring maven spring-boot spring-boot-maven-plugin


    【解决方案1】:

    我不了解您想要的所有内容,但您有两种选择可以让某些东西正常工作。

    首先,在您的一个 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 配置文件中设置的值。

    【讨论】:

    • 我想通过切换 maven 配置文件来切换 spring boot 配置文件。因此,当我使用 maven prod 配置文件构建项目时。它使用名为 prod 的 spring boot 配置文件通过 application-prod.ymp 属性文件构建项目。
    • 是的,这正是文档中描述的内容。
    猜你喜欢
    • 2020-05-07
    • 2015-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-16
    • 2022-01-04
    • 2021-01-08
    相关资源
    最近更新 更多