SpringBoot基于Maven多环境yml配置文件

2019-06-10  本文已影响0人  叫我null

1、pom.xml maven配置

<properties>
    <java.version>1.8</java.version>
</properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <delimiters>
                        <delimiter>@</delimiter>
                    </delimiters>
                    <useDefaultDelimiters>false</useDefaultDelimiters>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/webapp</directory>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>

    <profiles>
        <profile>
            <id>local</id>
            <properties>
                <spring.active>local</spring.active>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <spring.active>prod</spring.active>
            </properties>
        </profile>
    </profiles>

2、application.yml 配置文件

custom:
  weather-api: https://restapi.amap.com/v3/weather/weatherInfo
  weather-key: c74ce7c9a4d0695ce2adddda52562d400b
  
spring:
  profiles:
    active: @spring.active@
  mvc:
    static-path-pattern: /static/**
    view:
      prefix: /WEB-INF/view
  devtools:
    restart:
      enabled: false
      additional-paths: src/main/java
      exclude: static/**,WEB-INF/view/**
  servlet:
    multipart:
      max-request-size: 100MB
      max-file-size: 100MB
      
---
server:
  port: 8088
spring:
  profiles: local
  datasource:
    url: jdbc:mysql://192.168.56.3:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: 123
    filters: wall,mergeStat
    
    
---
server:
  port: 8080
spring:
  profiles: prod
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: 123qwe
    filters: wall,mergeStat

3、调试与发布
由于pom指定了默认local配置(activeByDefault ),所以启动的是可以从控制台输出


TIM截图20190610152853.jpg

发布的时候可以通过命令编译jar包

mvn clean package -Dmaven.test.skip=true -P prod

打开生产的jar包中的yml文件可以看见,现在活动配置已经是prod

spring:
  profiles:
    active: prod

4、jenkins中生产环境配置
Build:pom.xml
Goals and options:clean package -Dmaven.test.skip=true -P prod

上一篇下一篇

猜你喜欢

热点阅读