Groovy Libraries allow custom Groovy functions and helpers to be shared and reused across all Groovy scripts in FlexDeploy
Table of Contents | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
...
The authored script code for each library is wrapped in a class declaration like so
Code Block | ||
---|---|---|
| ||
package com.flexagon.groovy.custom; // imports in the script are moved here public class LIB_MY_LIB { // your script code is inserted here } |
...
Finally, when running a Groovy script, an instance of this library class is instantiated and made available to the script as a variable matching the Library Key.
You can think of it as FlexDeploy automatically adding the following lines at the beginning of all of your Groovy scripts:
Code Block | ||
---|---|---|
| ||
def LIB_MY_LIB = new com.flexagon.groovy.custom.LIB_MY_LIB(); |
Importing Other Custom Libraries
...
Code Block | ||
---|---|---|
| ||
// imports omitted for brevity // create a new instance of our custom library // the LOG variable should be passed in the constructor for any imported custom library instance def logger = new com.flexagon.groovy.custom.LIB_LOGGER(LOG) void myPipelineFunction() { logger.logMessage("Logger library import") // code omitted for brevity } |
Static Methods
Methods can be called statically, without an instance created, if they have the static modifier. Note - this only applies when importing other custom libraries since other Groovy scripts have instances created behind the scenes. See Library Compilation above.
For example:
LIB_LOGGER
Code Block | ||
---|---|---|
| ||
static void logMessage(String message)
{
// log
}
void logMessage2(String message)
{
// log
} |
LIB_PIPELINE_UTILS
Code Block | ||
---|---|---|
| ||
import com.flexagon.groovy.custom.LIB_LOGGER;
def loggerInstance = new LIB_LOGGER(LOG);
void myPipelineFunction()
{
// This WILL work
LIB_LOGGER.logMessage("Logger library static method")
// This will NOT work
LIB_LOGGER.logMessage2("Logger library instance method")
// BOTH of these will work
loggerInstance.logMessage("Logger library static method")
loggerInstance.logMessage2("Logger library instance method")
} |
Note |
---|
Static method caveat A final note on static methods - Static methods cannot access instance variables. This means if you have a static method defined in your library and try to access In this case you must either remove the static modifier or not use the variable in question. |
Circular Imports
Warning |
---|
Circular imports of custom libraries are not allowed and if found an error will be thrown |
...