I need some help to consume another .yml file that complement the original one in the application. Then I have a file filled by me(dev) however some configuration(as database infos) should be inserted by our clients.
The one which is in the application(application.yml) have general configs, the other on path C:\Users\Public\ datasource.yml have:
spring:
datasource:
platform: postgres
driverClassName: org.postgresql.Driver
url: jdbc:postgresql://localhost:5432/mydb
username: foo
password: bar
Has anyone done this before?
configure the below annotation at your main configuration class
#PropertySource("classpath:datasource.yml")
Related
spring-boot-starter-parent:2.7.5
I have configured the following in my src/main/resources/application.yml file:
spring:
liquibase:
change-log: classpath:/db/changelog/db.changelog-master.xml
---
spring:
config:
activate:
on-profile: prod
datasource:
driver-class-name: org.postgresql.Driver
url: ${DB_URL}
username: ${DB_USERNAME}
password: ${DB_PASSWORD}
Additionally, the following resource exists in the project repository:
src/main/resources/db/changelog/db.changelog-master.xml
I also have the following configured in src/test/resources/application.yml:
spring:
config:
activate:
on-profile: test
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
username: sa
password: sa
liquibase:
change-log: classpath:/db/changelog/db.changelog-master.xml
When I try to build this project the build fails with the following error message:
Liquibase failed to start because no changelog could be found at 'classpath:/db/changelog/db.changelog-master.yaml'
Is there some way to configure spring boot to allow an xml based liquibase changelog file? If so, what am I missing? Or does it only support the yaml format?
Your configurations seems valid, but some configuration still points to classpath:/db/changelog/db.changelog-master.yaml (based by on provided error message)
If there is no some typo, than may be you should look for this property in some other configuration (.properties files, command line arguments and other, see doc for all possible ways)
I have seen below syntax in a few configs but I cannot find reference or documentation for this anywhere.
spring:
datasource:
username: FILE(/etc/usernames....)
How does this work? Why not use file: instead of FILE(. Also, any reference would be great.
I have a new MongoDB, that means there's no user, password and/or an authentication database.
Translating this to a .properties file, this should be:
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
...and for .yaml/.yml:
spring:
data:
mongodb:
host: localhost
port: 27017
Now let's suppose I want to use environment variables instead, if they're set, like this:
spring:
data:
mongodb:
host: ${MONGODB_HOST:localhost}
port: ${MONGODB_PORT:27017}
Everything until now works as expected.
What I want to achieve is the same for the spring.data.mongodb.user, spring.data.mongodb.password and spring.data.mongodb.authentication-database properties. I've tried doing the same technique for these properties, but in case they're not found in the environment, an exception is thrown, like this:
spring:
data:
mongodb:
host: ${MONGODB_HOST:localhost}
port: ${MONGODB_PORT:27017}
username: ${MONGODB_USERNAME}
password: ${MONGODB_PASSWORD}
authentication-database: ${MONGODB_AUTHENTICATION_DATABASE}
I have even tried setting empty/blank valuesm like ${MONGODB_USERNAME:}, ${MONGODB_USERNAME:''} and ${MONGODB_USERNAME:""}.
How do I get to achieve this? Is it even possible?
I've not tried this, but Spring boot should be able to pickup environment variables and use them as properties without having to put them in the yaml file. For example if you name a OS environment variable SPRING_DATA_MONGODB_USERNAME the value should show up in the spring property spring.data.mongodb.username. This would allow you to specify the username property only when you require it.
The other way to do this is to use externalised config. If you are booting your application from a fat jar, an application.yml outside the jar in the same directory can add in your additional properties:
spring:
data:
mongodb:
username: fred
password: password
without affecting the properties specified in the application.yml provided inside the jar.
There's a long ordered list of where spring-boot looks for properties documented here.
I have:
spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=Europe/Madrid
spring.datasource.username=root
spring.datasource.password=secret
spring.jpa.hibernate.ddl-auto=update
and It works fine.
But when I try to do it this way:
spring:
datasource:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?serverTimezone=Europe/Madrid
username: root
password: secret
jpa:
hibernate:
ddl-auto: update
It fails with:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
I assume it is a silly syntax error, but I can't notice where it is.
If you are going to use the YAML syntax, you need to specify it by changing the name of the file from application.properties to application.yml.
YML is very sensitive. Make sure you:
Use spaces instead of tabs
Use the proper extension: application.yml
See this: https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config-yaml
I have multimodule maven project in which I have one common part which I want to share between applications.
In common module I'm defining the configuration for liquibase and db connection. For this I have 2 profiles common-prod and common-dev located in application.yaml.
spring:
profiles: common-prod
# POSTGRE
datasource:
url: ${DATABASE_URL:jdbc:postgresql://localhost:5432/dbname}
driver-class-name: org.postgresql.Driver
username: pguser
password: pguser
jpa:
database-platform: org.hibernate.dialect.PostgreSQLDialect
hibernate:
ddl-auto: none
properties:
hibernate.default_schema: public
#LIQUIBASE
liquibase:
enabled: true
change-log: /db/changelog/db-changelog-master.xml
drop-first: false
---
spring:
profiles: common-dev
# POSTGRE
datasource:
url: ${DATABASE_URL:jdbc:postgresql://localhost:5432/dbname-dev}
username: pguserdev
password: pguserdev
#LIQUIBASE
liquibase:
enabled: true
drop-first: true
now in web application I want to define some additional properties. But when I include them in application.yaml (in web module) I think it overrides the application.yaml from common module.
So I need to
rename the application.yaml from common to something like application-common.yaml
rename the application.yaml in application to something like application-web.yaml
if I rename the common module's yaml file am I able to reference the profiles from within this file? eg something like -Dspring.profiles.active=common-common-prod or I need to separate this profiles to separate yaml files?
If I rename web modules config file to application-web.yaml I must allways include the web profile. I can live with that, but it would be simpler to include the profiles in application.yaml and don't care about command line arguments.