从SpringBoot项目的属性文件中获取基于Maven的内容 [英] Get Maven basedir from a properties file in a SpringBoot project
问题描述
我继承的Maven项目中有一些在JUnits中使用的资源文件.
这些文件在属性文件中称为绝对路径.
假设Maven项目位于主pom.xml
所在的myproject
下.
The Maven project I inherited has some resource files used in JUnits.
Those files are referred in a properties file as absolute paths.
Let's assume the Maven project is under myproject
, where the main pom.xml
resides.
config.properties
文件具有:
keystore.file=C:/Users/tom/myproject/web/src/test/resources/myfile.jks
我想从Maven项目的相对路径中引用该资源.
所以我一直在尝试类似的东西:
I want to refer to that resource from a relative path of the Maven project.
So I have been trying something like:
keystore.file=${project.basedir}/web/src/test/resources/myfile.jks
I have been reading about resource filtering which is referred in this question.
该项目使用SpringBoot,并且在运行JUnit时抱怨:
The project uses SpringBoot, and when running a JUnit, complains with:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'project.basedir' in string value "${project.basedir}/web/src/test/resources/myfile.jks"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
推荐答案
尝试了不同的方法后,找到了解决我问题的方法.
在Spring Boot application.properties
中添加这一行:
After trying different things, found a solution to my problem.
Adding this line in Spring Boot application.properties
:
maven.basedir=@project.basedir@
然后在config.properties
中:
keystore.file=${maven.basedir}/web/src/test/resources/myfile.jks
Makes it work.
Check: Spring Boot automatic Property Expansion Using Maven.
这篇关于从SpringBoot项目的属性文件中获取基于Maven的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!