Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 578
Zombies Avatar asked Apr 15 '10 13:04

Zombies


People also ask

Is Groovy a scripting language?

Groovy is a scripting language with Java-like syntax for the Java platform. The Groovy scripting language simplifies the authoring of code by employing dot-separated notation, yet still supporting syntax to manipulate collections, Strings, and JavaBeans.

Is Groovy scripting easy?

Groovy programming language is much easier to learn and much of the code that you write using it will compile and work as expected. The learning curve for Groovy is small. It is not difficult for someone who is proficient in Java to get started with Groovy. The build tool is rapidly gaining popularity.

Is Groovy script same as Javascript?

Groovy makes it easier for the javascript developer as it uses fewer lines of code which results in the same output as the javascript code. There is little difference found in groovy wrt to javascript some of them are as follows i.e function is replaced by def in groovy, {} are replaced by [] .

Does Jenkins uses Groovy as its scripting language?

Jenkins features a Groovy script console which allows one to run arbitrary Groovy scripts within the Jenkins controller runtime or in the runtime on agents.


1 Answers

@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.

like image 156
Binil Thomas Avatar answered Oct 09 '22 00:10

Binil Thomas