7

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)
nephewtom
  • 2,941
  • 3
  • 35
  • 49
  • what did you mean 'With no good results.' ? what is actual result? – Sergey Bzhezitskiy May 16 '19 at 17:20
  • Can you try like this keystore.file=./web/src/test/resources/myfile.jks. It is from current directory. – Sambit May 16 '19 at 17:26
  • @nephewtom could you point me on the your structure of maven projects? I have added the example here https://github.com/sbzDev/stackoverflow/tree/master/question56173541 – Sergey Bzhezitskiy May 16 '19 at 17:47
  • @SergeyBzhezitskiy I mean that running a JUnit fails to understand the file path, whereas with the absolute path it works. – nephewtom May 16 '19 at 19:24
  • @SergeyBzhezitskiy Well, the real project has one more level. It has `myproject/pom.xml` and `myproject/web/pom.xml`, which is where the JUnits reside. – nephewtom May 16 '19 at 19:33
  • @Sambit I already tested that and reports: `Caused by: java.io.FileNotFoundException: .\web\src\test\resources\myfile.jks (The system cannot find the path specified) at java.io.FileInputStream.open0(Native Method) ` – nephewtom May 16 '19 at 19:40

1 Answers1

8

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.

nephewtom
  • 2,941
  • 3
  • 35
  • 49
  • I tried using the same in bootstrap.yml file, getting the exception `Caused by: while scanning for the next token found character '@' that cannot start any token. (Do not use @ for indentation)`. Is there any solution for this issue? – Paramesh Korrakuti Jun 12 '20 at 06:08
  • Do not know, sorry. – nephewtom Jun 12 '20 at 09:09