Spring boot external properties not working - spring-boot

My project structure looks like as attached file. Even though I have profile specific properties, I would like to run my app with external properties file i.e., outside of jar file.
I tried with following command:
java -jar test_service.jar --spring.config.location=file:///C:/external_props/test.properties
But its taking application-default.properties file.
from log file:
No active profile set, falling back to default profiles: default
Why it is not taking external properties file ?

When you pass --spring.config.location command line argument SpringBoot won't consider application-*.properties files in src/main/resources directory. The filename you mentioned for --spring.config.location is taken as base filename, in your case test. So, it will only load test.properties file from that path you provided as default profile.
If you want to enable certain profile, say prod, you need to create file C:/external_props/application-prod.properties and enable prod profile using --spring.profiles.active=prod.

Spring will automatically look for some property file in a specific location.
From where you execute the jar file, Spring will look in that directory for a property file called application.properties
An other way is to put a config directory in the directory you execute the jar from and put the application properties in there.
There is one more option and that is the -Dspring.profiles.active={profiles} parameter.
Spring will then look in the directory or config directory to application-{profile}.properties
Reference:
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
Also i think you use the
--spring.config.location=file:///C:/external_props/test.properties
is not used correctly for a windows based file path.
Windows uses the \ instead of the /.

Related

Spring: How to access a txt file located outside the jar?

Currently, I am passing the environment specfic configs in a properties file outside the jar using -Dspring.config.location=/apps/conf/application-dev.properties. I also have properties file included in the jar named application.properties which has configs that don't change often. There's one config called nameFile:
nameFile=classpath:config/nameList.txt
And the actual nameList.txt is included in the jar under spring-app/src/main/resources/config/nameList.txt but I want to move the nameList.txt outside the jar.
What I've tried:
Changing it to nameFile =${auth.nameList} and added this to /apps/conf/application-dev.properties like:
auth.nameList=classpath:/apps/conf/nameList.txt
And I moved the nameList.txt file from the jar to /apps/conf/ location but the Spring application fails because it cannot find the txt file. How do I fix this? I want to move the nameList.txt to outside the jar.
Removed classpath:
auth.nameList=/apps/conf/nameList.txt
No luck!
When you provide a path, classpath will "tell" the JVM that the location is within the jar/war file. If you have the resource you need in an external location, you will need file instead.
auth.nameList=file:/apps/conf/nameList.txt

how to set profile-specific applicaiton.properties(spring boot) outside war(jboss)?

I have spring boot application that I am running in jboss(instead of tomcat).
I am using spring profile for loading environment specific application.properties.
Profile specific application{env}.properties is working fine when placed under "src/main/resources/" however, it is not working when placed externally.
I tried setting property in jboss standalone.xml but application fails to start in jboss.
<property name="spring.config.location" value="C:/Dev/config/rt" />
Please suggest how to load the environment specific application.properties files that are not placed inside the war file.
I was expecting spring to pick the profile specific file from the folder but looks like that's not the case.
It appears that spring.config.location needs to have the absolute file location instead of folder location. so, updated spring.config.location in standalone.xml and it worked :
<property name="spring.config.location" value="C:/Dev/config/rt/application-dev.properties" />
You can provide externalized configuration file using below command when you are initially starting the application,
java -jar <your-jar-name.jar> --spring.config.location=<path><external property>
example :
java -jar mySample.jar --spring.config.location=./application-external.properties
read more
Generally it was picked very easily when it's placed under the 'src/main/resources' folder. suppose you have to different files for profiles like - application-dev.properties and application-prod.properties, you need to set only the current working profile in the application.properties like
spring.profiles.active=dev
and it will be picked easily. If it's doesn't you need to create a workaround by creating a new bat or sh file like run.bat and run.sh in the bin folder of the jboss and pass the file location like
--spring.config.location=
The complete command to be added in the bat/sh file will be
java -jar appName.jar --spring.config.name=application-dev --spring.config.location=c:/Dev/application-dev.properties

Spring boot starts even if start with nonexistent profile

I have spring boot application - server.jar with next structure:
-resources
-application.yaml
After the build, I create a folder with a name source. And put my jar to this folder. Also, I create start .bat file
-source
-server.jar
-start.bat
In start.but the file I write next:
java -Dspring.profiles.active="foofoofoo" -jar server.jar
pause
When I run start.bat my server starts with log:
The following profiles are active: foofoofoo
And use properties from application.yaml. I have not profile with name foofoofoo and I have not apllication-foofoofoo.yaml. Why? Why spring writes that it loads foofoofoo profile, load application.yaml and work?
It must crash because I start the application with the nonexistent profile!
Instead, I see a running application with another property! How can I process this?
When you start the application application.yml file in your resources directory is getting included anyway.
If you also have application-yourProfileName.yml in resources directory and add
--spring.profile.active=yourProfileName parameter,
then both property files are getting included, and NOTE that in this case application-yourProfileName.yml override the same properties in application.yml.
The profile can be used in more ways than just application's properties. It does not matter if you do not have application-your-profile.{properties, yml}. Loading profile does not mean loading properties file.

Spring Boot external properties file (with profiles)?

What I am trying to accomplish, is to specify a directory on my application.yml file located directly on the classpath (under /resources). I would like to be able to have something like:
spring:
datasource:
driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver
profiles:
active: dev
config:
location: C:\\app\\configs\\
Then under the c:\settings\configs\ location, I would like to have multiple config files based on the profile being loaded, such as:
c:\app\configs:
application-dev.yml
application-staging.yml
application-production.yml
Using this approach, the IDE would always default to application-dev.yml. When I build the app via gradle, and run it while passing in the command line arguments, I can specify the profile, thus loading the appropriate file. Ideally, being able to do just this:
java -jar -Dspring.profiles.active=staging appliation.jar
All the examples or answers I've seen have shown how to pass this all these config options from the command line only, not using
spring.config.location
and
spring.profiles.active
from within a default config file (eg: application.yml). Preference is to set this up as described above for minimal deployment.
Thank you for any suggestions / help!
You could give it a try and provide your base configuration as "bootstrap.yml" instead of "application.yml". Afterwards, spring boot should choose the right config depending on the chosen profile from your configuration path.
To me, this seems to be the closest solution to your use case, if you do not want to make use of command line arguments.

Build Spring project for run on another system

I Successfully create a spring boot project on my own local system. I want to build a jar file so I can install it on remote server. so I had to configure server address and mySql address of remote server but I can not Build and it have many errors, and they all right cause my system can not see the remote server address and database.
this is my .properties file:
spring.datasource.url=jdbc:mysql://localhost:8081/aths
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=create
server.address=192.168.24.250
server.port=8080
how can handle it for running on another configurations? ( another IP, datasource, and ...)
Am I doing it right or not? thanks
You can use spring profiles here :
Create different property files for different profiles using application-{profile}.properties format, e.g. application-dev.properties for dev, and application-prod.properties for production put your profile specific configurations in them. Then when you're running the spring boot application, activate your intended profile using the SPRING_PROFILES_ACTIVE environment variable or spring.profiles.active system property.
and at the end, you will run your jar file with command
java -jar -Dspring.profiles.active=prod application.jar
You can have different application.properties within your resources folder and use spring profiles for example application-{profile}.properties and run the application with the specified profile. However this still limits the configuration items to what has been hard coded within the properties files. When running the application, if it was to be distributed to other people, where non of the profiles are supported you can provide a properties file at start up.
So in the same directory for example as the .jar file create a file named application.properties with empty place holders for all the variables required for the application so the admin can enter the details correct for them. Then they will be required to start the application with the following command
java -jar 'applicaitonname.jar -Dspring.config.name="file:/path/to/application.properties"
Or springboot will load properties from application.properties files in the following locations:
A /config subdirectory of the current directory.
The current directory
Failing that the default application.properties from the resources folder will be loaded.

Resources