Cloud Native Buildpacks/Paketo with Java/Spring Boot:如何配置不同的 JDK 下载 uri(例如无法访问 github.com) [英] Cloud Native Buildpacks/Paketo with Java/Spring Boot: How to configure different JDK download uri (e.g. no access to github.com)

查看:29
本文介绍了Cloud Native Buildpacks/Paketo with Java/Spring Boot:如何配置不同的 JDK 下载 uri(例如无法访问 github.com)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

拥有一个 Spring Boot 应用程序,我尝试使用 spring-boot-maven-plugin 目标 mvn spring-boot:build-image 构建它.但是构建无法从 github.com 下载 bellsoft-jre11.0.9.1+1-linux-amd64.tar.gz,因为我无法访问它来自我的构建管道:

<代码>...Paketo BellSoft Liberica Buildpack 5.2.1https://github.com/paketo-buildpacks/bellsoft-liberica构建配置:$BP_JVM_VERSION 11.0.9 Java 版本启动配置:$BPL_JVM_HEAD_ROOM 0 内存计算空间$BPL_JVM_LOADED_CLASS_COUNT 35% of classes 内存计算中加载的类数$BPL_JVM_THREAD_COUNT 250 内存计算中的线程数$JAVA_TOOL_OPTIONS JVM 启动标志BellSoft Liberica JDK 11.0.9:对层的贡献从 https://github.com/bell-sw/Liberica/releases/download/11.0.9.1+1/bellsoft-jdk11.0.9.1+1-linux-amd64.tar.gz 下载无法调用图层创建者无法获得依赖jdk无法下载 https://github.com/bell-sw/Liberica/releases/download/11.0.9.1+1/bellsoft-jdk11.0.9.1+1-linux-amd64.tar.gz无法请求 https://github.com/bell-sw/Liberica/releases/download/11.0.9.1+1/bellsoft-jdk11.0.9.1+1-linux-amd64.tar.gz错误:构建失败:退出状态 1

有什么方法可以将 bellsoft-jdk11.0.9.1+1-linux-amd64.tar.gz 下载到我的构建管道可访问的位置并配置 bellsoft-liberica buildpack改用这个?

解决方案

根据文档:

<块引用>

Paketo Buildpacks 可以从 Internet 下载依赖项.对于例如,Java Buildpack 将下载 BellSoft Liberica JRE默认情况下来自 Liberica github 版本.如果无法从构建环境访问依赖项 URI,则可以使用绑定将新的 URI 映射到给定的依赖项.

使用 spring-boot-maven-plugin(或 Gradle 插件)配置绑定需要 Spring Boot 2.5+.如果您使用的是旧版本,则需要升级或切换到 pack CLI.

=== 使用带绑定的 pack CLI 配置不同的 JDK 下载 uri ===

打包文档告诉我们一般的绑定目录的布局(/platform/bindings 稍后在包构建容器中创建):

/chooseYourBindingsName├── key-name-of-our-buildpacks-binding-configuration└── type-name-of-our-buildpacks-binding-configuration

1.创建绑定目录

那么让我们尝试创建一个完全运行的示例!为了将绑定配置移交给 pack CLI,我们需要先创建一个目录:

mkdir bellsoft-jdk-config &&cd bellsoft-jdk-config

2.创建文件类型,包含绑定密钥

现在我们需要在此目录中创建一个名为 type 的文件,其中包含 bellsoft-liberica 绑定类型dependency-mapping:

echo 依赖映射">>类型

一个新文件 type 应该存在于包含字符串 dependency-mapping 的目录中.

3.从 buildpack.toml 中选择 JDK 版本

由于我们要更改 JDK 的 bellsoft-liberica 下载 uri,我们需要决定我们要使用的 JDK 版本. bellsoft-liberica buildpack 的 buildpack.toml 概述了 buildpack 中可用的 JRE/JDK 版本.对于这里的这个例子,我使用了最新的 JDK 版本 11,它在 buildpack.toml 中配置如下:

<代码>...[[metadata.dependencies]]id =jdk"名称 = BellSoft Liberica JDK"版本 = 11.0.9"uri = https://github.com/bell-sw/Liberica/releases/download/11.0.9.1+1/bellsoft-jdk11.0.9.1+1-linux-amd64.tar.gz"sha256 = 786c48fa6429d6a3f0afb189a65f0a43772e42afbab836852b9a1fdfdb8fc502"stacks = [ "io.buildpacks.stacks.bionic", "org.cloudfoundry.stacks.cflinuxfs3";]...

4.下载 JDK

确定版本后,我们需要从 uri 字段中提供的位置下载 JDK 到我们稍后在构建环境中可以访问的位置(因为我们无法访问 github.com).假设我们已经下载了 JDK,并且可以从 http://your-accessible-uri-to/bellsoft-jdk11.0.9.1+1-linux-amd64.tar.gz 获得.>

5.创建名为 sha256 的文件,其中包含 JDK uri

现在我们应该创建另一个文件,完全根据[[metadata.dependencies]]部分的sha256摘要值命名.我们在 buildpack.toml 中选择的 JDK 版本.此文件必须包含我们下载的 JDK 的 uri:

echo "http://your-accessible-uri-to/bellsoft-jdk11.0.9.1+1-linux-amd64.tar.gz";>>786c48fa6429d6a3f0afb189a65f0a43772e42afbab836852b9a1fdfdb8fc502

最后我们的目录 bellsoft-jdk-config 应该符合 包 CLI 绑定目录 docs 看起来像这样:

/bellsoft-jdk-config├── 786c48fa6429d6a3f0afb189a65f0a43772e42afbab836852b9a1fdfdb8fc502└── 类型

6.使用 --volume 执行 pack CLI 进行绑定 &BP_JVM_VERSION

最后我们可以发出我们的 pack CLI 命令.确保 pack CLI 已安装在您的系统上.还请务必使用 --env BP_JVM_VERSION=exactJDKversionNumberHere 环境变量配置,与您下载的JDK版本和buildpack.toml:

pack build your-application-name-here \--路径.\--volume $(pwd)/bellsoft-jdk-config:/platform/bindings/bellsoft-jdk-config \--env BP_JVM_VERSION=11.0.9 \--builder paketobuildpacks/builder:base

现在bellsoft-liberica buildpack将从http://your-accessible-uri-to/bellsoft-jdk11.0.9.1+1-linux-amd64.tar.gz<下载JDK tar.gz:

<代码>...Paketo BellSoft Liberica Buildpack 5.2.1https://github.com/paketo-buildpacks/bellsoft-liberica构建配置:$BP_JVM_VERSION 11.0.9 Java 版本启动配置:$BPL_JVM_HEAD_ROOM 0 内存计算空间$BPL_JVM_LOADED_CLASS_COUNT 35% of classes 内存计算中加载的类数$BPL_JVM_THREAD_COUNT 250 内存计算中的线程数$JAVA_TOOL_OPTIONS JVM 启动标志BellSoft Liberica JDK 11.0.9:对层的贡献从 http://your-accessible-uri-to/bellsoft-jdk11.0.9.1+1-linux-amd64.tar.gz 下载...

Having a Spring Boot app I tried to build it using the spring-boot-maven-plugin goal mvn spring-boot:build-image. But the build fails downloading the bellsoft-jre11.0.9.1+1-linux-amd64.tar.gz from github.com, since I don't have access to it from my build pipeline:

...
Paketo BellSoft Liberica Buildpack 5.2.1
  https://github.com/paketo-buildpacks/bellsoft-liberica
  Build Configuration:
    $BP_JVM_VERSION              11.0.9          the Java version
  Launch Configuration:
    $BPL_JVM_HEAD_ROOM           0               the headroom in memory calculation
    $BPL_JVM_LOADED_CLASS_COUNT  35% of classes  the number of loaded classes in memory calculation
    $BPL_JVM_THREAD_COUNT        250             the number of threads in memory calculation
    $JAVA_TOOL_OPTIONS                           the JVM launch flags
  BellSoft Liberica JDK 11.0.9: Contributing to layer
    Downloading from https://github.com/bell-sw/Liberica/releases/download/11.0.9.1+1/bellsoft-jdk11.0.9.1+1-linux-amd64.tar.gz
unable to invoke layer creator
unable to get dependency jdk
unable to download https://github.com/bell-sw/Liberica/releases/download/11.0.9.1+1/bellsoft-jdk11.0.9.1+1-linux-amd64.tar.gz
unable to request https://github.com/bell-sw/Liberica/releases/download/11.0.9.1+1/bellsoft-jdk11.0.9.1+1-linux-amd64.tar.gz
ERROR: failed to build: exit status 1

Is there a way I can download the bellsoft-jdk11.0.9.1+1-linux-amd64.tar.gz to a location accessible to my build pipeline and configure the bellsoft-liberica buildpack to use this instead?

解决方案

According to the docs:

Paketo Buildpacks may download dependencies from the internet. For example, the Java Buildpack will download the BellSoft Liberica JRE from the Liberica github releases by default. If a dependency URI is inaccessible from the build environment, a binding can be used to map a new URI to a given dependency.

Configuring bindings using the spring-boot-maven-plugin (or the Gradle plugin) requires Spring Boot 2.5+. If you are on an older version, you need to upgrade or switch over to pack CLI.

=== Use pack CLI with bindings to configure a different JDK download uri ===

The pack docs tell us about the general layout of a binding directory (/platform/bindings is later created inside the pack build container):

/chooseYourBindingsName
├── key-name-of-our-buildpacks-binding-configuration
└── type-name-of-our-buildpacks-binding-configuration

1. Create bindings directory

So let's try to create a fully running example! In order to hand over the binding configuration to pack CLI we need to create a directory first:

mkdir bellsoft-jdk-config && cd bellsoft-jdk-config

2. Create file type, containing the binding key

Now we need to create a file called type inside this directory containing the binding key for the bellsoft-liberica binding type dependency-mapping:

echo "dependency-mapping" >> type

A new file type should be present in the directory containing the string dependency-mapping.

3. Choose JDK version from buildpack.toml

As we want to change the bellsoft-liberica's download uri of the JDK, we need to decide which JDK version we exaclty want to use. The bellsoft-liberica buildpack's buildpack.toml gives an overview on which JRE/JDK versions are available inside the buildpack. For this example here I used the latest JDK version 11 which is configured inside the buildpack.toml like this:

...

[[metadata.dependencies]]
id      = "jdk"
name    = "BellSoft Liberica JDK"
version = "11.0.9"
uri     = "https://github.com/bell-sw/Liberica/releases/download/11.0.9.1+1/bellsoft-jdk11.0.9.1+1-linux-amd64.tar.gz"
sha256  = "786c48fa6429d6a3f0afb189a65f0a43772e42afbab836852b9a1fdfdb8fc502"
stacks  = [ "io.buildpacks.stacks.bionic", "org.cloudfoundry.stacks.cflinuxfs3" ]
...

4. Download JDK

Having decided on the version, we need to download the JDK from the location provided inside the uri field to a location we have access to later inside our build environment (since we don't have access to github.com). Let's assume, we have the JDK downloaded and available at http://your-accessible-uri-to/bellsoft-jdk11.0.9.1+1-linux-amd64.tar.gz.

5. Create file named as the sha256, containing the JDK uri

Now we should create another file named exactly according to the sha256 digest value of the [[metadata.dependencies]] section of the JDK version we chose inside the buildpack.toml. This file must contain the uri of our downloaded JDK:

echo "http://your-accessible-uri-to/bellsoft-jdk11.0.9.1+1-linux-amd64.tar.gz" >> 786c48fa6429d6a3f0afb189a65f0a43772e42afbab836852b9a1fdfdb8fc502

In the end our directory bellsoft-jdk-config should comply to the pack CLI bindings directory docs and looks somehow like this:

/bellsoft-jdk-config
├── 786c48fa6429d6a3f0afb189a65f0a43772e42afbab836852b9a1fdfdb8fc502
└── type

6. Execute pack CLI with --volume for binding & BP_JVM_VERSION

Finally we can issue our pack CLI command. Ensure that pack CLI is installed on your system. Be also sure to also provide the exact JDK version number using the --env BP_JVM_VERSION=exactJDKversionNumberHere environment variable configuration, which matches your downloaded JDK version and the section in the buildpack.toml:

pack build your-application-name-here \
    --path . \
    --volume $(pwd)/bellsoft-jdk-config:/platform/bindings/bellsoft-jdk-config \
    --env BP_JVM_VERSION=11.0.9 \
    --builder paketobuildpacks/builder:base

Now the bellsoft-liberica buildpack will download the JDK tar.gz from http://your-accessible-uri-to/bellsoft-jdk11.0.9.1+1-linux-amd64.tar.gz:

...
Paketo BellSoft Liberica Buildpack 5.2.1
  https://github.com/paketo-buildpacks/bellsoft-liberica
  Build Configuration:
    $BP_JVM_VERSION              11.0.9          the Java version
  Launch Configuration:
    $BPL_JVM_HEAD_ROOM           0               the headroom in memory calculation
    $BPL_JVM_LOADED_CLASS_COUNT  35% of classes  the number of loaded classes in memory calculation
    $BPL_JVM_THREAD_COUNT        250             the number of threads in memory calculation
    $JAVA_TOOL_OPTIONS                           the JVM launch flags
  BellSoft Liberica JDK 11.0.9: Contributing to layer
    Downloading from http://your-accessible-uri-to/bellsoft-jdk11.0.9.1+1-linux-amd64.tar.gz 
...

这篇关于Cloud Native Buildpacks/Paketo with Java/Spring Boot:如何配置不同的 JDK 下载 uri(例如无法访问 github.com)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆