Image by Karsten Paulick from Pixabay

I had a complicated case for Spring Boot properties configuration today that I’d like to share.

My project uses Atomikos for XA transaction management. Atomikos needs to store its transaction logs somewhere, and there’s a special Spring Boot property spring.jta.log-dir that configures the directory to use for these logs. The property is configured in the application.yml file.

The transaction logs that are generated during tests may be safely deleted after the tests have passed, so it makes sense to dump these logs under the target directory of the Maven module. Then they will not pollute the source code and will be naturally disposed of during Maven’s clean .

However, there are two different cases for running those tests —from IDEA (manually) and from Maven (as part of the automated Jenkins build). I needed to make sure that in both cases the logs end up under the target directory.

Whenever I run my application using a Spring Boot run configuration in IntelliJ IDEA, I want those logs to end up in a special pre-configured directory under my user’s home folder. When I run the application as part of the Docker container (production environment), I want them to end up in a folder provided in a specific environment variable (it should point to an external Docker-mounted volume).

So I have multiple values for the same setting, depending on the test, development or production mode, some of which should be provided externally. But I don’t want to specify the current mode explicitly anywhere, as it may be easily deduced from the context. E.g., if I run a test, it’s already located under the src/test/java directory, so the runtime should be aware that this is a test environment, and the test settings should be used to run it.

So here’s the solution for the test environment. I added a single application-default.yml file under the src/test/resources directory.

# properties in this file override the ones from application.yml 
# for all integration tests

spring:
  jta:
    # this puts the Atomikos transaction logs under the target
    # directory for tests run from IDE and under the target/target
    # directory for tests run via Maven, which is also fine
    log-dir: target/transaction-logs

The name of the file matters: the default suffix means this file is applied to the default profile, which is something known to Spring Boot, meaning it will be applied for all tests. Since it is located under the src/test/resources directory, it will be active in tests only. And since this file has different name than the src/main/resources/application.yml file, it won’t override the whole file, but instead add or override specific properties.

Now for the main application configuration. Spring Boot allows to not only use placeholders in the application.ymland populate them from the environment variables, but also use “placeholders inside placeholders” and set default values if the placeholders are not present. Thanks to this killer-feature, here’s how I solved the problem:

spring:
  jta:
    log-dir: ${MY_ENV_VAR:${user.home}/.myapp}/transaction-logs

So what happens here? The production folder name is being passed in the MY_ENV_VAR environment variable, which is set on production only. If this variable is present, then the logs will be stored under the folder it refers to.

But there’s no such variable defined on my machine. So the placeholder resolver relies on the value after the colon as the default value. Note that this default value also contains a placeholder — the user.home system property set by the JVM automatically to refer to the user’s home folder. It should always be present, so the logs will be stored under ~/.myapp/transaction-logs in my home folder whenever I run the application manually from IDEA.

I like this solution because it’s very simple and non-intrusive, I don’t have to mess around with different profiles, and I still leave enough extensibility to override the configuration externally.