The Groovy spread operator * (asterisk)

Whenever it comes to operating on collections, you know why you’ll love Groovy.
In this example we’ll get to know the asterisk or spread operator, a really helpful colleague – and believe me, it’s got nothing to do with pointers from C ;-) !

Actually, the operator has two functions:

  • Firstly, it’s used to spread or “explode” lists. So it’s actually the counterpart of a join operation
  • Secondly it provides an even shorter way of traversing and operating collections.

Let’s have a look on the first use case and assume we’ve two lists that we’d like to merge. Using the spread operator, it’s a one-liner:

def list0ne = ['a','b']
def listTwo = ['c','d']
def listAll = [*list0ne, *listTwo]

assert listAll.equals(['a','b','c','d'])

def otherList = []
otherList.addAll(listOne)
otherList.addAll(listTwo)
assert listAll.equals(otherList)

So listAll is created by “exploding” the lists first and packing their elements into the new list. A more Java-like way to achieve the same is shown using the addAll method.

The second use case of the spread operator refers to calling an object’s methods on each item of a collection. Note that the method called cannot be an arbitrary method but must be one known to the object.
For example to get the hashCodes of strings, one could use this statement:

def strings = ["1","2","3"]
def hashCodes = strings*.hashCode()
// Result: [49, 50, 51]

More examples can be found on the Groovy documentation.


Shifting Weceem CMS to Grails 2.0

Weceem is an OpenSource CMS built with “rock solid” Grails. Weceem can be used as standalone web application, but also as plugin for existing Grails applications using Weceem as content editor and/or repository.

Like  a christmas gift, Grails 2.0 has been released in Q4 2011, bestowing us with new fancy features. Although the current release 1.1.2 of Weceem is built on Grails 1.3, Marc Palmer (the head behind Weceem) doesn’t make a secret that it’s actually ready to run with Grails 2.0.

As there is no .war package available configured in order to run with Grails 2.0, I tried to package it on my own – and it wasn’t that difficult :-) . So here we go:

  1. using Grails 2.0, create a new, blank Grails application (call it weceem) using the “create-app” command
  2. Download the 1.1.2 sources of Weceem from github
  3. copy the sources into the newly created project so that the application’s structure is preserved
  4. slightly adapt the project structure and build the grails project as war (using grails war). What has to be changed in detail?
Well, the actual part is the modification of application.properties. We’ll update the plugin’s versions so that they match the conventions of Grails 2. It should therefore look like that:

#Grails Metadata file
app.grails.version=2.0.0
app.name=weceem
app.servlet.version=2.5
app.version=2.0
plugins.bean-fields=1.0.RC5
plugins.blueprint=1.0.2
plugins.cache-headers=1.1.5
plugins.ckeditor=3.6.0.0
plugins.feeds=1.5
plugins.hibernate=2.0.0
plugins.jquery=1.7.1
plugins.jquery-ui=1.8.15
plugins.mail=0.9
plugins.navigation=1.3.2
plugins.quartz=0.4.2
plugins.searchable=0.6.3
plugins.spring-security-core=1.2.6
plugins.svn=1.0.0.M1
plugins.taggable=1.0
plugins.tomcat=2.0.0
plugins.weceem-spring-security=1.1

Now, go to grails-app/views and make sure that there is no index.gsp – you can safely remove it as it will be replaced by the start page of your website.

If your using IntelliJ, it should automatically download the newest plugins. Your project structure should look like that:

IntelliJ project configuration for Weceem

IntelliJ project configuration for Weceem

Having that done, try out “Grails war” to build the web application archive.  When being asked if a plugin should be overwritten by a newer version, say “n” to preserve the existing version. I’m not sure if a newer one would do, but that way we’re on the safe side.

Did it work? Then enjoy Weceem with Grails 2!


Follow

Get every new post delivered to your Inbox.