Spring Boot external Tomcat and external application.properties - java

i created a new spring boot application.
For development i use the default "appilcation.propperties" for my configuration.
But for production i want to have a external configuration file, because this application will run on multiple tomcats. So i want to have a "application.properties" on every tomcat.
EG:
"SERVER/TOMCAT1_ROOT/conf/application.propperties"
"SERVER/TOMCAT2_ROOT/conf/application.propperties"
Now i thought i can add this path dynamicly in my default "application.propperties":
spring.config.additional-location=file:${CATALINA_HOME}/conf/application.properties
But this line is not parsable. So is there any way to include a path dynamicly?

Related

SpringBoot application with multiple application.properties running under Tomcat

I need two application.properties in my Spring Boot App.
I know that using the annotation #PropertySource I can specify more than 1 property files.
I tried to use: #PropertySource({"classpath:application.properties","classpath:external.properties"})
The idea of it is having application.properties with the machine independent properties and this file will be included inside the war file.
The other file (external.properties), will leave in the machine, and won't be included in the war file. Here I want to leave properties like the database connection and so on.
I've already changed catalina.properties for adding the external.properties location into the classpath, but unfortunately when running on Eclipse it doesn't work (complains about the missing database properties.).
If the external properties file will be available in a known location on the machine, then have an environment variable, system property, or command-line argument set up with the path to the file. Then, reference the file in you #PropertySource annotation using file: rather than classpath:
Example: #PropertySource("file:${CONF_DIR}/external.properties")
References:
Spring boot docs on external configuration
PropertySource documentation
Blog post regarding PropertySource

maven: can't override server.port for tomcat server

I am new with Maven/SpringBoot and trying to deploy a repository with different Tomcat Server port.
By default, I would be happy to run tomcat on :8080. But today, I wanted to add Jenkins pipelines to my project and I deployed tomcat on :8080 (with jenkins on it) before my spring cloud gateway repository.
Now, once I try to deploy gateway, compiler obviously says address :8080 already in use.
Now, I want my gateway to deploy Tomcat on another port, (or use already-existing tomcat on :8080 if possible?) so I wanted to deploy it using this command:
$ mvn spring-boot:run -Dserver.port=8181
However, same error based on :8080 happens to appear:
[ERROR] Failed to execute goal
org.springframework.boot:spring-boot-maven-plugin:1.5.8.RELEASE:run
(default-cli) on project crw-gateway: An exception occurred while
running. null: InvocationTargetException: Connector configured to
listen on port 8080 failed to start.
I tried putting server.port=8080 to application.properties or application-dev.properties files but I cant override it.
Any ideas? How can I override the port? Is there a possibility that I can use already existing tomcat-server on :8080?
Thank you for your time!
EDIT: I had my configurations under ~/config folder. There, I had gateway.properties, which included the line server.port=8080. It has overridden the command line interface as the accepted answer asserts. Changing it to 8888 worked.
According to Spring Boot documentation :
Spring Boot uses a very particular PropertySource order that is designed to allow sensible overriding of values. Properties are
considered in the following order:
Devtools global settings properties on your home directory (~/.spring-boot-devtools.properties when devtools is active).
#TestPropertySource annotations on your tests.
#SpringBootTest#properties annotation attribute on your tests.
Command line arguments.
Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).
ServletConfig init parameters.
ServletContext init parameters.
JNDI attributes from java:comp/env.
Java System properties (System.getProperties()).
OS environment variables.
A RandomValuePropertySource that has properties only in random.*.
Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants).
Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants).
Application properties outside of your packaged jar (application.properties and YAML variants).
Application properties packaged inside your jar (application.properties and YAML variants).
#PropertySource annotations on your #Configuration classes.
Default properties (specified by setting SpringApplication.setDefaultProperties).
So your problem is that your command line (4.) can not override the application.properties file configuration (13., 14., 15.).
If you want to override the server.port property, you need to ensure to respect this order in your configuration.

Getting datasource from tomcat context.xml instead of application.properties

I have a question about deploying a springboot app. Is it possible to get the data source settings from tomcat context.xml resources instead of the application.properties file?
In this project, I already have a war deployed in tomcat and it uses the data source properties from context .xml of tomcat (it is maven project it and it uses hibernate to access the database).
I'd like to do the same thing but using springboot.
You can use the jndi name as you use now like this in spring boot:
spring.datasource.jndi-name=java:comp/env/jdbc/mydb
that part is the actual name
jdbc/mydb

Specify spring config file from default config file

I'm building a war package which will be deployed into a web container using spring boot. And I want to put the spring boot config file outside the war file. My idea is to put the file location in System Property and specify it from spring boot's default config file in directory resources, something like this:
spring.config.additional-location: ${encryption.home}/
or
spring.config.location: ${encryption.home}/
But neither works, seems it doesn't load the external config file. How can I do this? Or whether there's another approach for me to put spring boot config file (not in classpath) outside a WAR file.

when deployed Spring Boot war in tomcat where should application.yml file should be placed?

When spring boot,mvc war file has been deployed in stand alone tomcat, application.yml is located at $CATALINA_HOME/webapps/demo/WEB-INF/classes/application.yml
Can I move the same to $CATALINA_HOME/conf/Catalina/localhost/dmo.yml ?
In general without using spring boot my configuration file will be located at $CATALINA_HOME/webapps/demo/META-INF/context.xml which can be moved to $CATALINA_HOME/conf/Catalina/localhost/demo.xml
Is the same is possible for application.yml also? Then How?
This will help while re-deploying my config file will be backed-up automatically.
You can change the default location for application.properties/yml files using spring.config.location and spring.config.name properties and their corresponding environment variables. The easiest way is to set the SPRING_CONFIG_LOCATION environment variable:
export SPRING_CONFIG_LOCATION=$CATALINA_HOME/conf/Catalina/localhost/dmo.yml
Check out spring boot documentation for more info on loading property files.

Categories