1. 程式人生 > >spring boot通過maven filter替換properties屬性(多環境配置)

spring boot通過maven filter替換properties屬性(多環境配置)

這兩天專案到了差不多收尾了,想把各種環境配置通過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 @[email protected] placeholders (you can override that with a Maven property resource.delimiter).

大致的意思是我的maven繼承了spring-boot-starter-parent,並且spring預設配置檔案接受的佔位符也是...mavenfilter{}佔位符就被spring的maven pom替換掉了,變成了@[email protected],我們可以通過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[email protected].jdbc.url@
jdbc.username[email protected].jdbc.username@
jdbc.password[email protected].jdbc.password@

#redis config
# Redis資料庫索引(預設為0)
redis.database[email protected].redis.database@
# Redis伺服器地址
redis.host[email protected].redis.host@
# Redis伺服器連線埠
redis.port[email protected].redis.port@
# Redis伺服器連線密碼(預設為空)
redis.password[email protected].redis.password@

這樣maven就能替換屬性了。

相關推薦

spring boot通過maven filter替換properties屬性環境配置

這兩天專案到了差不多收尾了,想把各種環境配置通過maven filter在maven打包的時候就替換掉properties的配置。之前一直用${my.properies}的方式,打包了以後就是替換不掉properties裡的屬性。 這是我的maven filt

Spring boot基於redis實現附近的人附原始碼下載

核心原始碼 public class NearbyPO { @NotNull(message = "id值不能為空") private Integer id; @NotBlank(message

spring-boot整合mybatis和druid連線池資料來源

上一篇文章介紹了 spring-boot整合mybatis和druid連線池如何配置和使用,本篇文章是上篇的擴充套件,如果業務中出現了需要從多個數據源中獲取資料,到底該如何實現?本文主要介紹在一種最為簡單的實現方案:多資料來源 - 多例項。 在上篇文章中不難看出Spring

Spring boot實戰專案整合阿里雲RocketMQ 非開源版訊息佇列實現傳送普通訊息,延時訊息 --附程式碼

一.為什麼選擇RocketMQ訊息佇列? 首先RocketMQ是阿里巴巴自研出來的,也已開源。其效能和穩定性從雙11就能看出來,借用阿里的一句官方介紹:歷年雙 11 購物狂歡節零點千萬級 TPS、萬億級資料洪峰,創造了全球最大的業務訊息併發以及流轉紀錄(日誌類訊息除外);  在始終保證高效能前提下

Spring Boot2.0整合Mybatis(自動生成註解方式,環境配置)

本介紹Spring Boot2.0整合Mybatis,通過MyBatis Generator外掛自動生成mapper的sql註解及Provider類的過程,支援多環境的yml配置檔案首先用IDE開發工具(IDEA,STS,Eclipse)建立一個Spring Boot工程sp

dubbo服務使用maven部署支援profile環境配置

部署dubbo服務通常有三種方式:1、打war包,放在tomcat中執行,缺點:增加複雜性(埠、管理) 浪費資源(記憶體)官方:服務容器是一個standalone的啟動程式,因為後臺服務不需要Tomcat或JBoss等Web容器的功能,如果硬要用Web容器去載入服務提供方,增

hibernate對映只讀屬性次對映

需在對映檔案中將insert="false" update="false",這樣,該欄位可被對映為多個         <discriminator column="obj_type"/>             <property insert=

maven專案引入spring boot依賴之後filter不生效的問題

建立一個maven專案,專案結構如下: 其中,pom.xml的內容如下: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-

Spring boot @Value註解讀取application.properties配置檔案中的屬性

@Value註解引用application.properties配置檔案屬性值 @Value 註解載入屬性值的時候可以支援兩種表示式來進行配置, 如下所示: 一種是PlaceHolder 方式, 格式為${...}, 大括號內為PlaceHolder。 另

Spring Boot通過application.yml配置文件獲取屬性及類信息

boot 實體類 ping override ria pro tid @value attr 實體類信息**@ConfigurationProperties(prefix="mycar")**br/>直接獲取單個屬性@Value("${mycar.carNo}")

spring-boot實戰【05】:Spring Boo環境配置配置屬性註入到對象

num java red component 配置 cati 定義 fin row 項目工程結構: 配置文件application.properties文件 com.yucong.blog.name=yucong com.yucong.blog.title=Spring

spring boot 通過session 取數據

get ets attribute ini () fig spring boot word //先把數據存入session 中  LoginInfo loginInfo = userService.login(userName, password); reque

spring boot 與 thymeleaf (3): 設置屬性、條件、遍歷、局部變量、優先級、內聯語法

負數 使用 cnblogs ttr price n) 原型 demo 解析結果 前面記錄了 thymeleaf 基本表達式, 這裏繼續看一下其他功能. 一. 設置屬性值 這裏的controller, html框架 還是沿用上一篇的部分. html: <div cl

Spring boot 工具類靜態屬性註入及環境配置

data 地址 val 文件 source 其他 username eat serve 由於需要訪問MongoDB,但是本地開發環境不能直接連接MongoDB,需要通過SecureCRT使用127.0.0.2本地IP代理。但是程序部署到線上生產環境後,是可以直接訪問Mong

微服務中基於Spring Bootmaven分散式專案框架的搭建

專案介紹 在微服務架構中,傳統的 maven 專案已經無法滿足,開始走向分散式架構,本專案主要搭建一個空的 maven 分散式架構,可以運用到實際專案中進行擴充套件,可以在文末獲取原始碼和更多資源。 這裡搭建的是基於 maven 的分散式工程,因為在一個專案中,多個微服務是屬於同

微服務中基於Spring Bootmaven分布式項目框架的搭建

struct batis tmpl 接收 modules 展現 子模塊 server face 項目介紹 這裏搭建的是基於 maven 的分布式工程,因為在一個項目中,多個微服務是屬於同一個工程,只不過是提供不同的服務而已,再加上 IDEA 是默認一個窗口打開一個項目工程(

【Oracle】spring boot使用Maven管理Oracle驅動包

由於Oracle授權問題,Maven3不提供Oracle JDBC driver,為了在Maven專案中應用Oracle JDBC driver,必須手動新增到本地倉庫。 手動新增到本地倉庫需要本地有oracle驅動包,有兩種方式獲取oracle的驅動包:一種是去oracle官網下載,另一種是如

idea spring boot 使用maven-plugin外掛打包

首頁需要需改pom.xml檔案的packaging : <modelVersion>4.0.0</modelVersion> <packaging>war</packaging> 新增外部執行的tomcat依賴: <dependenc

Spring Boot使用過濾器Filter

首先建立一個測試用的controller:TestController: package com.zifeiy.test.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.spring

Spring Boot-- Servlet、Filter、Listener、Interceptor

Spring Boot-- Servlet、Filter、Listener、Interceptor 在Spring Boot中讓Servlet、Listener、Filter生效的方法: 在Application上使用@ServletComponentSc