'convert application.yml file to application.properties

I have my application.yml file as below. How to convert it to application.properties I am trying it but how can i write multiple properties in same file. Its giving me duplicate kery error.

 ---
  spring:
    profiles: peer1
  eureka:
     instance:
        hostname: peer1
     client:
        serviceUrl:
           defaultZone: http://peer2/eureka/

 ---
 spring:
    profiles: peer2
 eureka:
    instance:
      hostname: peer2
    client:
      serviceUrl:
         defaultZone: http://peer1/eureka/


Solution 1:[1]

IntelliJ and other IDEs provide plugins for the same.

For eg- https://plugins.jetbrains.com/plugin/13804-convert-yaml-and-properties-file

Install the plugin, right click on your yaml or properties file and choose - "Convert yaml and properties file".

Solution 2:[2]

You'll need to create differents files, example:

  • application-dev.properties
  • application-prod.properties
  • application-test.properties

And then you deffine your active profile in the application.properties with:

 spring.profiles.active=dev

Solution 3:[3]

When using properties file, you cannot have multiple "sections" per profile in the same file, this is a feature available only with Yaml. You will have to create several properties file, one per profile, as described here : https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html#howto-change-configuration-depending-on-the-environment

To do the same thing with properties files, you can use application-${profile}.properties to specify profile-specific values

You will have one main application.properties file containing common values, and then one application-${profile}.properties file per profile containing values that are environment/profile dependent.

Finally, you will have to set the active profile either as a System property when running the application, or directly in your main application.properties file, as described here: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html#howto-set-active-spring-profiles

Solution 4:[4]

With Spring Boot 2.4, there's the possibility to use the switch spring.config.activate.on-profile for this purpose, everything defined after spring.config.activate.on-profile=myprofile will only be set when the active profile is set to myprofile. In the given example, you would do as follows:

#-- Peer1 Config
spring.config.activate.on-profile=peer1
eureka.instance.hostname=peer1
eureka.client.serviceUrl.defaultZone=http://peer2/eureka/
#-- Peer2 Config
spring.config.activate.on-profile=peer2
eureka.instance.hostname=peer2
eureka.client.serviceUrl.defaultZone=http://peer1/eureka/

See https://spring.io/blog/2020/08/14/config-file-processing-in-spring-boot-2-4 for more information.

Solution 5:[5]

to do it manually:

spring:
    profiles: peer1
  eureka:
     instance:
        hostname: peer1
     client:
        serviceUrl:
           defaultZone: http://peer2/eureka/

will be like this:

spring.profiles=peer1
spring.eureka.instance.hostname=peer1
spring.eureka.client.serviceUrl.defaultZone=http://peer2/eureka/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Satakshi Pandey
Solution 2 Vitor Gabriel Carrilho
Solution 3
Solution 4 A. Markóczy
Solution 5 HibaHasan