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
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 thought I had this working because in my IDE the correct environment properties file was being used, and a local application.properties was being correctly used. I am trying to use profile specific properties to determine various values throughout my Spring-Boot 1.3.5 app.
My src/main/resources/application.properties is:
spring.profiles.active=test
server.port=8080
deploy.server=http://localhost
liquibase.change-log:classpath:/db/changelog/db.changelog-master.xml
liquibase.check-change-log-location=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
logging.file=myapp.log
spring.jackson.serialization.write_dates_as_timestamps=false
And I have a src/main/resources/application-test.properties:
server.port=8081
deploy.server=https://test.myapp.com
logging.level.liquibase:TRACE
logging.level.org.springframework.security:DEBUG
Now I apparently have two problems:
After I run "gradle clean build" I get my jar and trying to run via:
java -jar build/libs/app.jar -Dspring.profiles.active=local
I see the message:
The following profiles are active: test
So it picking that up from the application.properties file and I can't seem to override it, which I think is causing my second problem:
My Jenkins build fails when I try to use a parameterized build with the values:
spring.profiles.active set to "test".
The error I get is Tomcat in a failed state. If I take that parameter out of the build, it works. But that build will not run from the command line unless I copy the application-{env}.properties to the run directory (which might be best practice anyway?).
I am not sure if the problem is the location of the properties files or the way I am trying to build them.
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.
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.
I am using Springboot in my project, i start an embedded Springboot application inside my App. My app is already having a crashHub which is conflicting with springBoot's crashHub, so how i can change the springBoot's crashHub port, I don't want to do it through a XML File.
There's an extensive list of configuration properties in Spring Boot's documentation, including those for configuring the ports used by the remote shell: shell.ssh.port and shell.telnet.port.
You should configure one or both of these properties in your application.properties file in src/main/resources.
Additionally, you can do it by specifying parameter to java:
-Dshell.ssh.port=<your-port>
Or command line argument for you application:
--shell.ssh.port=<your-port>