Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Groovy Libraries allow custom Groovy functions and helpers to be shared and reused across all Groovy scripts in FlexDeploy

Table of Contents
maxLevel6
minLevel1
include
outlinefalse
indent
exclude^Groovy Libraries allow
stylenone
typelist
printabletrue
class

...

The authored script code for each library is wrapped in a class declaration like so

Code Block
languagegroovy
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
languagegroovy
def LIB_MY_LIB = new com.flexagon.groovy.custom.LIB_MY_LIB(); 

Importing Other Custom Libraries

...

Code Block
languagegroovy
// 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
languagegroovy
// this WOULD work in the way LIB_PIPELINE_UTILS is using it
static void logMessage(String message)
{
  // log
}

// this would NOT work in the way LIB_PIPELINE_UTILS is using it
void logMessage(String message)
{
  // log
}

LIB_PIPELINE_UTILS

Code Block
languagegroovy
import com.flexagon.groovy.custom.LIB_LOGGER;

void myPipelineFunction()
{
  LIB_LOGGER.logMessage("Logger library static method")
  // code omitted for brevity
}

...