Fetch application properties from an embedded jar - Spring Boot - java

I have a dependent jar file which I have added as a dependency. Is there a way to fetch use the application.properties file present in the dependent jar?

Let the property spring.config.location point to that application.properties
like
spring.config.location=classpath:com/company/whatever/application.properties
You can set spring.config.location on the command line when starting the application
$ java -jar myproject.jar --spring.config.location=classpath:com/company/whatever/application.properties
You find this explained in the spring boot docs

Related

How to override Java resources when you run Spring Boot fat-jar?

How to override Java resources when you run Spring Boot fat-jar?
I've created a Spring Boot far-jar that contains inside as resource a log4j.xml configuration file. Now when I run the fat-jar I'm trying to override it in this way
$ java -cp conf/ -jar target/myapp.jar
and I've put in the conf/ folder a new log4j.xml. But nothing, it continues to use the resource inside the jar.
If your goal is only to define your own log4j.xml configuration file, this could help:
java -Dlogging.config='/path/to/log4j2.xml' -jar target/myapp.jar
(this was mentioned already in How can I change the default location of log4j2.xml in Java Spring Boot? )
If you just want to add resources by classpath addition you could refer to
https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-executable-jar-format.html#executable-jar-property-launcher-features
where I found loader.path :
loader.path can contain directories (which are scanned recursively for
jar and zip files), archive paths, a directory within an archive that
is scanned for jar files (for example, dependencies.jar!/lib), or
wildcard patterns (for the default JVM behavior). Archive paths can be
relative to loader.home or anywhere in the file system with a
jar:file: prefix.

Spring boot - Default properties file not being read when using profile

I have a spring boot 2 application with 2 property files:
application.properties
application-dev.properties
When I run the application in IntellijIdea using the dev profile, Spring reads both the profile specific file and the default file.
Once I build an executable jar file, I created a new application-prod.properties outside of the jar, with production environment properties and execute the application with --spring.profiles.active=prod expecting it to read this file AND the default application.properties, but it is only reading the file outside the jar (application-prod.properties).
Should Spring Boot read both files or am I expecting something that it does not do by default?
If it should, what can I be missing?
As mentioned by Michael, Spring boot supports property hierarchies.
I tried this and it works as expected, you can find a reference project in here
A few things that I can think about:
Extract the contents of your jar file and confirm that the file named application.properties is bundled in there.
You may be overwriting the spring.config.location property and using filenames instead of directories
From Spring Boot documentation
If you have specified any files in spring.config.location, profile-specific variants of those files are not considered. Use directories in spring.config.location if you want to also use profile-specific properties.
If you override the default spring.config.location then the application may not be able to find the default application.properties file.
By default, application.properties will be used to run the Spring Boot application. While running the JAR file, you need to specify the spring active profile. only one file will be read

Search application.properties in directory external to jar spring boot

There is an option to disregard the spring boot jar package application.properties file and
search the properties values ​​of the application.properties from a directory on the server.
On the server where we run the jar application with spring boot, I need the application to look up the properties values ​​of the application.properties file in another directory on the server, disregarding the existing BOOT-INF\classes\application.properties directory in the jar.
I would like to make an option to disregard the application.properties in jar spring boot generation and fetch the values ​​of an application.properties from a directory on the server.
This configuration would be for the application to work for each environment (production, approval and development) without the need for the developer to change the profiles in the application before the packaging.
Any examples of this setup?
Taken from the Spring documentation, you can pass inthe location of your externalised config using this:
java -jar myproject.jar --spring.config.location=/path/application.properties
This command will support directories, provided spring.config.name matches the name of your config file.

Spring Boot configuration from application.properties

I see that the values in application context are not getting picked up when the Spring Boot project is war file and runs on an external Tomcat.
How do I resolve this issue?
Example - server.port=8096 but the port when loaded is still 8080
You may need to move the properties file outside of your project. The application.properties under src/main/resources will remain but referencing the external properties file. For e.g.
spring.config.location=./tomcatfolder/external-application.properties

Run java -jar and also specify the configuration path

A standalone(spring boot)jar can be run like this:
java -jar springboot.jar
How can I configure the configuration path as well in the command above ?
Like this below: ?
java -jar springboot.jar --configuration:/some-path-here
n.b. My configuration is external.
Thanks!
By default, Spring Boot will look in the working directory for external application.properties and application.yml files. For example, if you start your Spring Boot app in the same directory as the JAR:
java -jar myapp.jar
Then this will look for application.properties (or application.yml or application-{profile}.properties etc.) in the same directory as your JAR file. This means if you set up your directory like so:
myapp/
| - myapp.jar
| - application.properties
And start the app within that directory, then it will automatically pick up the application.properties there.
If this isn't an option for you, the spring.config.location property can be specified on the command to give it additional locations to use when looking for configuration files, e.g.:
java -jar myapp.jar --spring.config.location=file:/etc/myapp/
For more info, read up on Spring Boot's Externalized Configuration documentation.
As per Spring docs:
java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
Use file instead of classpath to specify external configuration file.

Categories

Resources