OmriYaHoo OmriYaHoo - 1 year ago 93
Java Question

Spring Boot Jar Execution internal properties files

I have a Spring Boot application that runs properly when I run it from Intellij as maven configurations.

I have a project structure of environments that is defined with properties files.

resources/conf/dev/environment.properties
resources/conf/qa/environment.properties
resources/conf/general.properties


etc.

Our framework works in a way that we are choosing the env with VM arguments.
for example
-Denv=dev
or
-Denv=qa


After packaging the App to an executable JAR and trying to run it, Spring Boot can't identify the properties files under
conf
path of the project.

When I look inside the JAR the properties files are under
{jar-name}.jar\BOOT-INF\classes\conf
.

The error is:

java.io.FileNotFoundException: conf\general.properties (The system cannot find the path specified)


2017-07-11 10:52:52.864 INFO 17896 --- [main] n.lifecycle: Can't find configuration file [conf\general.properties]
2017-07-11 10:52:52.865 ERROR 17896 --- [main] n.lifecycle: configuration file [null] not found (use default properties as error handling)


I've tried to work with the documentaion here: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
but nothing seems to fix it.
Also tried to work with this guide - http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#executable-jar-launching but also failed...

The guides are referring to the properties files as "external" but my properties are packed in the JAR.

Answer Source

I've found out what's wrong. Our in-house framework that's loading the resources are using File.separator to read the path of the resources.

Now for some reason when the jar is created with maven spring boot plugin the classpath is built with '/' for example /C:/Users/MyUser/Projects/MyApp/target/MyApp-1.0.1.jar!/BOOT-INF/classes!/ (the resources are in classes path inside of the jar of course)

And when we are trying to run the jar it's trying to read the resources with '\' so the path that is built is (if we choose "dev" for example) /C:/Users/MyUser/Projects/MyApp/target/MyApp-1.0.1.jar!/BOOT-INF/classes!/conf\dev and that is why the app is failing to load.

I still don't know why this is happening.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download