1. Overview
In this tutorial, we’ll learn how to convert the default application.properties file we receive when we download a new Spring Boot project from Spring Initializer to the more readable application.yml file.
2. Difference Between Properties and YML File
Before diving directly into the topic, let’s see the difference between these two file formats in the form of code.
In the application.properties file, properties are stored as a single-line configuration. Spring Boot generates the properties file as the default one:
spring.datasource.url=jdbc:h2:mem:testDB
spring.datasource.username=user
spring.datasource.password=testpwd
On the other hand, we can create an application.yml. This is a YML-based file that is easy to read when having hierarchical data compared to the properties file:
spring:
datasource:
url: 'jdbc:h2:mem:testDB'
username: user
password: testpwd
As we can see, with the help of YML-based configuration, we have removed the need to add repeated prefixes (spring.datasource).
3. Convert Properties to YML
3.1. IntelliJ Plugin
If we’re using IntelliJ as our IDE to run the Spring Boot application, we can do the conversion by installing the following plugin:
We need to go to File > Settings > Plugins > Install “Convert YAML and Properties file“.
Once we’ve installed the plugin, we:
- Right-click on the application.properties file
- Select the option “Convert YAML and Properties file” to convert the file to application.yml automatically
We’ll be able to convert it back as well.
Instead of using Intellij and installing plugins, we can also directly copy-paste the properties file’s content from our codebase to the simpleStep converter website.
For security purposes, we must ensure we don’t enter passwords for conversion on third-party websites:
As we can see in the screenshot, we first select the input and output content types using the two dropdown boxes. When we paste the properties file content in the “Input” section, the converted YML format appears instantly in the “Output” section.
4. Conclusion
In this article, we’ve seen the difference between .properties and .yml files and learned how to convert the application.properties file to application.yml using various tools and plugins.