这两天项目到了差不多收尾了,想把各种环境配置通过maven filter在maven打包的时候就替换掉properties的配置。之前一直用${my.properies}的方式,打包了以后就是替换不掉properties里的属性。
这是我的maven filter的属性
filter.spring.freemarker.template-loader-path=file:/mnt/web/ftl/
filter.spring.resources.static-locations=file:/mnt/web/static/
filter.server.port=80
#mysql connection info
filter.jdbc.url=jdbc:mysql://192.168.1.111:3306/ubip?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&allowMultiQueries=true
filter.jdbc.username=root
filter.jdbc.password=banger
#redis config
filter.redis.database=0
filter.redis.host=192.168.1.111
filter.redis.port=6379
filter.redis.password=1234
在application.properties文件里需要被替换的属性
jdbc.url=${filter.jdbc.url}
...
maven配置
<profiles>
<profile>
<id>product</id>
<properties>
<env>product</env>
</properties>
</profile>
</profiles>
<build>
<filters> <!-- 指定使用的 filter -->
<filter>src/main/filters/${env}.properties</filter>
</filters>
<resources>
<resource> <!-- 配置需要被替换的资源文件路径, properties 应该在 src/main/resource 目录下 -->
<directory>src/main/resources</directory>
<excludes>
<exclude>static/**</exclude>
<exclude>ftl/**</exclude>
</excludes>
<filtering>true</filtering> <!-- 是否使用过滤器 -->
</resource>
</resources>
</build>
以前这样配置都是可以的。在spring boot里就不行。后来看了官方文档,里面有这样一句话
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...,所以mavenfilter {}占位符就被spring的maven pom替换掉了,变成了@..@,我们可以通过resource.delimiter来覆盖它。
看下spring-boot-starter-parent 这个pom里写这一段
<properties>
<java.version>1.6</java.version>
<resource.delimiter>@</resource.delimiter> <!-- delimiter that doesn't clash with Spring ${} placeholders -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
那一切就清楚了,我将properties中的占位符替换下变成
#mysql connection info
jdbc.url=@filter.jdbc.url@
jdbc.username=@filter.jdbc.username@
jdbc.password=@filter.jdbc.password@
#redis config
# Redis数据库索引(默认为0)
redis.database=@filter.redis.database@
# Redis服务器地址
redis.host=@filter.redis.host@
# Redis服务器连接端口
redis.port=@filter.redis.port@
# Redis服务器连接密码(默认为空)
redis.password=@filter.redis.password@
这样maven就能替换属性了。
Original url: Access
Created at: 2019-06-04 19:49:14
Category: default
Tags: none
未标明原创文章均为采集,版权归作者所有,转载无需和我联系,请注明原出处,南摩阿彌陀佛,知识,不只知道,要得到
java windows火焰图_mob64ca12ec8020的技术博客_51CTO博客 - 在windows下不可行,不知道作者是怎样搞的 监听SpringBoot 服务启动成功事件并打印信息_监听springboot启动完毕-CSDN博客 SpringBoot中就绪探针和存活探针_management.endpoint.health.probes.enabled-CSDN博客 u2u转换板 - 嘉立创EDA开源硬件平台 Spring Boot 项目的轻量级 HTTP 客户端 retrofit 框架,快来试试它!_Java精选-CSDN博客 手把手教你打造一套最牛的知识笔记管理系统! - 知乎 - 想法有重合-理论可参考 安宇雨 闲鱼 机械键盘 客制化 开贴记录 文本 linux 使用find命令查找包含某字符串的文件_beijihukk的博客-CSDN博客_find 查找字符串 ---- mac 也适用 安宇雨 打字音 记录集合 B站 bilibili 自行搭建 开坑 真正的客制化 安宇雨 黑苹果开坑 查找工具包maven pom 引用地 工具网站 Dantelis 介绍的玩轴入坑攻略 --- 关于轴的一些说法 --- 非官方 ---- 心得而已 --- 长期开坑更新 [本人问题][新开坑位]关于自动化测试的工具与平台应用 机械键盘 开团 网站记录 -- 能做一个收集的程序就好了 不过现在没时间 -- 信息大多是在群里发的 - 你要让垃圾佬 都去一个地方看难度也是很大的 精神支柱 [超级前台]sprinbboot maven superdesk-app 记录 [信息有用] [环境准备] [基本完成] [sebp/elk] 给已创建的Docker容器增加新的端口映射 - qq_30599553的博客 - CSDN博客 [正在研究] Elasticsearch, Logstash, Kibana (ELK) Docker image documentation elasticsearch centos 安装记录 及 启动手记 正式服务器 39 elasticsearch 问题合集 不断更新 6.1.1 | 6.5.1 两个版本 博客程序 - 测试 - bug记录 等等问题 laravel的启动过程解析 - lpfuture - 博客园 OAuth2 Server PHP 用 Laravel 搭建带 OAuth2 验证的 RESTful 服务 | Laravel China 社区 - 高品质的 Laravel 和 PHP 开发者社区 利用Laravel 搭建oauth2 API接口 附 Unauthenticated 解决办法 - 煮茶的博客 - SegmentFault 思否 使用 OAuth2-Server-php 搭建 OAuth2 Server - 午时的海 - 博客园 基于PHP构建OAuth 2.0 服务端 认证平台 - Endv - 博客园 Laravel 的 Artisan 命令行工具 Laravel 的文件系统和云存储功能集成 浅谈Chromium中的设计模式--终--Observer模式 浅谈Chromium中的设计模式--二--pre/post和Delegate模式 浅谈Chromium中的设计模式--一--Chromium中模块分层和进程模型 DeepMind 4 Hacking Yourself README.md update 20211011
Laravel China 简书 知乎 博客园 CSDN博客 开源中国 Go Further Ryan是菜鸟 | LNMP技术栈笔记 云栖社区-阿里云 Netflix技术博客 Techie Delight Linkedin技术博客 Dropbox技术博客 Facebook技术博客 淘宝中间件团队 美团技术博客 360技术博客 古巷博客 - 一个专注于分享的不正常博客 软件测试知识传播 - 测试窝 有赞技术团队 阮一峰 语雀 静觅丨崔庆才的个人博客 软件测试从业者综合能力提升 - isTester IBM Java 开发 使用开放 Java 生态系统开发现代应用程序 pengdai 一个强大的博主 HTML5资源教程 | 分享HTML5开发资源和开发教程 蘑菇博客 - 专注于技术分享的博客平台 个人博客-leapMie 流星007 CSDN博客 - 舍其小伙伴 稀土掘金 Go 技术论坛 | Golang / Go 语言中国知识社区
最新评论