Since Grails 3 we can use a random value for a configuration property. This is because Grails now uses Spring Boot and this adds the RandomValuePropertySource
class to our application. We can use it to produce random string, integer or lang values. In our configuration we only have to use ${random.<type>}
as a value for a configuration property. If the type is int
or long
we get a Integer
or Long
value. We can also define a range of Integer
or Long
values that the generated random value must be part of. The syntax is ${random.int[<start>]}
or ${random.int[<start>,<end>}
. For a Long
value we replace int
with long
. It is also very important when we define an end value that there cannot be any spaces in the definition. Also the end value is exclusive for the range. If the type is something else then int
or long
a random string value is generated. So we could use any value after the dot (.
) in ${random.<type>}
to get a random string value.
In the following example configuration file we use a random value for the configuration properties sample.random.password
, sample.random.longNumber
and sample.random.number
:
Continue reading →
Grails 3 is build on top of Spring Boot and this adds a lot of the Spring Boot features to our Grails application. For example in Spring Boot we can store configuration properties in an external file. A default Grails application already adds application.yml
in the grails-app/conf
directory. If we want to specify different values for a configuration property for each environment (like development, test and production) we can use environment
section in application.yml
. We know this from previous Grails versions with a Groovy configuration file Config.groovy
. But we can also create different configuration files per environment and set the value for the configuration property in each file. We can use the following naming pattern for the file: application-{env}.yml
or application-{env}.properties
. These files need be in:
- a
config
directory in the directory the application is running from
- the root of the directory the application is running from
- in a
/config
package on the classpath
- in the root of the classpath
Continue reading →
Grails 3 introduces GrailsAutoConfiguration
as the base class for a Grails application. We can extend this class, add a main
method and we are ready to go. By default this class is the Application
class in the grails-app/init
directory. We can override several Grails lifecycle methods from the GrailsAutoConfiguration
base class. One of them is doWithSpring
. This method must return a closure that can be used by the Grails BeanBuilder
class to add Spring beans to the application context. The closure has the same syntax as what we already know for the grails-app/conf/spring/resources.groovy
file, we know from previous Grails versions.
We start with a simple class we want to use as a Spring bean:
Continue reading →
In a previous post we learned how to pass Java system properties from the command-line to a Java process defined in a Gradle build file. Because Grails 3 uses Gradle as the build tool we can apply the same mechanism in our Grails application. We need to reconfigure the run
task. This task is of type JavaExec
and we can use the method systemProperties
to assign the system properties we define on the command-line when we invoke the run
task.
We have a simple Grails 3 application with the following controller that tries to access the Java system property sample.message
:
Continue reading →
Since Grails 3 is based on Spring Boot we can re-use many of the Spring Boot features in our Grails application. For example in a Spring Boot application we can use environment variables to give configuration properties a value. We simply need to follow some naming rules: the name of the configuration property must be in uppercase and dots are replaced with underscores. For example a configuration property feature.enabled
is represented by the environment variable FEATURE_ENABLED
.
We create the following controller in a Grails 3 application with a message
property. The value is set with the @Value
annotation of the underlying Spring framework. With this annotation we tell the application to look for an (external) configuration property sample.message
and assign it's value to the message
property. If it cannot be set via a configuration property the default value is "gr8".
Continue reading →
In Grails we can use the list-plugins
command to get a list of all available plugins. The list returns only the plugins that are available for the Grails version we are using. So if we invoke this command in Grails 3 we get a different list than a Grails 2.x version. To get more detailed information, like website, source code URL and dependency definition we use the plugin-info
command.
Let's run the plugin-list
command for Grails 3:
Continue reading →
Grails has a data binding mechanism that will convert request parameters to properties of an object of different types. We can customize the default data binding in different ways. One of them is using the @DataBinding
annotation. We use a closure as argument for the annotation in which we must return the converted value. We get two arguments, the first is the object the data binding is applied to and the second is the source with all original values of type SimpleMapDataBindingSource
. The source could for example be a map like structure or the parameters of a request object.
In the next example code we have a Product
class with a ProductId
class. We write a custom data binding to convert the String
value with the pattern {code}-{identifier}
to a ProductId
object:
Continue reading →
With Grails 3 we also get Spring Boot Actuator. We can use Spring Boot Actuator to add some production-ready features for monitoring and managing our Grails application. One of the features is the addition of some endpoints with information about our application. By default we already have a /health
endpoint when we start a Grails (3+) application. It gives back a JSON response with status UP. Let's expand this endpoint and add a disk space, database and url health check indicator.
We can set the application property endpoints.health.sensitive
to false
(securing these endpoints will be another blog post) and we automatically get a disk space health indicator. The default threshold is set to 10MB, so when the disk space is lower than 10MB the status is set to DOWN. The following snippet shows the change in the grails-app/conf/application.yml
to set the property:
Continue reading →
We can let Grails log some extra information when the application starts. Like the process ID (PID) of the application and on which machine the application starts. And the time needed to start the application. The GrailsApp
class has a property logStartupInfo
which is true
by default. If the property is true than some extra lines are logged at INFO and DEBUG level of the logger of our Application
class.
So in order to see this information we must configure our logging in the logback.groovy
file. Suppose our Application
class is in the package mrhaki.grails.sample.Application
then we add the following line to see the output of the startup logging on the console:
Continue reading →
Since Grails 3 we can borrow a lot of the Spring Boot features in our applications. If we look in our Application.groovy
file that is created when we create a new Grails application we see the class GrailsApp
. This class extends SpringApplication
so we can use all the methods and properties of SpringApplication
in our Grails application. Spring Boot and Grails comes with the class ApplicationPidFileWriter
in the package org.springframework.boot.actuate.system
. This class saves the application PID (Process ID) in a file application.pid
when the application starts.
In the following example Application.groovy
we create an instance of ApplicationPidFileWriter
and register it with the GrailsApp
:
Continue reading →
Grails 3 is based on Spring Boot. This means we can use a lot of the stuff that is available in Spring Boot now in our Grails application. If we look at the logging of a plain Spring Boot application we notice the logging has colors by default if our console supports ANSI. We can also configure our Grails logging so that we get colors.
First we need to change our logging configuration in the file grails-app/conf/logback.groovy
:
Continue reading →
A good thing in Grails is that in Grails artifacts like controllers and services we have a log
property to add log statements in our code. If we want to have the output of these log statements we must use a special naming convention for the log names. Each logger is prefixed with grails.app
followed by the Grails artifact. Valid artifact values are controllers
, services
, domain
, filters
, conf
and taglib
. This is followed by the actual class name. So for example we have a controller SampleController
in the package mrhaki.grails
then the complete logger name is grails.app.controllers.mrhaki.grails.SampleContoller
.
The following sample configuration is for pre-Grails 3:
Continue reading →