You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CloudBees Core环境下Jenkins共享库Builder模式实例化失败排查

Troubleshooting "unable to resolve class HttpRequest.HttpRequestBuilder" in Jenkins Shared Libraries

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 14:12:50