Your browser was unable to load all of the resources. They may have been blocked by your firewall, proxy or browser configuration.
Press Ctrl+F5 or Ctrl+Shift+R to have your browser try again.

Able to create user custom function in somewhere and share in everywhere. #4238

JerryLee ·
  • replies 7
  • views 81
  • stars 0
robinshen ADMIN ·

This should be able to be solved with my suggestion in the issue comment. Please let me know if it works.

JerryLee ·

Thanks to reply it.
However, it doesn't make sense and does not work.

I think that I need to tell you the scenario with the code.

here is my funcA

${groovy:
    def print_log(String s) {
        logger.debug(s);
    \}
}

I want to use this 'print_log' in any step.

Please let me know what I have to do.

robinshen ADMIN ·

You may define a variable say "utils" and define common functions, for instance:

def print_log(String s) {
  logger.debug(s);
}

Then anywhere you want to call print_log, write it like below:

groovy:
def utils = Eval.me("logger", logger, vars.getValue("utils"));

utils.print_log("some log message");
JerryLee ·

I created a variable 'utils' and a step 'test_command' as following your comment.
Unfortunately, it says null point exception like shown below.
Could you check it, once again?

ERROR - Step 'master>test command' is failed: java.lang.NullPointerException: Cannot invoke method print_log() on null object
at org.codehaus.groovy.runtime.NullObject.invokeMethod(NullObject.java:77)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:45)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
at org.codehaus.groovy.runtime.callsite.NullCallSite.call(NullCallSite.java:32)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:54)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at script15890722679861485409386.run(script15890722679861485409386.groovy:4)
at com.pmease.quickbuild.plugin.basis.BasisPlugin$32.evaluate(BasisPlugin.java:382)
at com.pmease.quickbuild.DefaultScriptEngine.evaluate(DefaultScriptEngine.java:305)
at com.pmease.quickbuild.DefaultScriptEngine....

robinshen ADMIN ·

Sorry I forget to return the object. Please modify content of variable "utils" like below:

def print_log(String s) {
  logger.debug(s);
}
return this
JerryLee ·

I really appreciate it your replies, it works well.
It might be the last question regarding this.
Could you let me know how do I use various class?

def utils = Eval.me("logger", logger, utils_src);

In this case, only "logger" class can use in 'utils_src'.
I would like to use not only "logger", but also others.

robinshen ADMIN ·

You can pass a map and access various objects via the map. For instance:

def utils = Eval.me("context", ["logger": logger, "build": build], utils_src);

In your utils src you can access context.logger, context.build, etc.