Get Maven basedir from a properties file in a SpringBoot project

喜夏-厌秋 提交于 2019-12-24 12:58:25

问题


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.

A config.properties file has:

keystore.file=C:/Users/tom/myproject/web/src/test/resources/myfile.jks

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.

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)

回答1:


After trying different things, found a solution to my problem.
Adding this line in Spring Boot application.properties:

maven.basedir=@project.basedir@

And then in config.properties:

keystore.file=${maven.basedir}/web/src/test/resources/myfile.jks

Makes it work.
Check: Spring Boot automatic Property Expansion Using Maven.



来源:https://stackoverflow.com/questions/56173541/get-maven-basedir-from-a-properties-file-in-a-springboot-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!