Thursday, June 26, 2008

Rapid Web Application Prototypes with Maven and Groovy

Utilizing both Maven and Groovy you can rapidly prototype web apps, and in this blog I'll walk you through exactly how to do that. First we are going to create a simple Maven web app project using the Maven Web App Archetype. If you don't have Maven installed already, go ahead and install it. Now let's create a directory called sandbox, and cd into that directory. Now let's create our project by issuing the following command:

mvn archetype:generate

When prompted to choose a number enter 18 (The Java web application archetype) and hit enter. Next you will be prompted for the groupId, enter com.sample and hit enter, for the artifactId enter sample and hit enter, for the version enter 1.0 and hit enter, and for the package name enter com.sample and hit enter. Confirm you project by entering "Y" and hitting enter. Once finsihed you will have a maven project ready to go for deployment. You should see a sample diretory that was created in /sandbox with a pom.xml file. The pom.xml file is what we will look at next.

Next we need to add the dependency for Groovy to our pom, as well as the Maven Groovy Plugin. We do this by adding the following inside the <build> element in the parent pom:


<plugins>
<plugin>
<groupId>org.codehaus.mojo.groovy</groupId>
<artifactId>groovy-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.0</version>
</plugin>
</plugins>
Now that we have the Groovy Plugin added, lets add the Groovy dependency to the pom as well by adding it inside the <dependencies> element of the pom:

<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>1.5.6</version>
</dependency>
Ok, let's save all that and make sure we can build everything. From /sandbox/sample issue the following command:

mvn clean install

You should now have a successful build, and you could actually deploy the war that gets created (in /sandbox/sample/target you will have sample.war) to an application server such as JBoss. Ok, lets get onto the cool stuff. Groovy has a concept that lets you write normal Java servlets in Groovy, called Groovlets. Groovlets are really easy to work with and have some nice features like implicit variables (e.g. request and response which are bound to the ServletRequest and ServletResponse). So what we are going to do is configure our webapp so that it can handle Groovlets and then create our first Groovlet. We are first going to edit our web.xml which is located at /sandbox/sample/src/main/webapp/WEB-INF/, go ahead an open that up and add the following between the <web-app> elements:

<servlet>
<servlet-name>GroovyServlet</servlet-name>
<servlet-class>groovy.servlet.GroovyServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>GroovyTemplate</servlet-name>
<servlet-class>groovy.servlet.TemplateServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GroovyServlet</servlet-name>
<url-pattern>*.groovy</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>GroovyTemplate</servlet-name>
<url-pattern>*.gsp</url-pattern>
</servlet-mapping>
This configures your webapp to compile your .groovy files to bytecode and execute your script when called. Next we need to create our Groovlet and then we are done. So, in /sandbox/sample/src/main/webapp create a file called Sample.groovy. We are going to create a simple Groovlet that accepts an HTTP GET request and has a parameter called username. We will process the request and just print out the response. So, go ahead and open up the Sample.groovy file you opened and add the following:

def username = request.getParameter("username")
println "Hello ${username}"
That's it. Rember that the request variable is implicit, meaning it's already bound to the ServletRequest and ready for use. Now we can compile our war, and deploy it to your favorite application server and you are ready to roll. Once deployed navigate to http://localhost:8080/sample/Sample.groovy?username=Chad in your web browser and you should see the print out "Hello Chad".

This is a really nice way to rapidly prototype webapps. I first started looking into this when I needed a servlet that could access the file system and return XML based off my lookup. Obiously using Groovy's nice features that have been added to the JDK for working with Files, and the MarkupBuilder, this task was trivial. Give it a spin and let me know what you think.

5 comments:

jlorenzen said...

Nice article.
I wonder how easy it would be to write simple REST Services with groovlets and how easy parsing uri's and params would be.

Also the maven groovy plugin is old. Its now the gmaven plugin.

Finally, you could easily tie it into the maven jetty plugin or the maven glassfish v3 plugin to easily run your war inline like mvn jetty:run or mvn glassfish:run (I think).

Anonymous said...

I've successfully taken the same approach for prototyping with a handwritten persistence layer. When the application grows enterpricy, I can strongly recommend checking out Grails, which adds an all-inclusive web framework, using the same technology. This video should show you all you need to know in one hour. You can get Maven support through the grails-maven-plugin, though Maven it is not a first class citizen.

Another tidbit is using the GMaven plugin and Groovy console to prototype and run your code in context of your webapp, mvn groovy:console.

Franz See said...

Nice post :)

I created an archetype based on what you said and placed it in http://code.google.com/p/maven-groovy-webapp-archetype/wiki/QuickStart.

I just added the jetty plugin but the rest are practically the same.

Anonymous said...

This was exactly what I needed to do, I had this up and running in 5 minutes. Thanks for the post, Chad.

peder said...

Good article, although there is a kink in the process using maven that doesn't quite seem "rapid".

You say "Now we can compile our war, and deploy it to your favorite application server and you are ready to roll. "

This means that every time we write some new code we have to rebuild target/ from src/ This seems like an extra step that slows things down quite a bit. Since groovy is a scripting language, ideally you'd be be able to just edit your src file, and reload it in the browser without rebuilding the war. I think it's called Hot Deployment, and it's standard practice with PHP etc. Any ideas?