Search application.properties in directory external to jar spring boot - java

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.

Related

What happens during WAR packaging in spring boot? Can we have both external Tomcat and Embedded Tomcat for a single application?

I actually want to know what exactly happens during WAR packaging of a spring boot application using Maven. My specific interest would be to know whether the embedded tomcat dependency will be included or not while packaging app as WAR.
Also, If we have some tomcat config properties defined in application.properties and deploy the WAR file to external Tomcat which config will be taken into account while running the application? Like the config properties defined in server.xml of Tomcat server or the properties defined in application.properties?
My specific interest would be to know whether the embedded tomcat dependency will be included or not while packaging app as WAR
This is covered in the Spring Boot reference documentation:
If you use the Spring Boot build tools, marking the embedded servlet container dependency as provided produces an executable war file with the provided dependencies packaged in a lib-provided directory. This means that, in addition to being deployable to a servlet container, you can also run your application by using java -jar on the command line.
When you deploy a war to Tomcat, the configuration in server.xml is used rather than the server- and Tomcat-related configuration in application.properties. Specifically, none of your application's server.* properties will have any effect.

How do I use a given application.properties file when deploying a .war on Apache Tomcat?

I have been tasked to deploy a ROOT.war app on Apache Tomcat. Here's what the ROOT.war file looks like on the inside:
The app I was told uses a Postgresql as its database. I already have sorted that one out.
I just want to know how to run this app on Apache Tomcat using this application.properties configuration file they have provided. Here's what it looks like:
Thank you very much and regards,
Jeremy
There are may ways to specify the location of the application.properties file. See Spring Boot Documentation for full list of how properties are loaded.
As described in section 2.3. Application Property Files:
SpringApplication loads properties from application.properties files in the following locations and adds them to the Spring Environment:
A /config subdirectory of the current directory
The current directory
A classpath /config package
The classpath root
It goes on to say:
If you do not like application.properties as the configuration file name, you can switch to another file name by specifying a spring.config.name environment property. You can also refer to an explicit location by using the spring.config.location environment property (which is a comma-separated list of directory locations or file paths).
Of the many options, I'd highlight:
Recommended: According to this answer, you can specify the location in the Tomcat context XML file, although that is not explicitly specified in the Spring documentation:
<Context>
<Parameter name="spring.config.location" value="file:/path/to/config/folder" />
</Context>
Modify the .war file and place the file in the WEB-INF/classes/config or WEB-INF/classes folder.
If you can specify the working directory for the running Tomcat, place the file there or in a config sub-folder.
If you can specify a JVM option for the running Tomcat, add -Dspring.config.location=file:/path/to/config/folder, and place the file there.

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

How to specify a profile when deploying a Spring boot war file to Tomcat?

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.

How to exclude default application.properties add custom properties file using profiles in maven for spring boot project?

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.

Categories

Resources