Cloud Native Buildpacks/Paketo with Java/Spring Boot:如何配置不同的 JDK 下载 uri(例如无法访问 github.com)

问题描述 投票:0回答:6

有了一个 Spring Boot 应用程序,我尝试使用

spring-boot-maven-plugin
目标
mvn spring-boot:build-image
来构建它。但是构建无法从
bellsoft-jre11.0.9.1+1-linux-amd64.tar.gz
下载
github.com
,因为我无法从构建管道访问它:

...
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

有没有办法可以将

bellsoft-jdk11.0.9.1+1-linux-amd64.tar.gz
下载到我的构建管道可访问的位置,并配置bellsoft-liberica 构建包来使用它?

java spring spring-boot buildpack paketo
6个回答
18
投票

根据文档

Paketo Buildpacks 可以从互联网下载依赖项。为了 例如,Java Buildpack 将下载 BellSoft Liberica JRE 默认来自 Liberica github 版本。如果无法从构建环境访问依赖项 URI,则可以使用绑定将新 URI 映射到给定依赖项。

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

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

pack 文档告诉我们绑定目录的一般布局(

/platform/bindings
稍后在pack构建容器内创建):

/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 "dependency-mapping" >> type

新文件

type
应该出现在包含字符串
dependency-mapping
的目录中。

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

由于我们想要更改 JDK 的bellsoft-liberica 的下载 uri,我们需要决定我们确切要使用哪个 JDK 版本。bellsoft-liberica buildpack 的 buildpack.toml 概述了哪些 JRE/JDK 版本可以在 buildpack 中找到。对于这里的示例,我使用了最新的 JDK 版本

11
,它在
buildpack.toml
内配置如下:

...

[[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。下载JDK

决定版本后,我们需要将 JDK 从

uri
字段中提供的位置下载到我们稍后可以在构建环境中访问的位置(因为我们无法访问 github.com) 。假设我们已下载 JDK 并可在
http://your-accessible-uri-to/bellsoft-jdk11.0.9.1+1-linux-amd64.tar.gz
获取。

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

现在我们应该创建另一个 文件,其名称与我们在

buildpack.toml
中选择的 JDK 版本的 sha256
 部分的 
[[metadata.dependencies]] 摘要值 完全一致。该文件必须包含我们下载的 JDK 的 uri:

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

最终我们的目录

bellsoft-jdk-config
应该符合包CLI绑定目录文档并且看起来像这样:

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

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

最后我们可以发出

pack
CLI 命令。确保您的系统上安装了pack CLI。另请务必使用 --env BP_JVM_VERSION=exactJDKversionNumberHere
 
环境变量配置 提供准确的 JDK 版本号,该版本号与您下载的 JDK 版本和 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
现在,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.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 ...
    

2
投票
我创建绑定:

/bindings/bellsoft-jdk-config ├── 786c48fa6429d6a3f0afb189a65f0a43772e42afbab836852b9a1fdfdb8fc502 ├── a3092627b082cb3cdbbe4b255d35687126aa604e6b613dcda33be9f7e1277162 ├── be27df8838a6d069a2212de5f46da4e39f33f087f2e77c8a725d0f7ec8b5273e ├── d9ff2d84528a2154ff669b85e6dbdee7f244194dcc64e0a8a1bedc470b3bcf56 └── type
然后创建一个 Dockerfile,复制这些绑定并基于前一个平台构建一个新平台:

FROM paketobuildpacks/builder:0.0.464-base-platform-api-0.3 COPY bindings /platform/bindings CMD ["/bin/bash"]
docker build -t your-repo-url/java-builder-test:1 .
docker push your-repo-url/java-builder-test:1
然后我配置了spring插件来使用这个平台:

<configuration> <imageBuilder>your-repo-url/java-builder-test:1</imageBuilder> <layers> <enabled>true</enabled> </layers> <image> <name>your-repo-url/${project.artifactId}:${project.version}</name> </image> </configuration>
此解决方法有效,但您必须使用正确的目录权限。


0
投票
我将 pom.xml 中的 Java 版本更改为 17:

<properties> <java.version>17</java.version> <spring-cloud.version>2020.0.2</spring-cloud.version> </properties>
    

0
投票
我将 pom.xml 文件中的 java 版本更改为 17 并且成功了!


0
投票
更改pom.xml中的Java版本即可。我已将其更改为 java 17。它对我有用。


0
投票
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <image> <env> <BP_JVM_VERSION>22</BP_JVM_VERSION> </env> <buildpacks> <buildpack>paketobuildpacks/azul-zulu</buildpack> <buildpack>urn:cnb:builder:paketo-buildpacks/syft</buildpack> <buildpack>urn:cnb:builder:paketo-buildpacks/executable-jar</buildpack> <buildpack>urn:cnb:builder:paketo-buildpacks/spring-boot</buildpack> </buildpacks> <builder>paketobuildpacks/builder-jammy-base</builder> </image> </configuration> </plugin>
我用这个配置解决了问题。

© www.soinside.com 2019 - 2024. All rights reserved.