Search code examples
javagroovyscripting-language

Using Groovy as a scripting language


I prefer to use scripting languages for short tasks, anything such as a really simple http bot, bulk importing/exporting data to/from somewhere, etc etc... Basic throw-away scripts and simple stuff. The point being, that a scripting language is just an efficient tool to write quick programs with. As for my understanding of Groovy at this point...

If you were to program in Groovy, and you wan't to write a quick script, wouldn't you be forced to going back to regular java syntax (and we know how that can be convoluted compared to a scripting language) in order to do anything more complicated? For example, if I want to do some http scripting, wouldn't I just be right back at using java syntax to invoke Commons HttpClient? To me, the point of a scripting language is for quickly typed and less forced constructs. And here is another thing, it doesn't seem that there is any incentive for groovy based libraries to be developed when there are already so many good java one's out there, thus making groovy appear to be a Java dependent language with minor scripting features.

So right now I am wondering if I could switch to Groovy as a scripting language or continue to use a more common scripting language such as Perl, Python or Ruby.


Solution

  • @Zombies, let me show you a quick example from a script I wrote recently:

    def fetch(build, toFile) {
        new FTPClient().with {
            connect ftpServer
            enterLocalPassiveMode()
            login ftpUser, ftpPassword
            changeWorkingDirectory "/var/staging/revision-${build}"
            fileType = FTPClient.BINARY_FILE_TYPE
            toFile.withOutputStream { ostream -> 
                retrieveFile "build-${build}.zip", ostream 
            }
            disconnect()
        }
    }
    

    It uses commons-net API, but I think you would agree that it has a much clearer syntax than comparable Java program. So I don't think using the Java APIs defeats the purpose of having a scripting language. Furthermore, it helps you leverage your existing knowledge of the Java APIs, so is a very pragmatic approach.