Spring Boot Gradle bootBuildImage task with private repo

5.2k views Asked by At

I'm trying to set up a Spring Boot / Gradle project to use bootBuildImage to build my Docker image, but am hitting a snag. We have limited external network access on our build servers; we use a private nexus repository.

When I try to run the bootBuildImage task, it's attempting to request a file from repo.spring.io:

[creator]     unable to invoke layer creator
[creator]     unable to contribute spring-cloud-bindings layer
[creator]     unable to get dependency spring-cloud-bindings
[creator]     unable to download https://repo.spring.io/release/org/springframework/cloud/spring-cloud-bindings/1.7.0/spring-cloud-bindings-1.7.0.jar
[creator]     unable to request https://repo.spring.io/release/org/springframework/cloud/spring-cloud-bindings/1.7.0/spring-cloud-bindings-1.7.0.jar

I have repo.spring.io added as a repository on our nexus server, but I'm unclear on how to get the Spring Boot Gradle plugin to use it. I tried adding it to my repositories block, but it didn't seem to have an effect.

repositories {
    // Other repositories...
    //
    
    maven {
        url "<my nexus server>/repository/spring-io-releases/"
    }

What am I missing?

3

There are 3 answers

2
Sergi Almar On

The bootBuildImage Gradle task uses Paketo Buildpacks to build your OCI container. There are a bunch of buildpacks participating in the creation of the container, one of them is the Spring Boot Buildpack. Among other things, the Spring Boot Buildpack will add Spring Cloud Bindings to the application classpath:

 [creator]       Spring Cloud Bindings 1.7.0: Contributing to layer
 [creator]         Downloading from https://repo.spring.io/release/org/springframework/cloud/spring-cloud-bindings/1.7.0/spring-cloud-bindings-1.7.0.jar
 [creator]         Verifying checksum
 [creator]         Copying to /layers/paketo-buildpacks_spring-boot/spring-cloud-bindings

This all happens inside the build container and the buildpack is not aware of your Gradle repositories. The dependency URL is defined in the buildpack itself:

[[metadata.dependencies]]
id      = "spring-cloud-bindings"
name    = "Spring Cloud Bindings"
version = "1.7.0"
uri     = "https://repo.spring.io/release/org/springframework/cloud/spring-cloud-bindings/1.7.0/spring-cloud-bindings-1.7.0.jar"
sha256  = "e3c18bf1a3c2e52743f9ff2fa46af59e5eee0a7f0683ff562eb35aa866e4a9e9"
stacks  = [ "io.buildpacks.stacks.bionic", "org.cloudfoundry.stacks.cflinuxfs3" ]

Using the environment variable BPL_SPRING_CLOUD_BINDINGS_ENABLED, you can disable autoconfiguring Spring Boot environment properties from bindings, but the jar file will be pulled anyway.

bootBuildImage {
    environment = ["BPL_SPRING_CLOUD_BINDINGS_ENABLED" : "false"]
}
2
jonashackt On

I had a similar problem where I needed to configure the download uri of the bellsoft-liberica JDK used inside the Spring Boot build image goal/task to not use github.com - but my own private server instead. It cannot be done with buildpack environment variable alone, but you can use bindings for that!

Please note that with the Spring Boot Gradle Plugin or Spring Boot Maven Plugin it requires Spring Boot 2.5+ (a bindings option has been added in 2.5). If you're on an older Spring Boot release, you need to either upgrade or switch over to pack CLI.

Bindings can be configured either through volumes or Kubernetes secrets. I created a fully comprehensible guide on how to use bindings in order to change a uri used inside a buildpack - but I will outline the key steps for switching the spring-cloud-bindings-x.y.z.jar uri:

1. Create bindings directory

In order to hand over the binding configuration to pack CLI we need to create a directory first:

mkdir spring-cloud-config && cd spring-cloud-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 spring-boot buildpack binding type dependency-mapping:

echo "dependency-mapping" >> type

3. Create file named as the sha256, containing the spring-cloud-bindings-x.y.z.jar uri

Now we should create another file named exactly according to the sha256 digest value of the [[metadata.dependencies]] section of the spring-cloud-bindings-1.7.0.jar inside the buildpack.toml:

[[metadata.dependencies]]
id      = "spring-cloud-bindings"
name    = "Spring Cloud Bindings"
version = "1.7.0"
uri     = "https://repo.spring.io/release/org/springframework/cloud/spring-cloud-bindings/1.7.0/spring-cloud-bindings-1.7.0.jar"
sha256  = "e3c18bf1a3c2e52743f9ff2fa46af59e5eee0a7f0683ff562eb35aa866e4a9e9"
stacks  = [ "io.buildpacks.stacks.bionic", "org.cloudfoundry.stacks.cflinuxfs3" ]

This file must contain the uri of your internal nexus incl. the spring-cloud-bindings.jar:

echo "http://<my nexus server>/repository/spring-io-releases/org/springframework/cloud/spring-cloud-bindings/1.7.0/spring-cloud-bindings-1.7.0.jar" >> e3c18bf1a3c2e52743f9ff2fa46af59e5eee0a7f0683ff562eb35aa866e4a9e9

4. Execute pack CLI with --volume to use the binding

Finally we can issue our pack CLI command. Ensure that pack CLI is installed on your system:

pack build your-application-name-here \
    --path . \
    --volume $(pwd)/spring-cloud-config:/platform/bindings/spring-cloud-config \
    --builder paketobuildpacks/builder:base

Alternatively, you can use the bindings option with Spring Boot 2.5+ Maven or Gradle plugins, see links above.

Now the spring-boot buildpack will download the spring-cloud-bindings-1.7.0.jar from http://<my nexus server>/repository/spring-io-releases/org/springframework/cloud/spring-cloud-bindings/1.7.0/spring-cloud-bindings-1.7.0.jar instead of https://repo.spring.io/release.

0
Aldo Inácio da Silva On

I started getting this error now on spring-cloud-bindings-1.7.1 but in the past it worked:

...
[INFO]     [creator]     Paketo Spring Boot Buildpack 4.2.0
[INFO]     [creator]       https://github.com/paketo-buildpacks/spring-boot
[INFO]     [creator]       Creating slices from layers index
[INFO]     [creator]         dependencies
[INFO]     [creator]         spring-boot-loader
[INFO]     [creator]         snapshot-dependencies
[INFO]     [creator]         application
[INFO]     [creator]       Launch Helper: Contributing to layer
[INFO]     [creator]         Creating /layers/paketo-buildpacks_spring-boot/helper/exec.d/spring-cloud-bindings
[INFO]     [creator]       Spring Cloud Bindings 1.7.1: Contributing to layer
[INFO]     [creator]         Downloading from https://repo.spring.io/release/org/springframework/cloud/spring-cloud-bindings/1.7.1/spring-cloud-bindings-1.7.1.jar
[INFO]     [creator]     unable to invoke layer creator
[INFO]     [creator]     unable to contribute spring-cloud-bindings layer
[INFO]     [creator]     unable to get dependency spring-cloud-bindings
[INFO]     [creator]     unable to download https://repo.spring.io/release/org/springframework/cloud/spring-cloud-bindings/1.7.1/spring-cloud-bindings-1.7.1.jar
[INFO]     [creator]     unable to request https://repo.spring.io/release/org/springframework/cloud/spring-cloud-bindings/1.7.1/spring-cloud-bindings-1.7.1.jar
...

Old log:

...
[INFO]     [creator]     Paketo Spring Boot Buildpack 4.2.0
[INFO]     [creator]       https://github.com/paketo-buildpacks/spring-boot
[INFO]     [creator]       Creating slices from layers index
[INFO]     [creator]         dependencies
[INFO]     [creator]         spring-boot-loader
[INFO]     [creator]         snapshot-dependencies
[INFO]     [creator]         application
[INFO]     [creator]       Launch Helper: Reusing cached layer
[INFO]     [creator]       Spring Cloud Bindings 1.7.1: Reusing cached layer
[INFO]     [creator]       Web Application Type: Contributing to layer
[INFO]     [creator]         Servlet web application detected
[INFO]     [creator]         Writing env.launch/BPL_JVM_THREAD_COUNT.default
[INFO]     [creator]       4 application slices
[INFO]     [creator]       Image labels:
[INFO]     [creator]         org.opencontainers.image.title
[INFO]     [creator]         org.opencontainers.image.version
[INFO]     [creator]         org.springframework.boot.spring-configuration-metadata.json
[INFO]     [creator]         org.springframework.boot.version
...
[INFO]     [creator]     *** Images (aafb789b0498):
[INFO]     [creator]           docker.io/library/cpo-process-registry:1.0.0
[INFO] 
[INFO] Successfully built image 'docker.io/library/cpo-process-registry:1.0.0'