Spring boot jar file, not reading application.properties in config folder - spring-boot

I have a jar file from a Spring Boot project created with "mvn clean install". I created a folder named "config" and placed the "application.properties" in that folder
However the application is still not using the external application.properties file, but it's using the one inside the jar file. How can I make it use the external application.properties if it exists.

Related

Exclude a file from dependency

I am using jython-slim-2.7.2.jar in my Spring Boot application. This JAR file contains a file I don't need. I want to remove that file without manually deleting it (by opening the JAR from local repository and then deleting the file). How can I configure Maven to delete that file while building executable JAR?

how to move spring boot config file bootstrap.yml out of resource package

I'm using spring boot, the config file bootstrap.yml located at src/main/resources. When I build a jar file, the file bootstrap.yml was also included in the jar. That caused I can not modify the file once the jar was build.
So how should I move the bootstrap.yml file out of the jar, I tried remove it out and using -classpath to indicate the file, but it doesn't work. I tried command as following:
java -classpath /user/local/bootstrap.yml -jar spring_test.jar
You can place bootstrap.yml outside the jar simply, and startup the application with regular command such as
nohup java -jar xx.jar >nohup.log &
It will work as you wish, and config in this file is loaded with higher precedence than that one (also name as bootstrap.yml) inside the jar.

spring boot jar not picking profile specific properties outside jar

I have a spring boot jar(name : myjar) located in the directory structure
D:/hello/myjar
The fat jar contains profile specific application.properties in src/main/resources for e.g. application-local.properties
I want to override the attributes defined in the application-local.properties inside the jar with application-local.properties outside the jar
Hence I created application-local.properties and kept it in the same folder as jar i.e. D:/hello
However when I run my jar using the command :
java -jar -Dspring.profiles.active=local D:/hello/myjar.jar
it still picks the properties which is inside the jar. Am I missing something ?
Try this use -D before -jar
java -Dspring.profiles.active=local -jar D:/hello/myjar.jar
let's see https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config
I think it was more of point that from where I was running my jar from the command prompt.
If I run the jar from the folder where my jar is present, it does picks the profile specific files present outside the jar.

Springboot externalizing log4j configuration

In a springboot application, I have a single jar, and then a subdirectory config with application.properties, applicationContext.xml, and log4j...properties file.
I am trying to externalize the log4j config. The application.properties is externalized this way.
But, when springboot runs it is using the log4j config file from within the jar file. By using the -Dlog4j.debug option I can see that log4j uses my external file first, but when spring starts it overrides the log4j setting with the one in the jar.
here is an example startup (with all options)
java -Dlog4j.debug
-Dlogging.config="file:/opt/config/log4j-qa.properties"
-Dlog4j.configuration="file:/opt/config/log4j-qa.properties"
-jar /opt/myjarName.jar
--spring.config.location=/opt/config/
on first startup log4j states
log4j: Reading configuration from URL file:/opt/config/log4j-qa.properties
then on springboot start
log4j: Reading configuration from URL jar:file:/opt/dms-events-api.jar!/log4j-qa.properties
but I want it to read only the external file file:/opt/config/log4j-qa.properties
resolution:
In our application we had line
#ImportResource("classpath:applicationContext.xml")
which then defined the log4j properties file from the classpath:
the simple solution
1. create a /config directory at the root of the api application and put the properties files there
2. remove the ImportResource line, it isn't needed now
3. add a line to the the application.properties file
logging.config=file:config/log4j-${our environment var}.properties
the explanation
By creating a /config directory at the root of the project then
we can work in eclipse as usual and find our properties files.
--and then to externalize configs
simply add a config directory off of where the application jar is and put properties files there.
Two problems are there:
Configuration for externalise: - Tried and works below one for me in spring boot jar
-Dlog4j.configuration=file:/Users/test/Any-Folder/log4j.properties
Spring logging takes over - for that you need to exclude the logging module. PFB the config for Gradle build.
configurations {
all*.exclude module : 'spring-boot-starter-logging'
}

How to put a directory first on the classpath with 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.
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?

Resources