从SpringBoot项目的属性文件中获取基于Maven的内容

从SpringBoot项目的属性文件中获取基于Maven的内容

问题描述:

我继承的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.