If we run our Grails 3 application in development mode our classes and GSP's are automatically recompiled if we change the source file. We change our source code, refresh the web browser and see the results of our new code. If we run our application with another environment, like production or a custom environment, then the reloading of classes is disabled. But sometimes we have a different environment, but still want to have hot reloading of our classes and GSP's. To enable this we must use the Java system property grails.reload.enabled and reconfigure the Gradle bootRun task to pass this system property.

Let's change our Gradle build file and pass the Java system property grails.reload.enabled to the bootRun task if it is set. We use the constant Environment.RELOAD_ENABLED to reference the Java system property.

// File: build.gradle
import grails.util.Environment
...
bootRun {

    final Boolean reloadEnabled =
            Boolean.valueOf(
                    System.properties\[Environment.RELOAD\_ENABLED\])

    if (reloadEnabled) {
        systemProperty Environment.RELOAD\_ENABLED, reloadEnabled
    }

}
...

Suppose we have extra Grails environment with the name custom. We can still have hot reloading if we use the following command:

$ grails -Dgrails.env=custom -Dgrails.reload.enabled=true run-app
...

Or we use the Gradle bootRun task:

$ gradle -Dgrails.env=custom -Dgrails.reload.enabled=true bootRun
...

Written with Grials 3.0.9.

Original article

shadow-left