15

I have spring boot server with 2 property files: application-local.properties and application-test.properties

In each file I have configs for dev machine and for test. Start it like this:

-Dspring.profiles.active=local

But in new spring boot project I use .yaml config file. And I do not understand how can I use profiles with .yaml. I tried read documentation but understood nothing. Can you explain what to do, step by step?

I need have two files?

application-local.yaml and application-test.yaml

Or I need write all in one application.yaml file? If in one file how can I separate configs? It is my config:

server:
  path: ***
  port: ***

cxf:
  path: ***

spring.datasource:
  type: com.zaxxer.hikari.HikariDataSource
  driver-class-name: oracle.jdbc.OracleDriver
  url: ***
  username: ***
  password: ***
  hikari:
    minimumIdle: 5
    maximumPoolSize: 20
    idleTimeout: 30000
    poolName: SpringBootJPAHikariCP
    maxLifetime: 2000000
    connectionTimeout: 30000
    connection-test-query: SELECT 1 FROM DUAL

spring.jpa:
  show-sql: false
  database-platform: org.hibernate.dialect.Oracle10gDialect
  properties.hibernate.jdbc.batch_size: 30
  properties.hibernate.cache.use_second_level_cache: false
  hibernate:
    ddl-auto: validate


spring.cache:
  ehcache:
    config: classpath:ehcache.xml

#app configs
my:
  messages-max-count: 5
  messages-delay: 100
  schedulers-charge-delay: 100
  client:
    first-server-address: ***
    second-server-address: ***
    last-server-address: ***
  enabled-client: FirstClient

I want create test profile and change database url (or change to postgreSql), change maximumPoolSize property

Michael
  • 41,989
  • 11
  • 82
  • 128
ip696
  • 6,574
  • 12
  • 65
  • 128

2 Answers2

21
  1. Create application.yaml and define all default properties there.
  2. Create application-local.yaml and override properties needed for the local profile.
  3. Create application-test.yaml and override properties needed for the test profile.
  4. Set spring.profiles.active by either passing it as a system property (-D for java) or defining it within application.yaml.

When you are running an app with a {PROFILE}, Spring will parse application-{PROFILE}.yaml after application.yaml.

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
  • 1
    if I override property in `application-{PROFILE}.yaml` Do I need remove this property from base `application.yaml`? – ip696 Oct 03 '18 at 12:56
  • 1
    @ip696 you can, but you don't have to. – Andrew Tobilko Oct 03 '18 at 12:59
  • 1
    @ip696 Imagine you decided to run the app without profiles, the startup could fail due to missing properties. – Andrew Tobilko Oct 03 '18 at 13:00
  • I try run app with `-Dspring.profiles.active=test` `(16:01:49: Executing task 'bootRun -Dspring.profiles.active=test'...)` but spring say: `Application: [] No active profile set, falling back to default profiles: default` I have 3 files: application.yaml, application-dev.yaml, application-test.yaml – ip696 Oct 03 '18 at 13:03
  • @ip696 I am not sure `bootRun` supports the `D` option, you need `java -jar -Dspring.profiles.active=test app.jar` – Andrew Tobilko Oct 03 '18 at 13:11
  • 1
    @ip696 add `spring.profiles.active: test` (with the proper formatting) to `application.yaml` and don't pass anything while running – Andrew Tobilko Oct 03 '18 at 13:14
  • @ip696 otherwise, take a look at https://stackoverflow.com/questions/25079244/how-to-pass-jvm-options-from-bootrun – Andrew Tobilko Oct 03 '18 at 13:15
5

Yes, you can create multiple profiles even with single file Profile are separated with 3 DASH (---)

logging:
  level:
    .: error
    org.springframework: ERROR

spring:
  profiles:
    active: "dev"
  main:
    banner-mode: "off"

server:
  port: 8085

---

spring:
  profiles: dev

---

spring:
  profiles: prod
Ramesh
  • 89
  • 1
  • 1