Since the introduction of the Jenkins Declarative Pipeline syntax (as opposed to the Scripted Pipeline syntax) the concept of a shared library has become more important as we are otherwise restricted to the sections of the pipeline model. So we’ll make a basic set-up with Continue reading
To run external Groovy scripts in our Java or Groovy application is easy to do. For example we can use
GroovyShell to evaluate Groovy code in our applications. If our script contains print methods like
println we can redirect the output of these methods. The
Script class, which is a base class to run script code, has an implementation for the
println methods. The implementation of the method is to look for a property
out, either as part of a
Script subclass or in the binding added to a
Script class. If the property
out is available than all calls to
println methods are delegated to the object assigned to the
out property. When we use a
PrintWriter instance we have such an object, but we could also write our own class with an implementation for the
out property the fallback is to print on
In Groovy we can add a method named
call to a class and then invoke the method without using the name
call. We would simply just type the parentheses and optional arguments on an object instance. Groovy calls this the call operator:
(). This can be especially useful in for example a DSL written with Groovy. We can add multiple
call methods to our class each with different arguments. The correct method is invoked at runtime based on the arguments.
To create JSON output with Groovy is easy using JsonBuilder and StreamingJsonBuilder. In the samples mentioned in the links we create a JSON object with a key and values. But what if we want to create JSON with a root JSON array using
StreamingJsonBuilder? It turns out to be very simple by passing a list of values using the constructor or using the implicit method
Since Groovy 2.4.8 we can use the
uncapitalize method on
CharSequence objects. The
capitalize method was already available for a long time, but now we have the opposite as well.
In the following example we see that the
uncapitalize method only replaces the first letter of a
String value to lower case:
assert 'Groovy'.uncapitalize() == 'groovy'
assert 'MrHaki'.uncapitalize() == 'mrHaki'
String message = 'Groovy Rocks!'
assert message.uncapitalize() == 'groovy Rocks!'
Written with Groovy 2.4.8.
In functional programming we have the concept of an identity function. An identity function returns the same result as the input of the function. Groovy has a lot of functional paradigms including a identity function. Of course in Groovy’s case it is an identity closure. It is defined as a constant in the
Closure.IDENTITY. If we use this closure we get the same result as the argument we provide.
Groovy adds a lot of useful methods to the Java JDK classes. One of them is the
sleep method that is added to all objects. With the
sleep method we can add a pause to our code. The
sleep method accepts a sleep time in milli seconds. The implementation of the method will always wait for he given amount of milli seconds even if interrupted. But we can add a closure as extra argument, which is invoked when the
sleep method is interrupted. We should return
true for the closure to really interrupt, otherwise we use
When we use the property syntax of Groovy to get the value for a property, Groovy will actually try to invoke a
get method for that property if it is available. So for example if we have the statement
user.getName() is invoked. If we want to reference a property field directly, so bypassing the
get method, we must place an
@ in front of the property field name. In the previous example we would write
user.@name to get the field value directly. The same rules apply for setting a value for a property with the Groovy syntax. If we write
user.name = 'mrhaki' then actually
user.setName('mrhaki') is invoked. We can use the
@ prefix also to set a value without invoking the
set method for that property. So in our example it would be
user.@name = 'mrhaki' and the
setName method is not used.
We have many ways to provide configuration properties to a Spring (Boot) application. We can add our own custom configuration properties format. For example we can use Groovy’s
ConfigObject object to set configuration properties. We need to read a configuration file using
ConfigSlurper and make it available as a property source for Spring. We need to implement two classes and add configuration file to support a Groovy configuration file in a Spring application.
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