maven filter不起作用
遇到的一个坑, spring boot + maven maven fileter没有起作用。spring boot把默认占位符改了
参考:https://blog.csdn.net/mn960mn/article/details/78834875
其中,pom.xml的内容如下:
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.pp.test</groupId>
- <artifactId>properties-test</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
- <name>properties-test</name>
- <url>http://maven.apache.org</url>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.5.9.RELEASE</version>
- </parent>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <maven.compiler.source>1.8</maven.compiler.source>
- <maven.compiler.target>1.8</maven.compiler.target>
- <java.version>1.8</java.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- </dependencies>
- <build>
- <filters>
- <filter>src/main/profiles/profile-${profiles.active}.properties</filter>
- </filters>
- <resources>
- <resource>
- <filtering>true</filtering>
- <directory>src/main/resources</directory>
- </resource>
- </resources>
- </build>
- <profiles>
- <profile>
- <id>dev</id>
- <properties>
- <profiles.active>dev</profiles.active>
- </properties>
- <activation>
- <activeByDefault>true</activeByDefault>
- </activation>
- </profile>
- <profile>
- <id>test</id>
- <properties>
- <profiles.active>test</profiles.active>
- </properties>
- </profile>
- <profile>
- <id>prod</id>
- <properties>
- <profiles.active>prod</profiles.active>
- </properties>
- </profile>
- </profiles>
- </project>
application.properties 文件内容:
- jdbc.driverClassName=${jdbc.driverClassName}
- jdbc.url=${jdbc.url}
- jdbc.username=${jdbc.username}
- jdbc.password=${jdbc.password}
profile-dev.properties 文件内容:
- jdbc.driverClassName=com.mysql.jdbc.Driver
- jdbc.url=jdbc:mysql:///db_users
- jdbc.username=root
- jdbc.password=root
启动应用,报错了,错误如下:
- Caused by: java.lang.IllegalArgumentException: Circular placeholder reference 'jdbc.url' in property definitions
- at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:141) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
- at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:162) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
- at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
- at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:236) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
- at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
- at org.springframework.core.env.AbstractPropertyResolver.resolveNestedPlaceholders(AbstractPropertyResolver.java:227) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
- at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:84) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
- at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:61) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
- at org.springframework.core.env.AbstractEnvironment.getProperty(AbstractEnvironment.java:527) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
- at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$1.getProperty(PropertySourcesPlaceholderConfigurer.java:132) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
- at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$1.getProperty(PropertySourcesPlaceholderConfigurer.java:129) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
- at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:81) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
- at org.springframework.core.env.PropertySourcesPropertyResolver.getPropertyAsRawString(PropertySourcesPropertyResolver.java:71) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
- at org.springframework.core.env.AbstractPropertyResolver$1.resolvePlaceholder(AbstractPropertyResolver.java:239) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
- at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:147) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
- at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
- at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:236) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
- at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
- at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:172) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
- at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:831) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
- at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1086) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
- at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
- at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
- at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
- at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
- ... 17 common frames omitted
报这个错误的原因是,maven的filtering没有起作用,没有把占位符给替换掉。(大家可以执行mvn clean package,看看打包后的jar里面的application.properties文件,是否有替换占位符)
filtering无效的原因是,pom.xml继承了spring boot的依赖
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.5.9.RELEASE</version>
- </parent>
点开这个依赖的pom.xml,我们发现
spring boot把默认的占位符号${}改成了@
找到原因了,那怎么解决就很简单了。
方法一:
在pom.xml里面添加如下内容
- <properties>
- <resource.delimiter>${}</resource.delimiter>
- </properties>
方法二:
application.properties里面不用${},改成@
- jdbc.driverClassName=@jdbc.driverClassName@
- jdbc.url=@jdbc.url@
- jdbc.username=@jdbc.username@
- jdbc.password=@jdbc.password@
方法三:
pom.xml不继承spring-boot-starter-parent,dependency里面配置全部的依赖和版本号(继承了之后,很多依赖不用写version)
方法四:
把
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.5.9.RELEASE</version>
- </parent>
改成
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- <version>1.5.9.RELEASE</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
maven filter不起作用的更多相关文章
- maven filter插件只替换了部分变量问题
maven filter简介 maven的resources插件,有一个filter的作用,能够在打包的时候,从特定文件里读取key-value对,替换配置文件中的占位符变量.很多线上线下有不同环境的 ...
- Maven Filter与Profile隔离生产环境与开发环境
Maven Filter与Profile隔离生产环境与开发环境 在不同的开发阶段,我们一般用到不同的环境,开发阶段使用开发环境的一套东西,测试环境使用测试环境的东西,可能有多个测试环境,生产环境使用的 ...
- maven filter 乱码,MalformedByteSequenceException: Invalid byte 3 of 3-byte UTF-8 sequence.
<plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactI ...
- 用 maven filter 管理不同环境的配置文件
使用 maven profile 一个项目可以部署在不同的环境当中,maven 的 profile 针对不同的环境指定各自的编译方法.在 pom.xml 的 profile 中,可以根据不同的环境定制 ...
- Maven介绍,包括作用、核心概念、用法、常用命令、扩展及配置
由浅入深,主要介绍maven的用途.核心概念(Pom.Repositories.Artifact.Build Lifecycle.Goal).用法(Archetype意义及创建各种项目).maven常 ...
- org.springframework.web.filter.DelegatingFilterProxy的作用
一.类结构 DelegatingFilterProxy类继承GenericFilterBean,间接实现了Filter,故而该类属于一个过滤器.那么就会有实现Filter中init.doFilter. ...
- spring security 4 filter 顺序及作用
Spring Security 有两个作用:认证和授权 一.Srping security 4 filter 别名及顺序 spring security 4 标准filter别名和顺序,因为经常要用就 ...
- [转]Maven介绍,包括作用、核心概念、用法、常用命令、扩展及配置
转自:http://www.trinea.cn/android/maven/ 两年半前写的关于Maven的介绍,现在看来都还是不错的,自己转下.写博客的一大好处就是方便自己以后查阅,自己总结的总是最靠 ...
- Maven 的classifier的作用
直接看一个例子,maven中要引入json包,于是使用了 <dependency> <groupId>net.sf.json-lib</groupId> <a ...
随机推荐
- redis 3.0 集群__故障测评
一, slave 是不能通过redis-cli 直接进行读写操作的,但是可以执行 keys, info 命令( 猜测类似全局性的不影响到原子性操作的命令应该都可以,没有一一试验) 二,集群中的某节点异 ...
- 如何使用MySQL Workbench创建数据库存储过程
方法/步骤 1 打开My SQL Workbench 2 右击要启动的my SQL数据库连接,然后选择[connection to database] 3 此时进入数据库实例管理界面,下方显示 ...
- ExclusiveTouch
Setting this property to true causes the receiver to block the delivery of touch events to other vie ...
- css3 -webkit-filter
-webkit-filter是css3的一个属性,Webkit率先支持了这几个功能,感觉效果很不错.下面咱们就学习一下filter这个属性吧. 现在规范中支持的效果有: grayscale 灰度 ...
- unix高级编程阅读
一.进程: fork,exec,waitpid 1.子进程:复制进程的代码与堆栈状态,因此子进程将会从刚执行的指令fork位置继续往下执行. 2.父进程通过waitpid等待子进程完成 二.线程: 1 ...
- 2016级算法第三次上机-F.ModricWang的导弹防御系统
936 ModricWang的导弹防御系统 思路 题意即为:给出一个长度为n的序列,求出其最长不降子序列. 考虑比较平凡的DP做法: 令\(nums[i]\) 表示这个序列,\(f[x]\) 表示以第 ...
- web服务的简单介绍及apache服务的安装
一,web服务的作用: 是指驻留于因特网上某种类型计算机的程序,可以向浏览器等Web客户端提供文档.可以放置网站文件,让全世界浏览: 可以放置数据让全世界下载.目前最主流的三个Web服务器是Ap ...
- 决策树C4.5算法——计算步骤示例
使用决策树算法手动计算GOLF数据集 步骤: 1.通过信息增益率筛选分支. (1)共有4个自变量,分别计算每一个自变量的信息增益率. 首先计算outlook的信息增益.outlook的信息增益Gain ...
- C语言中malloc函数与free函数
- malloc函数 全称是memory allocation,中文叫动态内存分配,用于申请一块连续的.指定大小的内存块区域以void*类型返回分配的内存区域地址,当无法知道内存具体位置的时候,想要绑 ...
- 让EntityFramwork自动更新表结构
在项目开发中,难免会遇到数据库表结构变化的情况,手动去维护数据库是一件繁琐的事情.好在EntityFramwork为我们这些懒人提供了可供自动更新数据结构的机制,废话不多说,直接上代码: 首先创建一个 ...