SpringBoot线上部署踩坑

SpringBOOT项目在线上部署时,要么在命令行里添加–spring.profiles.active=prod来指定环境,要么通过maven 打包时动态传参激活–spring.profiles.active=prod,之前对于SpringBOOT的项目部署都是采用命令行方式激活环境分支,现项目集成部署环境,采用的maven -p方式激活环境分支,需将激活的分支变量传递给
spring.profiles.active=prod

plugin指定maven打包时需要取得参数,根据参数来设置打包的环境的配置文件
另外需要建一个application.yml文件指定启动文件环境
application.yml方式配置如下:

spring:
  profiles:
    active: @profileActive@
<profiles>
        <profile>
            <id>productid>
            <properties>
                <profileActive>prodprofileActive>
            properties>
        profile>
    profiles>
    <build>
        <resources>
            <resource> 
                <directory>src/main/resourcesdirectory>
                <excludes>
                    <exclude>static/**exclude>
                excludes>
                <filtering>truefiltering> 
            resource>
        resources>
    build>

传统的maven 项目在对配置文件中进行占位符替换时,如下开启对某个目录下的文件过滤替换就行,配置文件中占位符为${…},但是在SpringBOOT项目中,配置文件要想从maven中获取环境变量,占位符得是@…@

    <build>
        <resources>
            <resource>
                <directory>src/main/resourcesdirectory>
                <filtering>truefiltering>
            resource>
        resources>
    build>
13.2 Maven 
Maven users can inherit from the spring-boot-starter-parent project to obtain sensible defaults. The parent project provides the following features:

Java 1.6 as the default compiler level. 
UTF-8 source encoding. 
A Dependency Management section, allowing you to omit tags for common dependencies, inherited from the spring-boot-dependencies POM. 
Sensible resource filtering. 
Sensible plugin configuration (exec plugin, surefire, Git commit ID, shade). 
Sensible resource filtering for application.properties and application.yml including profile-specific files (e.g. application-foo.properties and application-foo.yml) 
On the last point: since the default config files accept Spring style placeholders (${…​}) the Maven filtering is changed to use @..@ placeholders (you can override that with a Maven property resource.delimiter).

大致的意思是我的maven继承了spring-boot-starter-parent,并且spring默认配置文件接受的占位符也是…,所以mavenfilter{}占位符就被spring的maven pom替换掉了,变成了@..@,我们可以通过resource.delimiter来覆盖它。
看下spring-boot-starter-parent 这个pom里写这一段

<properties>
        <java.version>1.6java.version>
        <resource.delimiter>@resource.delimiter> 
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <maven.compiler.source>${java.version}maven.compiler.source>
        <maven.compiler.target>${java.version}maven.compiler.target>
    properties>

SpringBOOT的相关issues

总结:所以Springboot 项目在启动时要么采用命令行方式激活,也可以采用mavan 打包传递参数激活,更多激活方式可以查看SpringBOOT的配置优先级可以参考 SpringBoot系列(三)内置配置及自定义配置

你可能感兴趣的:(spring-boot)