Does groovy provide an include mechanism?

Christopher Klewes picture Christopher Klewes · Aug 12, 2009 · Viewed 13.7k times · Source

We are searching for an include mechanism for groovy scripts to have space for cross-cutting-concerns.

In my example we have, web service endpoints as groovy scripts and want to log to our web service protocol. for that we use our implicit object (getting from our framework) to create the logging statement.

But this is boilerplate code if we code this in every web service endpoint.

We are searching for something like include() in php, that includes other groovy scripts, are there any ideas how to do this?

Answer

Anton Mamaenko picture Anton Mamaenko · Dec 2, 2010

Groovy treats its files as objects (think of it as of automatic wrapping). And it makes all .groovy files within the java classpath available as classes. So if you have the file util.groovy, that contains something like this inside:

def static AuxMethod() {
    return "Hello World"
}

To call it from another file you just write:

println util.AuxMethod()

That's it. Again, just make sure that your util.groovy file is in the classpath.