Enterprise Java

Spring Properties Cleanup

Configuration files in Spring Boot projects often begin as concise and readable files. However, as your project evolves—adding new modules, supporting multiple environments, or onboarding more developers—these files often spiral into cluttered, hard-to-read blocks of key-value pairs. Duplication, misalignment, inconsistent naming, and excessive whitespace can turn even the smallest configuration change into a frustrating debugging session. Let’s explore how the different approaches works and why it should be part of your regular development and CI/CD process.

1. Introduction to Properties File in Spring

In Spring Boot, configuration is externalized to application.properties or application.yml files. This design supports environment-specific behavior without changing code. Properties control everything from database connections to logging levels, security credentials, and service endpoints.

Example:

server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=secret
spring.application.name=my-app

These files can quickly become long and error-prone. Developers may copy-paste entries from other modules or environments, forget to remove deprecated properties, or use inconsistent naming conventions.

Managing this complexity manually becomes impractical, especially in teams or large codebases. That’s where tooling comes in.

2. Setting Up Spring Properties Cleaner in Your Project

The Spring Properties Cleaner is a Gradle plugin that helps format and organize your .properties files. Once configured, it can run locally during development or as part of CI jobs to maintain clean configuration files across environments.

To begin, add the plugin to your build.gradle:

plugins {
  id "io.github.ozkanpakdil.spring-properties-cleaner" version "1.2.0"
}

Also, configure plugin resolution in settings.gradle:

pluginManagement {
  repositories {
    gradlePluginPortal()
  }
}

Once setup is complete, run the cleaner:

./gradlew cleanProperties

This scans all .properties files and rewrites them by applying formatting, sorting, whitespace trimming, and duplicate resolution.

It’s a lightweight tool that doesn’t require annotations or code changes—perfect for continuous integration environments.

3. Cleaning Up Duplicates and Standardizing Format

One of the biggest issues in configuration files is duplicate property definitions. For example:

spring.datasource.username=root
spring.datasource.username=admin

Spring will use the last value, but it’s unclear to developers whether this is intentional or accidental. Such ambiguity leads to production bugs or unexpected test results.

The cleaner removes earlier definitions, keeping only the final line. This makes property files clearer and less error-prone.

It also normalizes whitespace, such as spacing around equals signs or tabs vs spaces—ensuring consistency across teams and editors.

4. Sorting and Structuring Property Keys

Alphabetical sorting might sound trivial, but it makes a huge difference in large files. It helps you quickly spot missing or misplaced entries and compare related properties without hunting through the entire file.

Consider this unsorted example:

server.port=8080
logging.level.org.springframework=DEBUG
spring.application.name=app
spring.datasource.url=jdbc:mysql://localhost/demo

After sorting:

logging.level.org.springframework=DEBUG
server.port=8080
spring.application.name=app
spring.datasource.url=jdbc:mysql://localhost/demo

This structure is easier to read, especially in diffs or pull requests. If you’re using version control, cleaner diffs reduce noise and make real changes easier to spot.

5. Removing Redundant Prefixes from Property Keys

In Spring projects, many keys follow a hierarchical pattern (e.g., security.oauth.client.id). Grouping these logically improves readability.

Example:

security.oauth.client.id=abc
security.oauth.client.secret=xyz
security.oauth.token.url=https://example.com/token

This isn’t true “nesting” like in YAML, but having related keys inline helps developers mentally organize information. It also simplifies validation scripts or future migrations to application.yml if needed.

6. Moving Shared Properties to a Common File

Spring Boot supports profiles using property file names like application-dev.properties or application-prod.properties. However, many teams accidentally duplicate keys across files that could be centralized.

For example:

// application-dev.properties
feature.toggles.newUI=true
server.port=8081

// application-prod.properties
feature.toggles.newUI=true
server.port=8082

Both environments use the same feature toggle. This should be moved to the base application.properties file:

// application.properties
feature.toggles.newUI=true

// application-dev.properties
server.port=8081

// application-prod.properties
server.port=8082

Centralizing shared configs reduces maintenance overhead and enforces consistency across environments.

7. Removing Excess Blank Lines

Developers often leave blank lines to visually separate sections, but over time these can get out of hand—especially in copy-pasted blocks.

Before cleanup:

server.port=8080


spring.application.name=demo


logging.level.root=INFO

After cleanup:

server.port=8080
spring.application.name=demo
logging.level.root=INFO

This makes the file compact and professional-looking while preserving clarity.

8. Conclusion

Configuration hygiene is often overlooked, but it can greatly impact application stability, onboarding experience, and debugging efficiency. Small errors in .properties files can lead to misconfigurations that are hard to trace.

By automating file cleanup with Spring Properties Cleaner, you offload the repetitive work of maintaining format, reduce human error, and ensure all environments follow the same standards.

Consider adding this tool to your development process or CI pipeline. Run it regularly—especially before releases—to catch issues early and keep your configuration debt in check.

Yatin Batra

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button