Groovy on Jetty on IntelliJ IDEA
I finally got around giving Groovy a relatively serious spin this week. Yeah, yeah, I know… a tad too late.
However, I did have my reasons for Groovy absence all this time. The first one, which pretty much got invalidated as I will soon explain, was: Since there’s Ruby and Ruby already rocks, why bother with Groovy in the first place?. To be honest, I’m pretty content with Ruby and Rails for personal projects, or projects for which I get to choose the development platform. But that, of course, is not always the case.
Then, there’s that name… It still puts me off. If the name of a language should ever be reason enough to vote against it, then Groovy’s the one. Probably too late to change it now, but seriously…
Despite the name aversion silliness, I am very glad I gave Groovy a chance. So far, the prospect of introducing Ruby, or any other non-Enterprise platform, in any of the Corporate environments I usually get to spend my job hours in has been a case of When hell freezes over. As many of us might figure, such decisions are, in the Enterprise arena, often affected by reasons far heavier than a platform’s potency or relevancy.
Enter Groovy. Groovy’s dirty lil’ secret is exactly the fact that it sits directly on top of Java, an Enterprise Certified platform. Also, because of that, it leverages whatever good things come with Java for free. And let’s be fair, there are a few… Because of this all but insignificant detail, Groovy can easily become part of a Programmer’s arsenal without having to put up a fight about its maturity or fit for purpose, since, after all, it is Java.
Groovy can prove itself invaluable in the quest for relief to the somewhat cumbersome Java web tier, and following is a development environment match made in heaven, compared to that of a pure Java approach.
The ingredients: Groovy, Jetty (or Simple) and IntelliJ IDEA.
Jetty and IDEA are two of my all time favorites and the fact that Groovy embraces their use means a very big deal to me. GroovyJ could be more feature rich, but just having some of the power of IDEA at hand combined with the flexibility of a dynamic language can work wonders. I’m not forgetting that all this can very well be possible with the other Java IDEs, I just happen to prefer IDEA.
The project’s directory tree looks something like this:
someproject:
src:
java:
Localhost.java
test:
LocalhostTest.java
webapp:
SomeGroovlet.groovy
AnotherGroovler.groovy
WEB-INF:
web.xml
lib:
antlr-2.7.5.jar
asm-2.0.jar
groovy-1.0-jsr-03.jar
jasper-compiler-4.1.30.jar
jasper-runtime-4.1.30.jar
org.mortbay.jetty-4.2.24.jar
servlet-api-2.3.jar
The contents of lib need to be in the classpath at runtime. Localhost.java is the Jetty server setup and it looks like this:
import org.mortbay.jetty.Server;
import org.mortbay.http.HttpListener;
import org.mortbay.http.SocketListener;
public class Localhost {
public static void main(String[] args) {
Server server = null;
try {
server = new Server();
server.addListener(listener());
server.addWebApplication("someproject", "src/webapp");
server.start();
} catch (Exception e) {
e.printStackTrace();
if (server != null) {
try {
server.stop();
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
}
private static HttpListener listener() {
HttpListener listener = new SocketListener();
listener.setPort(8080);
return listener;
}
}
And here’s the web.xml for setting up GroovyServlet, aka support for Groovlets:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>someproject</display-name>
<servlet>
<servlet-name>Groovy</servlet-name>
<servlet-class>groovy.servlet.GroovyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Groovy</servlet-name>
<url-pattern>*.groovy</url-pattern>
</servlet-mapping>
</web-app>
Groovlets and Groovy Templates are very powerful, their syntax is sweet and they are recompiled at runtime when changed. This means that I can fire up Localhost once, and keep on developing with instant feedback but without the typical stop server, compile, deploy, start server interruptions that often come with in-container Java development.
I’m pretty sure I haven’t seen half of it yet, but this far Groovy looks like a big, elegant and terribly diplomatic win in all aspects.

November 8th, 2006 at 4:00 pm
Checkout Grails (http://grails.org)
November 8th, 2006 at 4:14 pm
I’ve been sitting and watching Groovy too. To me the idea of implementing (g)rails for Java is just weird, but I’d like to have a legacy-free, curly-brace-free syntax at my JVM’s disposal.