在 spring-boot:build-image 中通过命令行传递 dockerPublisher maven 属性

Sta*_*son 7 maven spring-boot spring-boot-maven-plugin

我想集成 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>
Run Code Online (Sandbox Code Playgroud)

现在我想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;
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

我正在构建:

./mvnw -B -s \
  -Dspring-boot.build-image.publish=true \
  -Ddocker-registry-username="$USERNAME" \
  -Ddocker-registry-password="$PASSWORD" \
  -Ddocker-registry="$REGISTRY" \
  clean deploy
Run Code Online (Sandbox Code Playgroud)

Zar*_*rtc 2

对于您的问题,我没有确切的解决方案:“在命令行上传递publishRegistry参数”,但如果可以的话,我有另一种解决方法,可以防止您在 pom.xml 中暴露您的凭据。

我所做的是将参数和凭据放入我的配置文件中,.m2/settings.xml如下所示:

    <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>
Run Code Online (Sandbox Code Playgroud)

然后,在命令行上,您只需传递配置文件的名称即可将凭据合并到当前版本。

mvn clean install -Pdocker-io-credentials
Run Code Online (Sandbox Code Playgroud)