前言:

最近在项目中使用springboot时发现,采用在pom中定义不同的profile,并且maven打包时

采用-P参数并不能替换我application.properties文件中指定占位符的问题。

配置文件布局:

在application.properties中定义整个项目中不同环境下共通的配置属性,并采用springboot针对配置文件的特性 - spring.profiles.active=dev或者test

来自动加载合并application-dev/test.properties中特有的配置的功能,开发,测试,生产环境配置文件命名方面需要遵守springboot的约束即可。

Maven pom 配置:

 

备注:实际上使用springboot,上述的<resources>标签,以及<outputDirectory>都是不需要的,其都已经定义过了。在这里写出来只是为了讲述maven打包的原理。

执行打包命令:

进入项目pom文件所在目录,执行

mvn clean package -P test

命令解释:

1:执行后会根据pom中定义的profiles标签寻找对应的profile id为test的,将其properties下的子标签获取到,拿到key = boot.profile,value = test。放入map中

2:根据 build中定义的resources标签,开始打包对应的资源文件,由于指定了filtering为true,故会将上一步中得到的map中的key拿去替换resource中指定的资源文件中的占位符${key},为value值。

遇到的问题:

 

执行后查看classes文件下的资源文件,发现并没有替换掉

原因:

发现最终是因为springboot导致的,查看其pom继承,

进入后发现,springboot默认指定资源文件中的占位符为@@,并不是maven默认的 ${}

 

解决方案:

1:将application.properties中的占位符由${key} -> @key@

2:覆盖springboot的默认规则

     <build>
<finalName>${project.artifactId}</finalName>
<resources>
<resource>
<filtering>true</filtering>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**/application*.yml</include>
<include>**/application*.yaml</include>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
<excludes>
<exclude>**/application*.yml</exclude>
<exclude>**/application*.yaml</exclude>
<exclude>**/*.properties</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>utf-8</encoding>
<useDefaultDelimiters>true</useDefaultDelimiters>
</configuration>
</plugin>
</plugins>
</build>
  其中,增加maven-resources-plugin就已经可以使用${}了(此时@@依旧存在),resources只是修改影响文件范围
  filtering设置为true很关键,不然引用不到profiles中的内容。

3:测试了再自己的pom中不增加resource配置,只是增加

也是可以的哦

建议:

既然已经使用了springboot,就不要再用回之前的写法了,平白增加配置。

附:pom的profile导入.properties

方法1:

   <profiles>
<!--开发环境-->
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<spring.profiles.active>dev</spring.profiles.active>
</properties>
<build>
<filters>
<filter>${basedir}/src/main/resources/application-dev.properties</filter>
</filters>
</build>
</profile>
<!--预发环境-->
<profile>
<id>pre</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<spring.profiles.active>pre</spring.profiles.active>
</properties>
<build>
<filters>
<filter>${basedir}/src/main/resources/application-pre.properties</filter>
</filters>
</build>
</profile>
</profiles>

方法2:

   <profiles>
<!--开发环境-->
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<spring.profiles.active>dev</spring.profiles.active>
</properties>
</profile>
<!--预发环境-->
<profile>
<id>pre</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<spring.profiles.active>pre</spring.profiles.active>
</properties>
</profile>
</profiles> <build>
<filters>
<filter>
${basedir}/src/main/resources/application-${spring.profiles.active}.properties
</filter>
</filters>
</build>

springboot多环境下maven打包的更多相关文章

  1. eclipse使用profile完成不同环境的maven打包功能

    原文:https://blog.csdn.net/duan9421/article/details/79086335 我们在日常开发工作中通常会根据不同的项目运行环境,添加不同的配置文件,例如 开发环 ...

  2. Windows环境下maven 安装与环境变量配置

    Maven是一个项目管理的Java 工具,在JavaEE中,我们可以使用Maven方便地管理团队合作的项目,现在我们在学习JavaEE框架,使用Maven可以管理类库,有效方便地供团队中的其他人员使用 ...

  3. windows 环境下Maven私服搭建

    使用Nexus.3.11在Windows环境上搭建1.下载nexus.3.11.zip包https://www.sonatype.com/download-oss-sonatype 下载下来之后,进行 ...

  4. linux环境下maven的安装配置

    1.到官网下载maven,上传到服务器上 https://maven.apache.org/download.cgi 2.将压缩包上传服务器对应路径解压: tar -zxvf apache-maven ...

  5. Linux环境下Maven的.m2文件夹

    aven中的.m2文件夹 安装完maven是没有.m2文件夹的.在linux中以.开头的文件夹都是隐藏的.当使用maven命令的时候,maven自动会创建.m2文件夹. 运行命令mvn help:sy ...

  6. windows下maven打包eclipse工程

    打包过程中,可能出现的2个问题: ①.[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build ...

  7. Video.js + HLS 在production环境下webpack打包后出错的解决方案

    Video.js是一个非常强大的视频播放库,能在微信下完美提供inline小窗口播放模式,但当涉及到hls格式视频播放时就比较麻烦,出现的数种现象都不好解决. 错误现象:  1.  PC Chrome ...

  8. win10环境下pyinstaller打包pytorch遇到的问题及解决方案

    pytorch-python源码生成windows的应用程序(.exe),报错OSError: could not get source code Failed to execute script h ...

  9. maven插件 按配置加载不同环境配置文件进行打包(maven-war-plugin)

    1.配置多种不同环境 如(本地local,开发dev,测试test 环境等) <profiles> <profile> <id>local</id> & ...

随机推荐

  1. A Full Hardware Guide to Deep Learning深度学习电脑配置

     https://study.163.com/provider/400000000398149/index.htm?share=2&shareId=400000000398149( 欢迎关注博 ...

  2. jetty demo实例启动

    Jetty是一个提供HHTP服务器.HTTP客户端和javax.servlet容器的开源项目.Jetty和tomcat相比,是轻量级服务器,支持热拔插,可扩展性大tomcat集成了很多功能,个性化瘦身 ...

  3. Srping的IOC

    XML方式: IOC:控制反转的底层原理就是:工厂模式+反射+配置文件DI:依赖注入就是通过配置文件设置属性值 BeanFactory 是老版本的工厂类:调用getBean的时候,才会生成类的实例Ap ...

  4. 《深入理解Linux内核》 读书笔记

    深入理解Linux内核 读书笔记 一.概论 操作系统基本概念 多用户系统 允许多个用户登录系统,不同用户之间的有私有的空间 用户和组 每个用于属于一个组,组的权限和其他人的权限,和拥有者的权限不一样. ...

  5. Docker 两键创建 ZeroTier moon 节点

    一条命令创建 ZeroTier moon 节点: $ docker run --name zerotier-moon -d -p 9993:9993 -p 9993:9993/udp seedgou/ ...

  6. System.Threading.Timer定时器使用注意事项

    1.定时器不要直接在方法里面定义和赋值,因为方法执行完,方法体内的变量会被GC回收. 有时候我们将timer定义在了方法里面,然后看到timer被执行了几次之后才失效,原因就是GC不一定会立即回收. ...

  7. 各手机PC品牌投屏功能连接方法

    一.iOS终端(iPhone/iPad)无线投屏: 1.将iPhone或iPad与必捷会议盒子连接至同一路由器: 2.滑动iPhone/iPad的屏幕,调出Airplay功能,选择需要投屏的主机,开始 ...

  8. 【记录】【MySQL】填充字符串函数 LPAD(str,len,padstr)

    LPAD(str,len,padstr) 1.如果str的长度等于len,那么就返回str 2.如果str的长度大于len,那么就返回str的前len个长度 3.如果str的长度小于len,那么就返回 ...

  9. git clean用法

    git clean用法   想批量删除branch中新加的文件(untracked files),,git reset --hard不行-   首先确认要删除的文件 git clean -fd -n ...

  10. [转帖]美团在Redis上踩过的一些坑-1.客户端周期性出现connect timeout

    美团在Redis上踩过的一些坑-1.客户端周期性出现connect timeout 博客分类: redis 运维 jedisconnect timeoutnosqltcp  转载请注明出处哈:http ...