'Pass dockerPublisher maven properties over command line in spring-boot:build-image

I want to integrate the spring boot maven plugins capability to build and publish an OCI Image to a remote Repository

My Goal

I want to use the following plugin configuration:

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <configuration>
    <image>
      <name>${docker.image.prefix}/${project.artifactId}:${project.version}</name>
    </image>
  </configuration>
  <executions>
    <execution>
      <goals>
        <goal>build-image</goal>
      </goals>
    </execution>
  </executions>
</plugin>

And now I want to pass the docker.publishRegistry variables by command line.

What I've tried so far

I've tried to pass the parameter with the -Ddocker.publishRegistry.username property but that didn't work.

When you take a look at the source code of the plugin Docker has no Parameter property assigned to it:

/**
 * Alias for {@link Image#publish} to support configuration via command-line property.
 */
@Parameter(property = "spring-boot.build-image.publish", readonly = true)
Boolean publish;

/**
 * Docker configuration options.
 * @since 2.4.0
 */
@Parameter
private Docker docker;

https://github.com/spring-projects/spring-boot/blob/82b90d57496ba85be316b9eb88a36d81f2cc9baa/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/BuildImageMojo.java#L159

So I guess it is not possible to define this parameter by command line or is it?

Current Workaround

Currently I'm defining the properties by global maven properties and reuse them in the docker scope. My pom.xml:

<properties>
  <docker-registry>https://example.org</docker-registry>
  <docker-registry-username>username</docker-registry-username>
  <docker-registry-username>password</docker-registry-username>
</properties>
<!-- ... -->
<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <configuration>
    <image>
      <name>${docker.image.prefix}/${project.artifactId}:${project.version}</name>
    </image>
    <docker>
      <publishRegistry>
        <username>${docker-registry-username}</username>
        <password>${docker-registry-password}</password>
        <url>${docker-registry}</url>
      </publishRegistry>
    </docker>
  </configuration>
  <executions>
    <execution>
      <goals>
        <goal>build-image</goal>
      </goals>
    </execution>
  </executions>
</plugin>

And I'm building with:

./mvnw -B -s \
  -Dspring-boot.build-image.publish=true \
  -Ddocker-registry-username="$USERNAME" \
  -Ddocker-registry-password="$PASSWORD" \
  -Ddocker-registry="$REGISTRY" \
  clean deploy


Solution 1:[1]

I have not the exact solution to you question: "passing publishRegistry parameters on the command line", but If I may, I have another workaround that shields you from exposing your credential in the pom.xml.

What i have done is to put the parameters and credential in a profile in my .m2/settings.xml like this:

    <profiles>
        <profile>
            <id>docker-io-credentials</id>
            <properties>
                <docker-reg>docker.io</docker-reg>
                <docker-reg.user>your-user-name</docker-reg.user>
                <docker-reg.pwd>your-token-or-passwd</docker-reg.pwd>
                <docker-reg.url>${docker-reg}/library/${docker-reg.user}</docker-reg.url>
            </properties>
        </profile>
    </profiles>

Then on the command-line you can simply pass the profile's name to merge the credential to the current build.

mvn clean install -Pdocker-io-credentials

Solution 2:[2]

You can define placeholders in the spring-boot plugin's configuration, which refer to environment variables. This will be slightly less complex, so like

...
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>main.Class</mainClass>
                <image>
                    <name>registry-url/library/image-name:${project.version}</name>
                </image>
                
                <docker>
                    <publishRegistry>
                        <username>docker-user</username>
                        <password>${env.docker_registry_password}</password>
                        <url>https://registry-url/v1/</url>
                        <email>[email protected]</email>
                    </publishRegistry>
                </docker>
            </configuration>
...

See more on this topic here: https://www.baeldung.com/maven-env-variables

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Zartc
Solution 2 ltuska