【问题标题】:Get Maven basedir from a properties file in a SpringBoot project从 SpringBoot 项目的属性文件中获取 Maven basedir
【发布时间】:2019-10-04 00:37:47
【问题描述】:

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

【问题讨论】:

  • “没有好的结果”是什么意思。 ?实际结果是什么?
  • 你可以试试这样 keystore.file=./web/src/test/resources/myfile.jks。它来自当前目录。
  • @nephewtom 你能告诉我你的maven项目结构吗?我在这里添加了示例github.com/sbzDev/stackoverflow/tree/master/question56173541
  • @SergeyBzhezitskiy 我的意思是运行 JUnit 无法理解文件路径,而使用绝对路径它可以工作。
  • @SergeyBzhezitskiy 好吧,真正的项目多了一层。它有 myproject/pom.xmlmyproject/web/pom.xml,这是 JUnit 所在的位置。

标签: java maven spring-boot junit properties-file


【解决方案1】:

在尝试了不同的事情之后,找到了解决我问题的方法。
在 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.

【讨论】:

  • 我尝试在 bootstrap.yml 文件中使用相同的,得到异常 Caused by: while scanning for the next token found character '@' that cannot start any token. (Do not use @ for indentation)。这个问题有解决办法吗?
  • 不知道,抱歉。
猜你喜欢
  • 2020-12-04
  • 1970-01-01
  • 2011-01-28
  • 2017-11-18
  • 2013-05-07
  • 1970-01-01
  • 1970-01-01
  • 2015-07-22
  • 2019-10-10
相关资源
最近更新 更多