Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put a directory first on the classpath with Spring Boot?

Tags:

spring-boot

I am building my Spring Boot application using Maven, so I can start it with:

java -jar myjar-1.0-SNAPSHOT.jar --spring.profiles.active=prod

I want to have a directory first on the classpath that would allow me to place some files on the filesystem without having to unzip the jar to change them.

I have tried using loader.path, but it does not seem to work.

java -Dloader.path="config/*" -jar myjar-1.0-SNAPSHOT.jar --spring.profiles.active=prod

The config dir is a subdirectory of where the jar is located. I am trying to load a keystore file which is injected as a Resource in my application. There is such a file in the src/main/resources, but that only works in my IDE, not when packaged as a jar. So I want to put a file first on the classpath so that that one is found first on the classpath.

like image 696
Wim Deblauwe Avatar asked May 02 '14 12:05

Wim Deblauwe


People also ask

What is default classpath in spring boot?

By default Spring Boot will serve static content from a directory called /static (or /public or /resources or /META-INF/resources) in the classpath.

What does classpath mean in spring?

This special prefix specifies that all classpath resources that match the given name must be obtained (internally, this essentially happens via a ClassLoader. getResources(...) call), and then merged to form the final application context definition. So classpath: starts at the root of your classpath.

How do I apply different application properties in spring boot?

Properties files are used to keep 'N' number of properties in a single file to run the application in a different environment. In Spring Boot, properties are kept in the application. properties file under the classpath. Note that in the code shown above the Spring Boot application demoservice starts on the port 9090.


1 Answers

You can use loader.path but only if the Main-Class is PropertiesLauncher (so it depends how you built the JAR file). Maybe you need to re-build the JAR with packaging=ZIP in the Boot plugin (e.g. docs here)? Can you not set the path to the keystore as a "file:" URL?

like image 72
Dave Syer Avatar answered Sep 18 '22 18:09

Dave Syer