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
Related
At the moment, all my properties are defined in the file src/main/resources/application.properties. However, I would like to have properties files relating to different profiles in the src/main/resources/config folder, and I want to be able to choose any of them. such as:
application-DEV.properties
application-TEST.properties
application-SERVER1.properties
So, the question is how to select these properties. If I was compiling to a jar file, I could do that easily by specifiying the profile when running the jar file, but here I just copy the generated war file to a Tomcat webapps directory.
Well, I've found a way to do that. In the conf directory of Tomcat, add this line to the file catalina.properties there.
spring.profiles.active=<YOUR_PROFILE>
Replace <YOUR_PROFILE> here of course with your profile's name. For example if you are using application-TEST.properties, it would be the following.
spring.profiles.active=TEST
You can define Jvm Argument -Dspring.profiles.active=<PROFILE> on server start up file (.bat/.sh) depending on your environment.
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.
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
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
I have developed a web application using spring boot. I have three resource folders in src/main/resources staging,qa,production which consists of application properties and logging configuration for the specific environment. Along with these folders I have application.properties and logging configuration in resources folder which I use it for dev environment. I want to package war file according to the environment using spring-boot-maven plugin. I am new to maven any help would be appreciated?
Convetion is application-{profileName}.properties
Point 10 and 11:
http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
e.g application-test.properties
it overrides the application.properties
Make profile in pom.xml
Introduction to profiles:
http://maven.apache.org/guides/introduction/introduction-to-profiles.html
Rather than using Maven to add different folders to your classpath for each environment, you can use Spring profiles.
First of all create different application.properties for each environment, such as:
application-staging.properties
application-qa.properties
application-production.properties
For the logging you can use the logging.config property. So, in application-staging.properties you could use:
logging.config=classpath:logback-staging.xml
In the other properties files you can use different logging.config properties.
Now just run your application with the spring.profiles.active property.
However, an easier solution would be to use externalized configuration. Rather than having to rebuild each time you want to change configuration for a specific profile, you can externalize it by putting an application.properties file next to your JAR/WAR in the correct environment, rather than on your classpath. Spring boot will pick this up automatically.
Now you can also externalize your logging config by placing a logback.xml (or log4j2.xml, ...) file next to your JAR/WAR and just configure your (externalized) application properties with:
logging.config=file:logback.xml
This allows you to edit your configuration and logging without having to change your JAR/WAR.