简体   繁体   中英

Java Spring application.propperties environment variables or default

I'm trying to make a complete development to the production pipeline.

In this scenario, I want to have a Jenkins job deploy the Java Spring application once a new commit is done.

An example of some of my applications.propperties is:

spring.datasource.url=jdbc:mariadb://localhost:3306/application-api?serverTimezone=CET
spring.datasource.username=USERNAME
spring.datasource.password=PASSWORD

Once you have this in a git repository your credentials are stored and visual for everyone. I don't have the plan to make anything public yet but seems like a bad idea to store application.properties with passwords on a git repository.

So best case scenario I want something like this, where I can set environment vars on my deployment server: spring.datasource.username=${DB_PASS}:PASSWORD In case of environment variable DB_PASS doesn't exist

I'm stuck on how to achieve this. I have seen the usage of @Value but I'm unaware of how to use that with given application properties.

You can use all properties also as environment variables.

Simply make the name uppercase and replace . by _

SPRING_DATASOURCE_URL=jdbc:mariadb://localhost:3306/application-api?serverTimezone=CET
SPRING_DATASOURCE_USERNAME=USERNAME
SPRING_DATASOURCE_PASSWORD=PASSWORD

Please read the documentation: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config

Why are you storing the github credentials in application.properties?

Consider connecting Jenkins to github via Webhooks:

Here is the plugin for Jenkins

Here is a tutorial on setting up and using Github webhooks with Jenkins

EDIT 3/24/2020: You can consider having a properties file that is external to the project and add it to the classpath (simply don't commit it) using something like @PropertySource . Further, once you build your project, you can place a properties file in the same directory and it will be picked up by default when you start your spring boot application.

Example:

C:<path to project>/target/project.jar

C:<path to project>/target/custom-properties.properties

then

custom-properties.properties

will be loaded on start up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM