spring-boot - 在 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;
所以我想不可能通过命令行定义这个参数,或者是吗?
当前的解决方法
目前我正在通过全局 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-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
推荐阅读
- python - 添加字母不起作用 - 请让我知道我哪里出错了
- python - 如何使用python计算特定目录中的LOG文件数
- xamarin.ios - 在 iOS 中使用滑块应用图像效果(亮度)时应用程序崩溃
- lightgbm - Lightgbm:返回leaf_index的意图
- oauth - 更改 Gmail 登录凭据以后会影响 Gmail API credentials.json 文件吗?
- python - 如何为列表中的每个元素分配一个整数
- python - 在 R Reticulate 中安装 Github Python 包
- unity3d - 什么是 AR 应用程序的多边形数量限制?
- android - 如果模式设置为时间,则 DateTimePicker 不会在 android 中为 react-native 应用程序打开
- javascript - 我有 2 个状态,但即使我只 setState 其中一个,它们都在更新