Jenkins/Groovy:如何向同一方法传递命名参数与闭包?
MissingMethodException for dockerBuildWith Let's break down what's going wrong here and how to fix it quickly.
Root Cause
Your shared library method is defined with the signature:
call(Map kwargs, String image_name, String version, Closure closure)
But your call is passing arguments in the wrong order:
dockerBuildWith("go-build", "latest", image_context: "build", changesets: ["build/Dockerfile"]) { ... }
Groovy doesn't automatically pack those image_context/changesets key-value pairs into a Map when they're not the last parameter. Instead, it's treating them as separate arguments, leading to a mismatch between the method you're trying to call and the one that exists. The error message even hints at this—notice it lists a possible method with signature call(java.lang.String, java.lang.String, groovy.lang.Closure) (no Map parameter), meaning Jenkins can't find your four-parameter method because the argument types don't align.
Solution 1: Match the Method Signature's Parameter Order
Explicitly pass the Map as the first argument, wrapped in square brackets:
dockerBuildWith( [image_context: "build", changesets: ["build/Dockerfile"]], "go-build", "latest" ) { // Your closure logic here }
This directly matches the call(Map kwargs, String image_name, String version, Closure closure) signature the shared library expects.
Solution 2: Adjust the Shared Library Method for Friendlier Calling
If you prefer to keep the more readable "named parameter" style of your original call, modify the shared library method's signature to accept the Map as the last parameter (before the closure):
call(String image_name, String version, Map kwargs = [:], Closure closure)
With this change, your original call will work as-is—Groovy automatically packs any trailing key-value pairs into the kwargs Map. The = [:] makes the Map optional, so you can still call the method without passing those extra parameters if needed.
内容的提问来源于stack exchange,提问作者Isaac Freeman

