Coding

Gradle Goodness: Init Script for Adding Extra Plugins to Existing Projects

Posted on by  
Hubert Klein Ikkink

Gradle is very flexible. One of the ways to alter the build configuration is with initialization or init scripts. These are like other Gradle scripts but are executed before the build. We can use different ways to add the init script to a build. For example we can use the command-line option -I or --init-script, place the script in the init.d directory of our GRADLE_HOME directory or USER_HOME/.gradle directory or place a file init.gradle in our USER_HOME/.gradle directory.

We can also use the apply(from:) method to include such a script in our build file. We can reference a file location, but also a URL. Imagine we place an init script on our company intranet to be shared by all developers, then we can include the script with the apply(from:) method. In the following build file we use this syntax to include the script:

Continue reading →

Gradle Goodness: Exclude Transitive Dependency from All Configurations

Posted on by  
Hubert Klein Ikkink

We can exclude transitive dependencies easily from specific configurations. To exclude them from all configurations we can use Groovy's spread-dot operator and invoke the exclude() method on each configuration. We can only define the group, module or both as arguments for the exclude() method.

The following part of a build file shows how we can exclude a dependency from all configurations:

Continue reading →

Gradle Goodness: Running Java Applications from External Dependency

Posted on by  
Hubert Klein Ikkink

With Gradle we can execute Java applications using the JavaExec task or the javaexec() method. If we want to run Java code from an external dependency we must first pull in the dependency with the Java application code. The best way to do this is to create a new dependency configuration. When we configure a task with type JavaExec we can set the classpath to the external dependency. Notice we cannot use the buildscript{} script block to set the classpath. A JavaExec task will fork a new Java process so any classpath settings via buildscript{} are ignored.

In the following example build script we want to execute the Java class org.apache.cxf.tools.wsdlto.WSDLToJava from Apache CXF to generate Java classes from a given WSDL. We define a new dependency configuration with the name cxf and use it to assign the CXF dependencies to it. We use the classpath property of the JavaExec task to assign the configuration dependency.

Continue reading →

A introduction to websockets.

Posted on by  
Richard Rijnberk

To make a long story short websockets allow us to create a pipeline between a web client and a server. Which is great because we can push and pull all kind of data through it. This in turn allows us to create highly responsive web applications that may update information over multiple sessions. Sound awesome doesn’t it. There’s just one catch, and you can probably guess what it is. That’s right, it’s not universally accepted by all browser or containers. Worse yet, most browsers that are currently in use have very poor to no support for it yet. And the same can be said for quite the percentage of web containers out there. Luckily we can use SocksJS. This library actually checks if it is possible to set up a websocket. And, when this is not the case, sets up a default fallback. This means that if a websocket connection cannot be created the SocksJS implementation will fall back to long pulling. Giving us the same behavior as before only at a higher performance cost. The SocksJS api is the same as the one for websockets. This allows us to change from a websockets implementation to a SocksJS implementation with minimal changes. So if you want to create a highly responsive webapp which updates as soon as new information arrives SocksJS is the way to go.

Continue reading →

Day two of the SpringOne 2GX in Washington.

Posted on by  
Richard Rijnberk

It was a full day starting with breakfast and 5 one and a half hour long presentations followed by dinner and a keynote delivered by Adrian Colyer, CTO of SpringSource. The keynote was on how application architecture has evolved over the last 10 years towards what it has become today. We started out with a static frontend client that communicated with a service layer, which in turn persisted to a database. Nowadays we have rich clients, multiple services in our service tier that in turn connect to services, websockets and/or other data sources. He proceeded to show his application, slowly scaling it up from one isolated environment into a cloud solution and from there into a multi cloud solution using brokers for each cloud region. From this he explained the use of auto scaling workers in a message queue and the advantages this mechanism has in relation to bandwith, throughput and latency. During his presentation he used scriptIt, which is a new browser based javascript editor that was released recently. He was enthusiastic about it due to its ability to follow declarations and support for auto completion. Another tool he was adamant about was a REST service console that allows reading and the exercise of calls that alter the state of the service. It is a great tool for seeing what goes on under the hood. For the code featured in this keynote see: github

Continue reading →

Keynote SpringOne 2GX

Posted on by  
Richard Rijnberk

Juergen Hoeller Hoeller begint zijn presentatie met de originele driehoek van spring. Zijn verhaal beschrijft vervolgens de transitie die het framework heeft gemaakt sinds dat model naar een annotation framework. Hij legt de nadruk vooral ook bij de beschrijvende namen van de annotaties, en hoe ze zorgvuldig zijn gekozen om de code leesbaarder te maken. Hierbij gaat hij dieper in op een aantalannotaties zoals stereotypes, injections en ook parameter annotaties. Hierbij geeft hij aan het gevoel te hebben dat de kracht van deze annotaties nog steeds wel onderschat wordt. Hoeller verteld hierna hoe er in de laatste tijd is gewerkt aan de WebApplicationInitialiser om xml configuraties zoals de web.xml iets van het verleden te maken. Hij gaat vervolgens in op de @configuration annotation en de manier waarop in deze configuratie classes gescanned kan worden op het classpath om filters en beans te injecten in de app. Dit soort Java based configuratie zorgt er voor dat xml configuraties zoals de persistance XML niet meer nodig zijn.

Continue reading →

Grassroots Groovy: Parse XML with XmlSlurper from Java

Posted on by  
Hubert Klein Ikkink

We can introduce Groovy into our Java projects at grassroots level. Even if we aren't allowed to run the Groovy compiler we can use other ways to run Groovy code. As long as we can include the Groovy libraries as a compile dependency than we can already use Groovy from Java. In this post we see how we can use the power of XmlSlurper to parse XML from our Java code.

To execute a Groovy script from we can use a GroovyShell object and invoke the evaluate() method. The evaluate() method can parse a Groovy script as File or Reader object. We can also use a String value to be evaluated. The last statement of the script that is evaluated can be assigned to a Java variable. To pass variables to the script we use the Binding object. This is a map of variables and their values. We assign values to the variables in the Java code and in the Groovy script we can use the variable values.

Continue reading →

JAXB class generation using Maven

Posted on by  
Albert van Veen

There is a useful maven plugin to generate all our JAXB classes for us. This plugin can be executed during the generate-sources phase of our maven build. It can be very useful, especially in the first stages of our project when the design of our XSD may change frequently. We can just add the plugin configuration to the pom.xml of our project.

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>

    <configuration>
        <extension>true</extension>
        <args>
            <arg>-Xfluent-api</arg>
        </args>
        <plugins>
            <plugin>
                <groupId>net.java.dev.jaxb2-commons</groupId>
                <artifactId>jaxb-fluent-api</artifactId>
                <version>2.1.8</version>
            </plugin>
        </plugins>
        <bindingDirectory>src/main/resources/</bindingDirectory>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Continue reading →

Groovy Goodness: Create a List with Default Values

Posted on by  
Hubert Klein Ikkink

Since Groovy 1.8.7 we can create a list and use the withDefault() method to define a default value for elements that are not yet in the list. We use a closure as argument of the method, which returns the default value. We can even access the index of the element in the closure as an argument.

Besides the withDefault() method we can use the withLazyDefault() which is just another name for the same functionality. If we request a value for an index that is greater or equal to the size of the list, the list will automatically grow up to the specified index. Any gaps are filled with the value null.

Continue reading →

Groovy Goodness: Getting the First and Last Element of an Iterable

Posted on by  
Hubert Klein Ikkink

Since Groovy 1.8.7 we can use the first() and last() methods on Iterable objects. With the first() method we get the first element and with the last() method we get the last element:

def list = 0..100

assert list.first() == 0
assert list.last() == 100

def abc = 'abc' as Character[]

assert abc.first() == 'a'
assert abc.last() == 'c'

def s = ['Groovy', 'Gradle', 'Grails', 'Rocks'] as Set

assert s.first() == 'Groovy'
assert s.last() == 'Rocks'

Continue reading →

Groovy Goodness: Using Groovy for Git Hooks

Posted on by  
Hubert Klein Ikkink

Git supports hooks, which are scripts that are fired when certain events happens. The scripts are simply shell scripts and we can use Groovy to run those scripts. We must make Groovy the script language with the hash-bang (#!) header in the Git hook script file. And then we are ready to go and use Groovy as the script language for the Git hooks.

Git hooks are placed in the .git/hooks directory of our project. We create an example script that will use growlnotify to create a notification message with information from the Git commit action. growlnotify is a command-line tool for Mac OSX to send out messages to Growl. Other operating systems also have tools to create notification message from the command-line.

Continue reading →

Gradle Goodness: Getting Announcements from Gradle Build

Posted on by  
Hubert Klein Ikkink

We can use the Gradle announce plugin to send announcements from the build process. We can send data to Twitter (I don't know if our followers are waiting for this, but if you want to you can), but also to notification applications on our local computers. For Mac OSX Growl is supported, for Linux notify-send and for Windows Snarl.

The plugin adds an announce object with the announce() method. The method accepts two arguments. The first argument is the message and the second argument is either twitter or local to indicate where to send the announcement.

Continue reading →

shadow-left