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.
Related
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
I'm new to Spring Boot.
Under 'resources' there are 2 files :
-- resources
-- application.properties
-- log4j2.xml
In development environment everything works fine.
In production environment, I copy both files and put them next to the app.jar :
-- app_folder
-- my-app.jar
-- application.properties
-- log4j2.xml
When I start the my-app.jar, :
application.properties is read from app_folder, as intended
log4j2.xml is read from 'resources', the one under app_folder is ignored
Shouldn't it work this way out of the box ? What am I doing wrong ?
All I had to do was putting a file named
name-of-my-spring-boot-jar-file.conf
in the same directory as the jar file itself.
Content of conf file :
JAVA_OPTS="-Dlog4j.configurationFile=/home/<user>/log4j2.xml"
Why do you assume it works that way?
It is true that according to its documentation Spring Boot will detect application.properties correctly if it is placed in the same directory as the jar file.
That being said log4j2.xml is not read by Spring Boot but by Log4J2 Framework and according to its documentation that framework oly looks for files on the classpath.
If the file is elsewhere you need to specify the path like this:
java -Dlog4j.configurationFile=path/to/log4j2.xml -jar my-app.jar
Edit:
my-app.jar is an executable, so I don't invoke the 'java' command when starting my-app.
Yes you do. Even if you're invoking it from a GUI (eg. double clicking in Explorer in Windows) it still runs java -jar my-app.jar under the hood.
Isn't the same folder the app.jar resides in considered classpath ?
Again, why would you assume that it is?
In the Java documentation (here for Java 8) in section Folders and Archive Files it clearly says that if the classes are stored in a jar, then the classpath includes only stuff from the jar (although in case of Spring Boot due to the custom classloader it also includes jars embedded in the jar, whch would normally not be the case - see Executable Jar Format).
You really should read the documentation (or at least relevant parts of it) before attempting to use any framework/library/programming language you have not used before - it will save you a lot of time in the long run.
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 /.
Currently I can build my Gradle-based Spring Boot app like so:
./gradlew build && java -Dspring.config=. -jar build/libs/myapp.jar
And this works fine provided I have an application.yml in the root of my project directory.
However, I would now like to have both an application-local.yml as well as an application-dev.yml, and to specify which one to use when I build + run myapp.jar.
How can I specify either file at startup?
You can use Spring boot's capability of using Profile Specific property file.
You can specify the application yml inline with your profile name
application-[profile].yml. In your case, it would be
application-dev.yml
application-local.yml
Specify the profile you would want to use as a command line argument
-Dspring.profiles.active=dev
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.