Loading External Application Properties in the Gradle bootRun Task

The Spring Boot gradle plugin provides the bootRun task that allows a developer to start the application in a “developer mode” without first building a JAR file and then starting this JAR file. Thus, it’s a quick way to test the latest changes you made to the codebase.

Sadly, most applications cannot be started or would not work correctly without specifying a couple configuration parameters. Spring Boot supports such parameters with it’s application.properties file. The parameters in this file are automatically read when the application is started from a JAR and passed to the application.

The bootRun task also allows to define such properties. The common way of doing this is like this in the build.gradle file:

bootRun {
  jvmArgs =
    [
      "-DmyApp.myParam1=value1",
      "-DmyApp.myParam2=value2"
    ]
}

However, if your are working at the codebase together with other developers, each developer may want to test different use cases and needs different configuration values. She would have to edit the build.gradle each time. And each time she checks in changes to the codebase, she has to check if the build.gradle file should really be checked in. Which is not what we want.

The solution to this problem is a specific properties file for each developer’s local environment that is not checked into the VCS. Let’s call it local.application.properties. In this file, put your applications configuration parameters just as you would in a real application.properties file.

To make the bootRun task load these properties, add the following snippet to your build.gradle:

def Properties localBootRunProperties() {
    Properties p = new Properties();
    p.load(new FileInputStream(
      file(project.projectDir).absolutePath + "/local.application.properties"))
    return p;
}

Then, in your bootRun task, fill the systemProperties attribute as follows:

bootRun {
  doFirst {
    bootRun.systemProperties = localBootRunProperties()
  }
}

The call to localBootRunProperties() is put into the doFirst closure so that it gets executed only when the task itself is executed. Otherwise event all other tasks would fail with a FileNotFoundException if the properties file is not found instead of only the bootRun task.

Further Reading

Tom Hombergs

As a professional software engineer, consultant, architect, general problem solver, I've been practicing the software craft for more fifteen years and I'm still learning something new every day. I love sharing the things I learned, so you (and future me) can get a head start. That's why I founded reflectoring.io.

Recent Posts

Distribute Static Content with Amazon CloudFront

Distribute Static Content with Amazon CloudFront

Amazon CloudFront is a fast content delivery network (CDN) service that securely delivers data, videos, applications, and APIs to customers globally with low latency.

Read more

One-Stop Guide to Mapping with MapStruct

When we define multi-layered architectures, we often tend to represent data differently at each layer. The interactions between each layer become quite tedious and cumbersome.

Read more
Using a Jump host to access an RDS database in a private subnet

Using a Jump host to access an RDS database in a private subnet

Back-end server resources like databases often contain data that is critical for an application to function consistently. So these resources are protected from public access over the internet by placing them in a private subnet.

Read more