Adding transparent imports to a script

In this recipe, we will learn how to transparently add one or more import statements to a Groovy script.

Why is this feature significant? For a start, it reduces the amount of code required to use a library. But above all, it may be the first step in building your own DSL as it helps to remove friction. This is because your DSL users don't have to write any import statements. The ImportCustomizer class allows the following import variants:

  • Class imports, optionally aliased
  • Star imports
  • Static imports, optionally aliased
  • Static star imports

Getting ready

For the sake of demonstration, we will create a script that requires some classes from the amazing Google Guava library. Additionally, we will also need to statically ...

Get Groovy 2 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.