首页 > 解决方案 > 在 spring-boot:build-image 中通过命令行传递 dockerPublisher maven 属性

问题描述

我想集成 Spring Boot Maven 插件功能来构建 OCI 映像并将其发布到远程存储库

我的目标

我想使用以下插件配置:

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

现在我想docker.publishRegistry通过命令行传递变量。

到目前为止我尝试过的

我试图通过-Ddocker.publishRegistry.username属性传递参数,但没有奏效。

当您查看插件的源代码时,Docker没有分配给它的 Parameter 属性:

/**
 * 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/启动/maven/BuildImageMojo.java#L159

所以我想不可能通过命令行定义这个参数,或者是吗?

当前的解决方法

目前我正在通过全局 Maven 属性定义属性并在docker范围内重用它们。我的 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>

我正在构建:

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

标签: spring-bootmavenspring-boot-maven-plugin

解决方案


您可以在 spring-boot 插件的配置中定义占位符,这些占位符引用环境变量。这会稍微不那么复杂,所以像

...
        <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>user@example.com</email>
                    </publishRegistry>
                </docker>
            </configuration>
...

在此处查看有关此主题的更多信息:https ://www.baeldung.com/maven-env-variables


推荐阅读