Customizing Groovy compilation
Whether you are using
groovycto compile classes or a
GroovyShell, for example, to execute scripts, under the hood, a compiler configuration is used. This configuration holds information like the source encoding or the classpath but it can also be used to perform more operations like adding imports by default, applying AST transformations transparently or disabling global AST transformations.
This compiler configuration can be changed and this still holds true even in Martini's development environment.
In fact, Martini itself uses a compilation customizer by default (defined
<martini-home>/conf/MartiniGroovyCompilerConfig.groovy) to apply the following changes to your Groovy
classes and scripts when they're compiled from Martini:
- Transparently import the following packages:
- Parse Groovydoc or Javadoc and implicitly add
@Documentedannotation allows for the automatic population of Swagger annotations and generation of Swagger API definitions.
Take the following snippet below for example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Because of Martini's default compiler configuration, it will be transformed to something roughly like the snippet below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
As you may have observed, Martini has injected import statements and annotations for your convenience whilst preserving the original elements of your code. Martini will do as much as it can to help you write better, less verbose code.
To change Martini's compilation process for Groovy files, edit the
<martini-home>/conf/MartiniGroovyCompilerConfig.groovy file. This configuration file is
made known to the actual Groovy compiler via
With access to the configuration file, you can do plenty of things, like adding an implicit import to all your Groovy files using code like below:
1 2 3 4 5 6
The official Groovy documentation on compilation customizers does a great job at enumerating and discussing behaviors configurable during compilation. There are also plenty of resources available online, like this Groovy Goodness blog post that you may refer to.
It is recommended to restart your Martini instance after changing
MartiniGroovyCompilerConfig.groovy to ensure
the changes are reflected.
This is entirely dependent on Martini's internal implementation As of now, there is no way to use a different filename for the Groovy compiler configuration script. ↩