我继承的Maven项目在JUnits中使用了一些资源文件。
这些文件在属性文件中称为绝对路径。
假设Maven项目位于myproject
主pom.xml
下。config.properties
文件具有:
keystore.file=C:/Users/tom/myproject/web/src/test/resources/myfile.jks
我想从Maven项目的相对路径中引用该资源。
所以我一直在尝试类似的东西:
keystore.file=${project.basedir}/web/src/test/resources/myfile.jks
我一直在阅读有关resource filtering的this question的信息。
该项目使用SpringBoot,并且在运行JUnit时抱怨:
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
中添加以下行:
maven.basedir=@project.basedir@
然后在
config.properties
中:keystore.file=${maven.basedir}/web/src/test/resources/myfile.jks
使它工作。
检查:Spring Boot automatic Property Expansion Using Maven.