首页 文章

Spring启动配置文件同步maven配置文件

提问于
浏览
0

我有一个Spring Boot应用程序 . 我需要将 spring 启动配置文件连接到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 dev配置文件 . 我每次切换配置文件时都会出现问题,它会从默认配置文件构建应用程序所以,请你帮我连接maven和spring boot profile . 这是我的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配置文件时只更改端口 .

1 回答

  • 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配置文件中设置的值 .

相关问题