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

...

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
}

Circular Imports

Warning

Circular imports of custom libraries are not allowed and if found an error will be thrown [Circular imports detected. Please check if libraries are creating instances of each other outside of a function scope..] when attempting to test or save the library.

The below example demonstrates a circular library import and why its a problem:

...

  1. FlexDeploy will iterate through all your Groovy Libraries and create instances of each

  2. A new instance of LIB_ONE is created.

  3. During initialization of LIB_ONE it will create a new instance of LIB_TWO as declared on line 1 of LIB_ONE

  4. An instance of LIB_TWO is created during step 3. This in turn creates an instance of LIB_ONE as declared on line 1 of LIB_TWO.

    1. We are now stuck in a loop between steps 3 and 4, infinitely creating new instances. This is what is known as a circular dependency and will result in an error being thrown.

Tip

If it is absolutely necessary to have a circular dependency between libraries then the creation of library instances should be done within a function scope.

  • LIB_ONE

Code Block
def libTwo = new com.flexagon.groovy.custom.LIB_TWO(LOG)

def myFunction() {
  // do some logic
}
  • LIB_TWO

Code Block
import com.flexagon.groovy.custom.LIB_ONE;

def myFunction() {
  def libOne = new LIB_ONE(LOG)
  // do some logic
}