Reputation: 48
I have a Jenkins shared Library that loads custom methods:
sharedLibrary.groovy
def hello(String world) {
if (world) {
echo "${world}"
}
else {
echo "no parameter"
}
}
The method is used in a Jenkins declarative pipeline
stages {
stage('Test and Package JAR') {
steps {
script {
sharedLibrary.hello("")
}
}
}
}
As long as I call the method with sharedLibrary.hello("")
or sharedLibrary.hello("Hello World!")
everything works as expected.
But If I call it with sharedLibrary.hello()
(using no quotes) I receive the following error even though to my knowledge this is valid groovy code.
java.lang.NoSuchMethodError: No such DSL method 'hello' found among steps
What is the reason behind this? It seems counterintuitive to pass ""
if I don't want to pass any input at all.
Upvotes: 0
Views: 922
Reputation: 1334
The behavior you're looking for can be achieved with default values:
def sayIt(it = "hi"){
println(it)
}
sayIt()
sayIt("Hello")
Also I would only recommend it in cases with very clear and sensible default values. In the case of "" I would advise against it, since most of the time an empty string is not the result you are looking for.
Upvotes: 1