问题描述
我正在尝试利用 jib-core 以编程方式构建和推送图像。我希望实现的是从一个容器云注册中心拉取镜像,并将其推送到另一个云注册中心。我相信代码应该是这样的:
/*
* Generate random text - we use this random generated text to change the sha-256 digest to signify that its a new image
* */
private void generaterandomText() {
//generate random 4 KB of data and write them to a file
String uid = UUID.randomUUID().toString();
try (RandomAccessFile f = new RandomAccessFile(RANDOM_TEXT,"rw")) {
// an UUID is 36 bytes times 113 makes 4KB
f.writeBytes(String.join("",Collections.ncopies(113,uid)));
} catch (IOException e) {
e.printstacktrace();
}
}
/**
* pull and push an image from one registry to another
* @param fromregistry - the registry we will be pushing the image from
* @param toRegistry - the registry we will push the image to
* */
public void pullAndPushImagetoRegistry(String fromregistry,String toRegistry) {
generaterandomText();
try {
jib.from(ImageReference.parse(fromregistry))
.addLayer(Arrays.asList(Paths.get(RANDOM_TEXT)),AbsoluteUnixPath.get("/"))
.containerize(
Containerizer.to(RegistryImage.named(toRegistry)
.addCredential("username","password")));
} catch (InterruptedException | RegistryException | IOException | CacheDirectoryCreationException | ExecutionException | InvalidImageReferenceException e) {
LOG.error("Failed to push image " + e.getMessage());
//e.printstacktrace();
}
}
对于 pullAndPushImagetoRegistry(otherregistry.azurecr.io/samples/Nginx,myregistry.azurecr.io/samples/Nginx)
的此类示例,我收到此错误:
Failed to push image java.lang.NoSuchMethodError: com.google.api.client.http.HttpRequest.setUseRawRedirectUrls(Z)Lcom/google/api/client/http/HttpRequest
并且不确定如何解决此问题。
解决方法
您的项目正在引入 Jib 所依赖的旧版 Google HTTP 客户端库。截至目前,related dependencies and their versions 是
com.google.http-client:google-http-client:1.38.1
com.google.http-client:google-http-client-apache-v2:1.38.1
-
com.google.auth:google-auth-library-oauth2-http:0.22.2
(不是 Google HTTP 客户端库的一部分,但这取决于它)
可能其他一些项目库也依赖于 Google HTTP 客户端库(直接或传递),而构建系统(无论是 Gradle 还是 Maven)最终会引入旧版本。
在 Gradle 和 Maven 中,有不同的方式和方法来解决这种依赖冲突,这里是 Maven 中的 example。