从SpringBoot项目的属性文件中获取基于Maven的内容 [英] Get Maven basedir from a properties file in a SpringBoot project

查看:565
本文介绍了从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.

这篇关于从SpringBoot项目的属性文件中获取基于Maven的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆