Jenkins2 pipeline - Global methods in shared-library fails when invoked

1,890 views
Skip to first unread message

Tor Christian Solevågseide

unread,
Jan 20, 2017, 6:24:21 AM1/20/17
to Jenkins Users
As documented in https://jenkins.io/doc/book/pipeline/shared-libraries/, I've created the file vars/acme.groovy in a library project checked into our SVN.


def setName(value) {
   
this.name = value;
}
def getName() {
   
return this.name;
}
def caution(message) {
    echo
"Hello, ${this.name}! CAUTION: ${message}"
}




My pipeline script in a Jenkins2 pipeline project:
acme.name = 'Alice'
echo acme
.name

acme
.caution 'The queen is angry!'




The two first lines, setting and getting the name works fine.
But when I include the last line, calling the "caution" function the script fails with


hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: static Alice.caution() is applicable for argument types: (java.lang.String) values: [The queen is angry!]
Possible solutions: caution(java.lang.Object), wait(), main([Ljava.lang.String;)
at groovy.lang.MetaClassImpl.invokeStaticMissingMethod(MetaClassImpl.java:1503)
at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1489)
at org.codehaus.groovy.runtime.callsite.StaticMetaClassSite.call(StaticMetaClassSite.java:53)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.methodCall(DefaultInvoker.java:18)
at WorkflowScript.run(WorkflowScript:8)



If I remove the first 2 lines of the pipeline script, leaving only the "acme.caution" call, the error detail is changed to
hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: static acme.caution() is applicable for argument types: (java.lang.String) values: [The queen is angry!]


Jenkins version 2.41
Pipeline plugin 2.4


Any idea what's going on here?  Feels like a bug, or at least wrong doc...?

regards
Tor Christian Solevågseide

Tor Christian Solevågseide

unread,
Feb 2, 2017, 4:50:46 AM2/2/17
to Jenkins Users
I was finally able to solve this myself. The problem was how I referenced the shared library. Apparently, I need to add the underscore character to the end if my @Library annotation like this:
@Library('customized-portal-lib') _

Also, I must make sure to not import my global acme variable.

This is documented in https://jenkins.io/doc/book/pipeline/shared-libraries/ for those who take their time to fine read it (which I didn't at first):
For Shared Libraries which only define Global Variables (vars/), or a Jenkinsfile which only needs a Global Variable, the annotation pattern @Library('my-shared-library') _ may be useful for keeping code concise. In essence, instead of annotating an unnecessary import statement, the symbol _ is annotated.
 
It is not recommended to import a global variable/function, since this will force the compiler to interpret fields and methods as static even if they were intended to be instance. The Groovy compiler in this case can produce confusing error messages.

/TC

R. Tyler Croy

unread,
Feb 2, 2017, 6:30:05 AM2/2/17
to jenkins...@googlegroups.com
(replies inline)

On Thu, 02 Feb 2017, Tor Christian Solev?gseide wrote:

> I was finally able to solve this myself. The problem was how I referenced
> the shared library. Apparently, I need to add the underscore character to
> the end if my @Library annotation like this:
> @Library('customized-portal-lib') _
>
> Also, I must make sure to not import my global *acme *variable.
>
> This is documented
> in https://jenkins.io/doc/book/pipeline/shared-libraries/ for those who
> take their time to fine read it (which I didn't at first):


FWIW, the underscore character is kind of a cute little hack. @Library is an
annotation so it must annotate *something*, annotating the underscore is kind
of like annotating a throwaway unused character. Technically you're able to
annotate other statements, as the section in the handbook describes


- R. Tyler Croy

------------------------------------------------------
Code: <https://github.com/rtyler>
Chatter: <https://twitter.com/agentdero>
xmpp: rty...@jabber.org

% gpg --keyserver keys.gnupg.net --recv-key 1426C7DC3F51E16F
------------------------------------------------------
signature.asc
Message has been deleted

Daniel Beck

unread,
Feb 2, 2017, 5:13:36 PM2/2/17
to jenkins...@googlegroups.com

> On 02.02.2017, at 17:27, David Karr <davidmic...@gmail.com> wrote:
>
> Wow, and the documentation page doesn't even MENTION this fact? I'm reading it now, and there's that underscore, completely without any statement like YES, THIS REALLY NEEDS TO BE HERE. Seriously?

Could you clarify what you're referring to? Tor Christian quoted the documentation which states…

David Karr

unread,
Feb 2, 2017, 5:49:27 PM2/2/17
to Jenkins Users, m...@beckweb.net

I perhaps phrased that stronger than it should have. However, the example that comes before this text uses the underscore character without mentioning it.  It's only this note that comes a few lines later on when this is mentioned. When I first wrote that reply (I did try to delete it), I saw the example without any explanation, and didn't notice the explanation until later.
Reply all
Reply to author
Forward
0 new messages