Dynamic Spring Boot Arguments With Gradle BootRun

If you're passing arguments to your Spring Boot app to perform certain functions only occasionally and use Gradle, here's how to become Master of your Argument Domain.

The Setup

Here's our situation. We're doing a good bit of development work in Solr lately and want to rebuild the Solr Index on occasion. Otherwise we want to run our Solr Spring Boot app normally to demonstrate various queries.

A GUI Configuration Equivalent

Here is the GUI equivalent of what we want to do from time-to-time, that is, pass –reindex=true as a Command Line Argument when we want to rebuild our Solr Index. We could create two of these in IntelliJ, one with the arguments and one without, but we're not going to do that.

Java Main()

Here's what the main() method of our SolrLauncher class looks like. And yes, there is the Spring Boot CommandLineRunner to use, but in this particular Module we're using it elsewhere. So in review, if the –reindex argument is present and is TRUE, then the SolrUI populate() method is called.

The Gradle

Here's the cool part, adding Spring Boot arguments dynamically in Gradle. You see we have a reindex task with the two << arrows. Technically speaking, that is the Gradle left-shift operator, where BootRun is passed the reindex task as a parameter.

The Results

Before we see how we're going to add the –reindex argument dynamically, let's see what happens without it. We run our selected SolrUI demo function.

Here's how we add the argument dynamically, with our desired result of rebuilding the Solr Index.

Source Code Notes for this Post

Source code discussed in this post can be found in my NixMash Blog project located on GitHub.

Posted February 27, 2017 03:22 PM EST

More Like This Post