Cloud Native Buildpacks/Paketo with Java/Spring Boot: How to configure different JDK download uri (e.g. no access to github.com)

后端 未结 2 1093
一向
一向 2021-01-27 09:58

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 <

2条回答
  •  借酒劲吻你
    2021-01-27 10:35

    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.

    For now configuring bindings isn't possible using the spring-boot-maven-plugin (or the Gradle plugin). Therefore we need to 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 
    ...
    

提交回复
热议问题