Ph: 1930110588
Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Email Address:

Build scripts with Groovy and Ant

Enhance your automated build process with powerful and flexible Groovy scripts

Print Email Feedback Resources Discuss
Digg Reddit SlashDot Stumble del.icio.us Technorati dzone

Ant has been widely celebrated as the future of build processing. In fact, it represents a great improvement over previous make tools: it is extendible, multiplatform, and standard-based. Some Ant characteristics particularly make it the tool of choice:

It is simple to use, even for junior developers, thanks to its XML syntax It is reasonably fast It is extensible It offers good support for J2EE packages and practices It is integrated out-of-the-box with several popular tools, such as JUnit, and with most important servers in the market


It has achieved first place in nearly all existing J2EE projects for everything related to process automation: building, packaging, testing, and deploying software.

However, as a consequence of this wide adoption and usage in such different (and sometimes unexpected) scenarios, Ant has some disadvantages over a more traditional building process based on scripts. First of all, though you can do most tasks with Ant, in some situations, it is simpler and quicker to use a more traditional scripting language (like Python, Perl, or Ruby) to perform specific jobs like connecting to servers and applications or processing files. As a result, you often see a mixture of XML build files and script batch files in project automation tools.

Moreover, the XML syntax doesn't allow us to use an object-oriented language's most common and useful characteristics, such as inheritance and polymorphism; in simple terms, you cannot extend (in the object-oriented meaning) an XML build file or write polymorphic XML files. These limitations result in every complex project having a complex build system.

The problem seems to be the approach: with a script-centric system, you have a flexible and powerful instrument, but you need to know the script language very well; on the contrary, with an XML and platform-independent approach, you have simplicity and reusability, but a bit less power.

But we want it all, and now. What's the solution? A script language with a native and simple support for XML syntax. Groovy.

The GroovyMarkup

Groovy is a new dynamic scripting language for the JVM, similar to famous scripting languages like Python and Ruby, but, at the same time, easy to learn and use by Java developers. The good news is that its standardization is underway in the Java Community Process as Java Specification Request 241, so in the future, it is going to be the second (after Java) official language for the JVM.

Note: Since this article is not an introduction to Groovy, it won't cover the basics of this language; you can find a list of introductory articles in Resources.

Groovy natively supports different markup languages like XML, HTML, and SOAP, thanks to a feature called GroovyMarkup. This means it defines a specific syntax to express tree-like structures. For example, the simple XML structure:

<book isbn="1234-5678" category="A-123">
   <author>Paul Jones</author>
   <title>The zen and the art of the scripting</title>
   <publisher>XYZ Pub. Co.</publisher>
   <year>2004</edition> 
</book>


can be described with the following Groovy script:

Print Email Feedback Resources
Digg Reddit SlashDot Stumble del.icio.us Technorati dzone
Comment
Welcome, Logout
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources
Download the source code that accompanies this article
http://www.javaworld.com/javaworld/jw-10-2004/groovy/jw-1004-groovy.zip
Java Development with Ant,
http://www.amazon.com/exec/obidos/ASIN/1930110588/javaworld Erik Hatcher, Steve Loughran, (Manning Publications, August 2002; ISBN1930110588)
Ant homepage
http://ant.apache.org/
"alt.lang.jreFeeling Groovy," Andrew Glover (developerWorks, August 2004)
http://www-106.ibm.com/developerworks/library/j-alj08034.html
Groovy homepage
http://groovy.codehaus.org
"Groovy—Scripting for Java," Mark Volkmann
http://www.ociweb.com/jnb/jnbFeb2004.html
For an introduction to Ant, read "Automate Your Build Process Using Java and Ant," Michael Cymerman (JavaWorld, October 2000)
http://www.javaworld.com/javaworld/jw-10-2000/jw-1020-ant.html
For more articles on development tools, browse the Development Tools section of JavaWorld's Topical Index
http://www.javaworld.com/channel_content/jw-tools-index.shtml
For more articles covering XML, browse the Java and XML section of JavaWorld's Topical Index
http://www.javaworld.com/channel_content/jw-xml-index.shtml
Also, browse the Java 2 Platform, Enterprise Edition (J2EE) section of JavaWorld's Topical Index
http://www.javaworld.com/channel_content/jw-j2ee-index.shtml?



You are viewing a mobilized version of this site...
View original page here

Mobilized by Mowser Mowser