I have a Jenkinsfile that calls function setup()
from shared-lib my_lib
:
// Jenkinsfile
@Library('my_lib@dev') my_lib
import groovy.json.JsonOutput
pipeline {
agent any
stages {
stage( "1" ) {
steps {
script {
d = my_lib.setup();
}
}
}
}
}
The shared-lib function tries to assign enums to dictionary elements.
// vars/my_lib.groovy
def setup() {
def d = [:]
d.a = org.foo.Foo.Event.A // ok
d.b = my_enum.getEvent() // ok
d.c = my_enum.Event.A // groovy.lang.MissingPropertyException: No such property: Event for class: my_enum
}
// src/org/foo/Foo.groovy
public class Foo {
enum Event {
A,
B;
}
def Foo() {}
}
my_enum.groovy
declares an enum, and a getter-function that returns one of those enums:
// vars/my_enum.groovy
public enum Event {
A,
B;
def Event() {}
}
def getEvent() { return Event.A }
Problem:
The above code fails in my_lib.groovy
at d.c = my_enum.Event.A
with error groovy.lang.MissingPropertyException: No such property: Event for class: my_enum
Questions:
Why does assigning my_enum.Event.A
fail?
How do I define and use a "file-scoped" enum?
Why does assigning an enum scoped to my_enum
fail when a class-scoped enum is okay, and also a simple wrapper function in my_enum
that returns Event.A
also okay?