Adding logging support to a class in Groovy is easy.
We can choose to add SLF4J, Log4j, Log4j2, Apache Commons or Java Util Logging to our class.
The default implementation of the Abstract Syntax Tree (AST) transformation is to add a log field of the correct type.
As category name the complete class name (including the package) is used.
We can change the name of the field with the value attribute.
To alter the category name we use the attribute category.
In the following example snippet we change the log field name to LOGGER and set a custom category name:
Continue reading →
In a previous post we learned how to use the toListString or toMapString methods.
With these methods we create a String representation of a List or Map object.
With a bit of Groovy code we can take such a String object and turn it into a List or Map again.
In the following code snippet we turn the String value [abc, 123, Groovy rocks!] to a List with three items:
Continue reading →
Groovy adds to Map objects the toMapString method.
With this method we can have a String representation of our Map.
We can specify an argument for the maximum width of the generated String.
Groovy will make sure at least the key/value pairs are added as a pair, before adding three dots (...) if the maximum size is exceeded.
def course = [
name: 'Groovy 101',
teacher: 'mrhaki',
location: 'The Netherlands']
assert course.toMapString(15) == '[name:Groovy 101, ...]'
assert course.toMapString(25) == '[name:Groovy 101, teacher:mrhaki, ...]'
Continue reading →
Groovy has many AST annotations that add code to our class (the Abstract Syntax Tree - AST) before it is compiled.
So the compiled class file contains the code added by the AST annotation.
With the @AutoClone annotation a clone method is added and the class implements the Cloneable interface.
We have different strategies to choose from to support cloning for our class.
The default strategy is to invoke super.clone() in the generated clone method.
The next statements will deep copy the properties (and optional fields) from our class.
If one of the properties cannot be cloned an exception is thrown.
In the following example code snippet we apply the @AutoClone annotation to the classes Course and Teacher:
Continue reading →
In a previous post we learned how to add a banner to a Grails 3.0 application.
We used the Spring Boot support in Grails to show a banner on startup.
The solution we used doesn't work for a Grails 3.1 application.
We need to implement a different solution to show a banner on startup.
First of all we create a new class that implements the org.springframework.boot.Banner interface.
We implement the single method printBanner and logic to display a banner, including colors:
Continue reading →
With Grails 3 we can create a so-called fat jar or war file.
To run our application we only have to use java -jar followed by our archive file name and the application starts.
Another option is to create a fully executable jar or war file, which adds a shell script in front of the jar or war file so we can immediately run the jar or war file.
We don't have to use java -jar anymore to run our Grails application.
The fully executable JAR file can only run on Unix-like systems and it is ready to be used as service using init.d or systemd.
To create a fully executable jar file for our Grails application we must add the following lines to our build.gradle file:
Continue reading →
In a previous post we learned how to add Git commit information to the /info endpoint in our Grails application.
We can add our own custom information to this endpoint by defining application properties that start with info..
Let's add the Grails environment the application runs in to the /info endpoint.
We create the file grails-app/conf/application.groovy.
To get the value we must have a piece of code that is executed so using the application.groovy makes this possible instead of a static configuration file like application.yml:
Continue reading →
We know Grails 3 is based on Spring Boot.
This means we can use Spring Boot features in our Grails application.
For example a default Grails application has a dependency on Spring Boot Actuator, which means we have a /info endpoint when we start the application.
We add the Git commit id and branch to the /info endpoint so we can see which Git commit was used to create the running application.
First we must add the Gradle Git properties plugin to our build.gradle file.
This plugin create a git.properties file that is picked up by Spring Boot Actuator so it can be shown to the user:
Continue reading →
Ratpack has a very useful class: TestHttpClient.
This is a blocking HTTP client that we normally use for testing our Ratpack applications.
For example we use MainClassApplicationUnderTest or GroovyRatpackMainApplicationUnderTest in a test and invoke the getHttpClient method to get an instance of TestHttpClient.
The class has a lot of useful methods to make HTTP requests with a nice DSL.
TestHttpClient is also very useful as a standalone HTTP client in other applications.
Suppose we have a piece of code that needs to access MapQuest Open Platform Web Services to get location details for a given combination of longitude and latitude values.
In the constructor we create an instance of the interface ApplicationUnderTest.
We then can use the getHttpClient method of ApplicationUnderTest to get a TestHttpClient instance:
Continue reading →
When we write code with a lot of method chaining that involves closures and the use the Reformat code feature of IntelliJ IDEA things might get screwed up.
Luckily we can changes some Groovy formatting rules in IntelliJ IDEA to prevent the reformatting.
In the following screenshot we see the original piece of code in the IntelliJ IDEA editor:
Continue reading →
Suppose we have a piece of code that uses an external HTTP service.
If we write a test for this code we can invoke the real HTTP service each time we execute the tests.
But it might be there is a request limit for the service or the service is not always available when we run the test.
With Ratpack it is very, very easy to write a HTTP service that mimics the API of the external HTTP service.
The Ratpack server is started locally in the context of the test and we can write extensive tests for our code that uses the HTTP service.
We achieve this using the Ratpack EmbeddedApp or GroovyEmbeddedApp class.
With very little code we configure a server that can be started and respond to HTTP requests.
In our example project we have a class GeocodeService that uses the external service MapQuest Open Platform Web Services.
We use the HTTP Requests library to make a HTTP request and transform the response to an object:
Continue reading →
When we define our Ratpack application using the Groovy DSL in a file ratpack.groovy, we can split up the definition in multiple files.
With the include method inside the ratpack configuration closure we can use the file name of the file we want to include.
The file that we include also contains a ratpack configuration closure.
We can use the same bindings, handlers and serverConfig sections.
The bindings configuration is appended to the parent configuration.
The handlers and serverConfig configuration is merged with the parent configuration.
In an example project we have the following ratpack.groovy, that includes two extra files: course.groovy and loghandler.groovy:
Continue reading →