CloudBees Core环境下Jenkins共享库Builder模式实例化失败排查
Let's break down the most likely issues you're hitting—Builder pattern missteps and Jenkins shared library class loading quirks are the usual culprits here:
1. Your HttpRequestBuilder isn't a static inner class
Groovy treats non-static inner classes differently than Java: you can't reference them directly via the outer class name without first instantiating the outer class. For the Builder pattern (where you want HttpRequest.HttpRequestBuilder or HttpRequest.builder() to work), your inner builder class must be marked static.
Fix your HttpRequest.groovy structure like this:
package com.yourcompany.http // Use a proper package structure (critical for shared libraries) class HttpRequest { // Your HttpRequest fields String url String method // Static inner Builder class static class HttpRequestBuilder { private HttpRequest request = new HttpRequest() HttpRequestBuilder url(String url) { request.url = url this } HttpRequestBuilder method(String method) { request.method = method this } HttpRequest build() { request } } // Optional convenience method to get a builder instance static HttpRequestBuilder builder() { new HttpRequestBuilder() } }
2. Missing or incorrect imports in Change.groovy
If your HttpRequest class lives in a package (which it should, per Jenkins best practices), you need to import it in Change.groovy before using it. For example:
// At the top of Change.groovy import com.yourcompany.http.HttpRequest // Then use the builder: def request = new HttpRequest.HttpRequestBuilder() .url("https://example.com") .method("GET") .build()
Or use the convenience builder() method for cleaner code:
def request = HttpRequest.builder() .url("https://example.com") .method("GET") .build()
3. Shared library dependency misconfiguration
Since your library (with HttpRequest) is being called by another Jenkins shared library, you need to ensure the calling library has an explicit dependency on your library in Jenkins' configuration:
- Go to Manage Jenkins > Configure System > Global Pipeline Libraries
- Find the calling library (the one containing
Change.groovy) - Under the "Dependencies" section, add your library (the one with
HttpRequest) as a dependency, specifying the correct version/branch if needed.
Without this, Jenkins' classloader won't load your library's classes when the calling library runs, leading to the resolution error.
4. Incorrect shared library directory structure
Jenkins shared libraries require Java-style package structure under the src directory. Make sure your HttpRequest.groovy is placed in a path that matches its package:
your-shared-library/ ├── src/ │ └── com/ │ └── yourcompany/ │ └── http/ │ └── HttpRequest.groovy └── vars/ ...
If you put the file directly under src/ without a package, Jenkins might not load it properly—especially when being called from another library.
5. Jenkins classloader isolation or caching issues
CloudBees Core has stricter classloader isolation than open-source Jenkins. Try these steps to rule out caching:
- Restart your CloudBees controller to clear any stale classloader caches.
- In the shared library configuration, ensure your library is marked as "Global" (if it needs to be accessible to other libraries) or that the calling library has explicit access.
- Re-push any changes to your shared library repository and confirm Jenkins is pulling the latest version (check the "Default version" setting in the library config).
内容的提问来源于stack exchange,提问作者Kaliyug Antagonist

