Skip to content

Groovy Environment Scripting

by Jeff

Problem: You want to use groovy all the time, but ignorant (but paying) forces keep you writing verbose tedious Java code instead.

Solution: Use groovy instead of shell scripts! In large Java development projects, developing in a local environment comes with a maddening list of pulling levers, pushing buttons, and spinning wheels. Many of these can be automated with scripts, and those that can can be Groovy scripts.

I wrote this script to clean up after liferay portal kruft:

#!/usr/bin/env groovy

class Build{
    def ant = new AntBuilder()
    def localhostDir = "C:/dev/liferay/tomcat/work/Catalina/localhost"
    def portraitDir = localhostDir + "/portrait"
    def liferayDir = "C:/Documents and Settings/ujxh744/liferay"
    def luceneDir = liferayDir + "/lucene"
    def classpath = ant.path {
        dirset(dir: liferayDir){
            include(name: "*")
        }
        dirset(dir: localhostDir) {
            include(name: "*")
        }
   }

    void cleanUpLiferayShit() {
        ant.delete(dir: portraitDir)
        ant.delete(dir: luceneDir)
    }

    void echoDirectories(dirPath) {
        def pathList = dirPath.list()
        println "There are " + dirPath.size() + " remaining directories"
        dirPath.list().each{ arg ->
            println arg
        }
    }
}

def b = new Build()
b.cleanUpLiferayShit()
b.echoDirectories( b.getClasspath())

Reference Links:
Javaworld
Groovy Ant site

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

3 Comments

  1. Bark wrote:

    give me one good reason that should be a class or a groovy script.
    How did this make you feel better by writing it in groovy.

    Whats the point actually?

    I could have written this script in 4 lines of script code after the declarations.

    Saturday, July 19, 2008 at 6:32 pm | Permalink
  2. Richard wrote:

    One good reason would be that its cross platform. I have experience where even a bash script would not work the same on two different linux distros, and clearly it wouldn’t work natively on windows, groovy is an ideal solution for this sort of thing.

    Sunday, July 20, 2008 at 5:04 am | Permalink
  3. jeff wrote:

    http://www.merriam-webster.com/dictionary/options
    ;-)

    Tuesday, July 22, 2008 at 2:19 pm | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*