Explain Codes LogoExplain Codes Logo

Setting active profile and config location from command line in Spring Boot

java
command-line-arguments
spring-boot
configuration-files
Anton ShumikhinbyAnton Shumikhin·Sep 20, 2024
TLDR

To kick things into gear, start by initializing the Spring Boot profile and configuration path via:

  • Profiles: --spring.profiles.active=profileName
  • Config Path: --spring.config.location=locationPath

Quick Use:

java -jar app.jar --spring.profiles.active=dev --spring.config.location=file:/config/

This command swiftly activates the dev profile and draws configurations from the specified file path.

While this gets the ball rolling, various other aspects take the Spring Boot command-line experience up a notch. Let's delve deeper.

Understanding property types and mixing profiles

It's pivotal to distinguish between Java System Properties and Spring Properties. These parameters are prefixed with -D and --, respectively. Command line invocations of Spring Boot applications offer powerful runtime customization when employed correctly.

# Who says you can't have the best of both worlds? java -Dspring.profiles.active=dev,qa -jar your-app.jar

This command activates the dev and qa profiles concurrently, allowing specific configurations from multiple profiles to co-exist if designed to complement each other.

Command-line best practices and common pitfalls

Ensuring command-line supremacy

To safeguard the flexibility of command-line overrides, make sure the active profiles are not hardcoded in your application's internal application.yml or application.properties. Instead, maintain these configuration files as neutral as Switzerland to avoid any showdown with the command-line arguments.

Command-line vs configuration files

To ensure command-line arguments aren't dethroned by higher priority configuration files, check the Spring Boot documentation for the order of precedence. This will confirm whether they are being overridden by files like the application's packaged application.properties or not.

File paths like a pro

When providing --spring.config.location, confirm the path's readability and correct format. Remember to prepend file: for file system paths to avoid classpath resource confusion or leading Spring Boot on a wild goose chase.

Take extra care with Gradle's bootRun and Eclipse. For bootRun, you can access command-line arguments by minding System.properties:

# "Sharing is caring" - System.properties probably bootRun { systemProperties = System.properties }

For Eclipse users, ascertain that the Gradle plugin is configured to faithfully deliver your command-line arguments to bootRun.

Maven's way of doing things

For all the Maven mavericks out there, correctly specify profiles during the build using -P option to avoid any surprises:

# Because who doesn't like surprises - said no developer ever! mvn spring-boot:run -Dspring-boot.run.arguments="--spring.profiles.active=prod --spring.config.location=file:/config/"

Tools in your utility belt and troubleshooting

Using the latest armoury

Updating to the latest Spring Boot version ensures you get all the shiny new tools and fortified bug fixes. Alongside, keeping the Spring Boot plugin updated takes care of any anterior compatibility issues.

Leaning on the coding fraternity

When every troubleshooting trick in the book has been exhausted, turn to the sacred and generous community forums for the wisdom of fellow developers who may have already banished the same problems.

The last stand

  • Run a spell check and maintain case sensitivity to avoid parameter typos.
  • Keep a uniform profile specification in both Maven or Gradle configs.
  • Multiple profiles should be separated by commas, and not by spaces.
  • Ensure the set command-line arguments and environment variables are being respected in your deployment environment.