Ratpack applications can be written in Java and Groovy. The Java API is already very clean and on top is a Groovy DSL to work with Ratpack. When we use Groovy we can use the DSL, which allows for more clean code. The Ratpack developers have used the @DelegateTo
annotation in the source code for the DSL definition. The annotation can be used to indicate which class or interface is used as delegate to execute the closure that is passed to the method. And this helps us a lot in the code editor of IntelliJ IDEA, because IDEA uses this information to give us code completion when we use the Groovy DSL in Ratpack. And that makes using the DSL very easy, because we rely on the IDE to give us the supported properties and methods and we make less mistakes.
Let's see this with an example in code. First we create a new Ratpack.groovy
file:
Continue reading →
Gradle has an idea
and eclipse
plugin that we can use to configure IntelliJ IDEA and Eclipse project files. When we apply these plugins to our project we get extra tasks to generate and change project files. Inside our Gradle build file we get new configuration blocks to specify properties or invoke methods that will change the configuration files. One of the nice things to add is to let the IDE download Javadoc files for dependencies in our Java/Groovy projects. By default the sources for a dependency are already downloaded and added to the project, but Javadoc files are not downloaded.
In the example build file we use the idea
and eclipse
plugins. We also add an idea
and eclipse
configuration block. The place where we need to set the property downloadJavadoc
is a bit different, but the end result will be the same.
Continue reading →
Ratpack is from the ground up build to be performant and asynchronous. Let's add a logging implementation that matches the asynchronous nature of Ratpack. Ratpack uses the SLF4J API for logging and if we write logging statement in our own code we should use the same API. For Groovy developers it is nothing more than adding the @Slf4j
AST annotation to our classes. The Logback library has an asynchronous appender which has a queue to store incoming logging events. Then a worker on a different thread will invoke a classic blocking appender, like a file or console appender, to actually log the messages. But in our example we don't use the standard async appender from Logback, but use a asynchronous logbook appender from the Reactor project. Now our queue is backed by a very performant reactor ring buffer implementation.
The following Logback configuration file shows how we can configure the reactor.logback.AsyncAppender
:
Continue reading →
To start our Ratpack application with a random port number we must use the value 0
as port number. The value 0
tells Ratpack to use a random port number (in a safe port range).
In the following example Ratpack application we configure the server port to be random:
Continue reading →
Update: Since Ratpack 1.1.0 the port number is always shown on the console, even if we don't add a SLF4J API implementation.
When we use all the defaults for a Ratpack application the default port that is used for listening to incoming requests is 5050. This is something to remember, because we don't see it when we start the application. If we want to show it, for example in the console, we must add a SLF4J Logging API implementation. Ratpack uses SLF4J API for logging and the port number and address that is used for listening to incoming requests are logged at INFO level. We must add a runtime dependency to our project with a SLF4J API implementation. We provide the necessary logging configuration if needed and then when we start our Ratpack application we can see the port number that is used.
Continue reading →
Grails 3 introduced the concept of application profiles to Grails. A profile contains the application structure, dependencies, commands and more to configure Grails for a specific type of application. The profiles are stored on the Grails Profile repository on Github. We can go there and see which profiles are available, but it is much easier to use the list-profiles
command. With this command we get an overview of all the available profiles we can use to create a new application or plugin.
The list-profiles
task is only available when we are outside a Grails application directory. So just like the create-app
and create-plugin
we can run list-profiles
from a non-Grails project directory.
Continue reading →
To start Grails in interactive mode we simply type grails
on the command line. This starts Grails and we get an environment to run Grails commands like compile
and run-app
. Since Grails 3 the underlying build system has changed from Gant to Gradle. We can invoke Gradle tasks in interactive mode with the gradle
command. Just like we would use Gradle from the command line we can run the same tasks, but this time when Grails is in interactive mode. Grails will use the Gradle version that belongs to the current Grails version. We even get TAB completion for Gradle tasks.
In the next example we start Grails in interactive mode and run the Gradle task components
:
Continue reading →
In this blog post we see how to update the Grails version of our application for a Grails 3 application. In previous Grails versions there was a special command to upgrade, but with Grails 3 it is much simpler. To update an application to a newer version in the Grails 3.0.x range we only have to change the value of the property grailsVersion
in the file gradle.properties
.
# gradle.properties
grailsVersion=3.0.8
gradleWrapperVersion=2.3
Continue reading →
Since Grails 3 the logging configuration is in a separate file. Before Grails 3 we could specify the logging configuration in grails-app/conf/Config.groovy
, since Grails 3 it is in the file grails-app/conf/logback.groovy
. We also notice that since Grails 3 the default logging framework implementation is Logback. We can define a different Logback configuration file with the environment configuration property logging.config
. We can set this property in grails-app/conf/application.yml
, as Java system property (-Dlogging.config=<location>
) or environment variable (LOGGING_CONFIG
). Actually all rules for external configuration of Spring Boot apply for the configuration property logging.config
.
In the following example configuration file we have a different way of logging in our Grails application. We save it as grails-app/conf/logback-grails.groovy
:
Continue reading →
With Grails 3 we get the Spring Boot mechanism for loading external configuration files. The default base name for configuration files is application
. Grails creates for example the file grails-app/conf/application.yml
to store configuration values if we use the create-app
command. To change the base name from application
to something else we must specify a value for the Java system property spring.config.name
.
In the following example we start Grails with the value config
for the Java system property spring.config.name
. So now Grails looks for file names like config.yml
, config.properties
, config-{env}.properties
and config-{env}.yml
in the default locations config
directory, root directory on the filesystem and in the class path.
Continue reading →
A Grails 3 application uses the same mechanism to load external configuration files as a Spring Boot application. This means the default locations are the root directory or config/
directory in the class path and on the file system. If we want to specify a new directory to search for configuration files or specify the configuration files explicitly we can use the Java system property spring.config.location
.
In the following example we have a configuration application.yml
in the settings
directory. The default base name for a configuration file is application
, so we use that base name in our example. In this sample we use a YAML configuration file where we override the property sample.config
for the Grails production environment.
Continue reading →
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 →