LWN.net Logo

Groovy, a Java-like Scripting Language

Groovy is a relatively new scripting language that resembles Java, it is built on top of the Java Virtual Machine (JVM). The main developers are James Strachan and Bob McWhirter.

Groovy is a new agile dynamic language for the JVM combining lots of great features from languages like Python, Ruby and Smalltalk and making them available to the Java developers using a Java-like syntax. Groovy is designed to help you get things done on the Java platform in a quicker, more concise and fun way - bringing the power of Python and Ruby inside the Java platform.

[Groovy] One of the main features of Groovy is that it is very easy for Java programmers to learn. The Groovy FAQ explains: "One of the main design goals of Groovy is to be a scripting language for Java developers to use. So we wanted to reuse both Java's semantics and the whole set of J2SE APIs rather than introduce a port of a different language with different semantics and APIs to learn and implement/maintain."

Some of the basic features of Groovy include:

  • It is a JRE-compliant scripting language.
  • It is an agile development language.
  • The core syntax is based on Java.
  • It supports existing Java objects and libraries.
  • Groovy is interpreted, compilation is performed at run time.
  • It supports command line operation.
  • Variables are dynamically typed.
  • Tuples, lists, maps, and closures are part of the basic syntax.
  • Closures are used for passing blocks of executable code.
  • Regular expressions are supported with ~"..." expressions.
  • Groovy provides operator overloading capabilities.
Some of Groovy's built-in components include:
  • GroovyMarkup for native support of XML, HTML, SAX and other markup languages.
  • The GPath path expression language.
  • Support for writing Java servlets (Groovlets).
  • GroovySql for working with SQL databases.
  • GroovyBeans, a simplified interface to Java Beans.
  • The Groovy Template Engines provide a templating framework.
  • Groovy supports scripting in Ant.
Groovy is available under a BSD / Apache style license. The most recent release of Groovy is version 1.0-beta-5, it was released on May 12, 2004. The code is available for download here. Dependencies include Java 1.4, the Groovy jar, and the ASM library.

If you are interested in learning more about Groovy, take a look at Andrew Glover's introductory article on IBM's developerWorks entitled Feeling Groovy.


(Log in to post comments)

The attitude to licenses is telling

Posted Aug 5, 2004 10:59 UTC (Thu) by angdraug (subscriber, #7487) [Link]

From the FAQ:

Groovy is open source using a BSD / Apache style licence

The link returns 404:

The resource you requested cannot be found.

So, what exactly kind of BSD/Apache license is used by Groovy?

The attitude to licenses is telling

Posted Aug 5, 2004 12:23 UTC (Thu) by hummassa (subscriber, #307) [Link]

/*
$Id: LICENSE.txt,v 1.2 2003/09/26 17:36:36 jstrachan Exp $

Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.

Redistribution and use of this software and associated documentation
("Software"), with or without modification, are permitted provided
that the following conditions are met:

1. Redistributions of source code must retain copyright
statements and notices. Redistributions must also contain a
copy of this document.

2. Redistributions in binary form must reproduce the
above copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other
materials provided with the distribution.

3. The name "groovy" must not be used to endorse or promote
products derived from this Software without prior written
permission of The Codehaus. For written permission,
please contact info@codehaus.org.

4. Products derived from this Software may not be called "groovy"
nor may "groovy" appear in their names without prior written
permission of The Codehaus. "groovy" is a registered
trademark of The Codehaus.

5. Due credit should be given to The Codehaus -
http://groovy.codehaus.org/

THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.

*/

Groovy, a Java-like Scripting Language

Posted Aug 5, 2004 21:53 UTC (Thu) by jonabbey (subscriber, #2736) [Link]

This looks very nice, indeed. We're working on integrating Jython into our app, but Groovy might make an attractive alternative.

Groovy, a Java-like Scripting Language

Posted Aug 6, 2004 2:22 UTC (Fri) by yanfali (subscriber, #2949) [Link]

I'm an intermediate Java programmer and a competent script writer (bash, tcl, and ruby mostly), so here's my experience with groove.

I wrote a simple script to automagically backup a mysql db in it just to test how easy it was to write stuff versus say bash or ruby. It ain't pretty. Firstly, it's well documented from an API point of view but there are almost no examples on the website beyond trivial code.

Secondly, it's obviously early. The release says beta, and two things stick out like sore thumbs:

1. debugging is really quite unclear for anything beyond simple brackets because it spits out full Java Exceptions which you have to parse rather than tell you the line number and what it thinks went wrong; it's in there but you have to dig.

2. there's a distinct lack of convenience libraries. The real power behind most scripting languages isn't the syntax it's the libraries that have been implemented that make them useful. Just look at Python, Perl and Ruby. The libraries are really what make you want to use it.

At this point, groove is a *great* start, but until it has the breadth of library functions of something like Ruby it's a bit of a pain to use for general scripting. Yes you can use all the Java classes and that's great, but it also means you have to start structuring your script like a Java program which defeats the point of using a scripting language in the first place.

I think the niche uses of JUnit testing and Ant are where it will get it's initial traction and that's not necessarily a bad thing.

For example, here's how I got a simple formatted date out of groove
<code>
def getFileDate() {
#This is ugly
calendar = Calendar.getInstance()
calendar.setTime(new Date())

year = calendar.get(Calendar.YEAR)
month = calendar.get(Calendar.MONTH) + 1
day = calendar.get(Calendar.DAY_OF_MONTH)

#Force to String to Keep Leading Zero
String amonth = month < 10 ? "0" + month : month
String ayear = year.toString()
String aday = day < 10 ? "0" + day : day

return amonth + "_" + aday + "_" + ayear
}
</code>

It's basically weakly-typed Java. A better way would be for me to have some sort of convenience method that parsed say a unix 'date' style string and returned a formatted date string; I guess I'll have to write it myself :) Over time I'm sure the issue of helper methods will be sorted out and I look forward to future releases, but right now it's frustrating.

On the plus side, the great things that stand out are autoboxing of variables, much more relaxed syntax and useful constructs for looping across objects. With time I think this will become a useful tool in the arsenal of any Java developer. I think it would be great if they could set up a cookbook on the codehaus website with recipes, this would ease the learning curve enormously and help build a community spirit.

Groovy, a Java-like Scripting Language

Posted Aug 12, 2004 18:16 UTC (Thu) by guymcarthur (guest, #23966) [Link]

The article above says 'you can use the whole set of J2SE APIs'. Your convenience method is provided in java.util.SimpleDateFormat.

Copyright © 2004, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds
Powered by Rackspace Managed Hosting.