When we develop our Ratpack application using Gradle we can use the continuous build feature of Gradle. If we make a change in a source file then our Ratpack application is automatically restarted. It would be nice to combine this with LiveReload using the Gradle LiveReload plugin. Then when we change for example a stylesheet file it is automatically reloaded in the web browser without invoking a refresh action.
In the following build file we add the Gradle LiveReload plugin and configure it to watch for changes in the output directory of the processResources
task. This task is executed when we change a file in the source directory if we use Gradle's continuous build feature.
Continue reading →
A Gradle build script is actually a Groovy script. The Gradle API uses Groovy a lot so we can have a nice DSL to define our builds. But we can also use Java code in a Groovy script. The compiler that compiles the build script understands Java code as well as the Groovy code. Sometimes I hear from people new to Gradle that they have difficulty understanding the DSL. I thought it would be a fun exercise to write a very simple Gradle build script using Java syntax.
Most notable is that we invoke the getProject
method to get a reference to org.grade.api.Project
. In the Gradle DSL we could use the simpler Groovy property reference project
or just leave it out, because all method invocations in the build script are delegated to Project
.
Continue reading →
In a previous post we learned how to use the NamedDomainObjectContainer
class. We could create new objects using a nice DSL in Gradle. But what if we want to use DSL syntax to create objects within objects? We can use the same mechanism to achieve this by nesting NamedDomainObjectContainer
objects.
We want to support the following DSL to create a collection of Server
objects, where each server can have multiple Node
objects:
Continue reading →
Interesting links for week 7 2016:
Continue reading →
Gradle offers the NamedDomainObjectContainer
class to create a collection of objects defined using a clean DSL. The only requirement for the objects we want to create is that they have a constructor that takes a String
argument and a name
property to identify the object. The value for the name
property must be unique within the collection of objects. We create a new instance of a NamedDomainObjectContainer
with the container
method of the Gradle Project
class. We can add the NamedDomainObjectContainer
instance to the extensions
property of our project
, so we can use a DSL to create instances of objects that need to be in the NamedDomainObjectContainer
object in our project.
The following code shows a simple build script in which we want to create a collection of Product
objects. The creation of the NamedDomainObjectContainer
object is done in a plugin so we only have to apply the plugin to use the DSL to create Product
objects:
Continue reading →
When we want to debug our Ratpack application written in Java we can simply use the Debug action on the main application class. When we described the application with the Groovy DSL we must add a Groovy runtime configuration to our project in IntelliJ IDEA to support debugging of the (R|r)atpack.groovy
script file.
First we select Run | Edit configurations.... IntelliJ IDEA opens a new dialog window where we can add or modify run/debug configurations. We select the option New configuration and choose the option Groovy:
Continue reading →
It is actually very easy to run a Ratpack application in the Groovy Console. The Groovy Console is a GUI application that is distributed with Groovy and allows us to write and run Groovy scripts. We start the Groovy Console with the groovyConsole
command: $ groovyConsole
. To run a Ratpack application we only have to add a dependency to Ratpack using the @Grab
annotation. We can write an application with the Groovy DSL and select Script | Run from the menu. If we make a change in the script file we invoke the Run command again. The Ratpack application restarts with our changes. This is very useful for trying out some Ratpack features without much hassle.
The following screenshot shows a simple Ratpack application. At the bottom we see the logging output of the running application:
Continue reading →
I am still settling down from an awesome week in Sweden, where I went for the JFokus conference and subsequent speaker conference. Both were of epic proportions, and I would like to share my experience of giving a presentation there that I would not soon forget. My presentation on Evolutionary Algorithms titled “Making Darwin Proud: Coding Evolutionary Algorithms in Java” was scheduled for Tuesday, making it a very special session. Not only was it the 10th “birthday” of JFokus, it was also the 1st birthday of my son Olivier. It was a tough decision to speak at JFokus instead of being at home to celebrate my sons very first birthday, but as my wife said “he’s not going to remember you weren’t there on the specific day…we’ll celebrate extensively before you leave for Sweden” (I love my wife). However, I still wanted to do something special for my boy, which led me to come up with the ludicrous idea of starting out the presentation by asking the audience (some 450 developers) if they would sing Happy Birthday with me while I recorded it with my phone. They did, and it was awesome! I would like to use this opportunity to thank them from the bottom of my heart, because they made the day, and gave my wife, Olivier and me so much joy by doing this. The tweet that followed is one of the most retweeted and liked I’ve ever had (as well it should be!): https://twitter.com/BWknopper/status/697107401686675457 After our singing intro, I felt a connection with the audience I haven’t felt before (and I gave this talk a couple of times) which in turn led to one of the most fun and relaxed presentations I ever gave. The abstract of the presentation:
Java Developers sometimes face programming challenges, such as creating a school roster or determining a salesperson’s optimal route, that are extremely difficult to crack using conventional approaches. Discover how Evolutionary Algorithms can be applied to solve these complex puzzles. The session starts with a success story from the NASA space archives to explain the concepts. Once the stage is set, it’s puzzle solving time! Learn to code Evolutionary Algorithms using plain Java - although existing Java frameworks such as JGAP are also addressed. The session concludes with a checklist that can be used to determine whether Evolutionary Algorithms are a good fit to the problem. With this checklist, the decision has never been easier!
Continue reading →
Interesting links for week 6 2016:
Continue reading →
To set the base directory for serving static files in a Ratpack application we can use the baseDir
method of the ServerConfigBuilder
class. We must provide a Path
or File
to this method. If we want to serve files from the class path, for example a JAR file or directory, we can use the find
method of the class BaseDir
. The find
method will search the class path for a marker file with the name .ratpack
. If the file is found then the directory or JAR file it is found in is used as the root of the file system. Normally the root of the class path is searched, but we can change the search path with an argument for the find
method.
In the following sample Ratpack application we use the value web-resources/.ratpack.base.dir
for the BaseDir.find
method. So in our class path we must have a web-resources
directory with the file .ratpack.base.dir
that will serve as the root file system for serving static files.
Continue reading →
Since Gradle 2.11 we can specify the test framework to use when we initialise a project with the init
task. There is a new option for this task: --test-framework
. By default JUnit dependencies are added, but if we specify the value spock
the Spock libraries are included in the dependencies section of our build.gradle
file.
Let's run the init
task to create a Java project with Spock as test framework:
Continue reading →
In a previous post we have seen how to execute a Groovy script in our source directories. But what if we want to use the Groovy command line to execute a Groovy script? Suppose we want to evaluate a small Groovy script expressed by a String value, that we normally would invoke like $ groovy -e "println 'Hello Groovy!'"
. Or we want to use the command line option -l
to start Groovy in listening mode with a script to handle requests. We can achieve this by creating a task with type JavaExec
or by using the Gradle javaexec
method. We must set the Java main class to groovy.ui.Main
which is the class that is used for running the Groovy command line.
In the following sample build file we create a new task runGroovyScript
of type JavaExec
. We also create a new dependency configuration groovyScript
so we can use a separate class path for running our Groovy scripts.
Continue reading →