Skip to content

Commit 26f1a0f

Browse files
committed
Support system properties for Docker registries in Maven plugin
1 parent c6e47d0 commit 26f1a0f

File tree

4 files changed

+170
-41
lines changed

4 files changed

+170
-41
lines changed

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/docs/antora/modules/maven-plugin/examples/packaging-oci-image/docker-pom-authentication-command-line.xml

Lines changed: 0 additions & 22 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- tag::docker-registry-global-settings[] -->
3+
<profile>
4+
<id>spring-boot-docker-creds</id>
5+
<activation>
6+
<activeByDefault>true</activeByDefault>
7+
</activation>
8+
<properties>
9+
<spring-boot.build-image.builder>my-custom/image-builder:1.0.0</spring-boot.build-image.builder>
10+
<spring-boot.build-image.docker.builderRegistry.username>demo-user</spring-boot.build-image.docker.builderRegistry.username>
11+
<spring-boot.build-image.docker.builderRegistry.email>[email protected]</spring-boot.build-image.docker.builderRegistry.email>
12+
<spring-boot.build-image.docker.builderRegistry.password>demo-user-password</spring-boot.build-image.docker.builderRegistry.password>
13+
</properties>
14+
</profile>
15+
<!-- end::docker-registry-global-settings[] -->

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/docs/antora/modules/maven-plugin/pages/build-image.adoc

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,24 @@ The following table summarizes the available parameters for `docker.builderRegis
9797
|===
9898
| Parameter | Description
9999

100-
| `username`
100+
| `username` +
101+
(`spring-boot.build-image.docker.[builderRegistry \| publishRegistry].username`)
101102
| Username for the Docker image registry user. Required for user authentication.
102103

103-
| `password`
104+
| `password` +
105+
(`spring-boot.build-image.docker.[builderRegistry \| publishRegistry].password`)
104106
| Password for the Docker image registry user. Required for user authentication.
105107

106-
| `url`
108+
| `url` +
109+
(`spring-boot.build-image.docker.[builderRegistry \| publishRegistry].url`)
107110
| Address of the Docker image registry. Optional for user authentication.
108111

109-
| `email`
112+
| `email` +
113+
(`spring-boot.build-image.docker.[builderRegistry \| publishRegistry].email`)
110114
| E-mail address for the Docker image registry user. Optional for user authentication.
111115

112-
| `token`
116+
| `token` +
117+
(`spring-boot.build-image.docker.[builderRegistry \| publishRegistry].token`)
113118
| Identity token for the Docker image registry user. Required for token authentication.
114119
|===
115120

@@ -440,21 +445,13 @@ When using the `publish` option on the command line with authentication, you can
440445
[source,shell]
441446
----
442447
$ mvn spring-boot:build-image \
443-
-Ddocker.publishRegistry.username=user \
444-
-Ddocker.publishRegistry.password=secret \
445-
-Ddocker.publishRegistry.url=docker.example.com \
448+
-Dspring-boot.build-image.docker.publishRegistry.username=user \
449+
-Dspring-boot.build-image.docker.publishRegistry.password=secret \
450+
-Dspring-boot.build-image.docker.publishRegistry.url=docker.example.com \
446451
-Dspring-boot.build-image.publish=true \
447452
-Dspring-boot.build-image.imageName=docker.example.com/library/my-app:v1
448453
----
449454

450-
and reference the properties in the XML configuration:
451-
452-
[source,xml,indent=0,subs="verbatim,attributes"]
453-
----
454-
include::example$packaging-oci-image/docker-pom-authentication-command-line.xml[tags=docker]
455-
----
456-
457-
458455

459456
[[build-image.examples.caches]]
460457
=== Builder Cache and Workspace Configuration
@@ -555,3 +552,21 @@ If the builder or run image is stored in a private Docker registry that supports
555552
----
556553
include::example$packaging-oci-image/docker-token-authentication-pom.xml[tags=docker-token-authentication]
557554
----
555+
556+
In addition to configuring credentials directly in the POM you can set these same credentials as system properties.
557+
558+
[source,shell]
559+
----
560+
$ mvn spring-boot:build-image \
561+
-Dspring-boot.build-image.docker.builderRegistry.username=user \
562+
-Dspring-boot.build-image.docker.builderRegistry.password=secret \
563+
-Dspring-boot.build-image.docker.builderRegistry.url=docker.example.com \
564+
----
565+
566+
Another option to externalizing credentials is to set the credentials in your global Maven `settings.xml` file.
567+
568+
[source,xml,indent=0,subs="verbatim,attributes"]
569+
----
570+
include::example$packaging-oci-image/docker-registry-global-settings.xml[tags=docker-registry-global-settings]
571+
----
572+

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/BuildImageMojo.java

Lines changed: 124 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.springframework.boot.loader.tools.LayoutFactory;
5050
import org.springframework.boot.loader.tools.Libraries;
5151
import org.springframework.boot.loader.tools.LoaderImplementation;
52+
import org.springframework.boot.maven.Docker.DockerRegistry;
5253
import org.springframework.util.StringUtils;
5354

5455
/**
@@ -187,6 +188,86 @@ public abstract class BuildImageMojo extends AbstractPackagerMojo {
187188
@Parameter(property = "spring-boot.build-image.imagePlatform")
188189
String imagePlatform;
189190

191+
/**
192+
* Alias for the builder registry {@link DockerRegistry#username} to support
193+
* configuration through command-line property.
194+
* @since 3.5.0
195+
*/
196+
@Parameter(property = "spring-boot.build-image.docker.builderRegistry.username")
197+
String builderRegistryUsername;
198+
199+
/**
200+
* Alias for the builder registry {@link DockerRegistry#password} to support
201+
* configuration through command-line property.
202+
* @since 3.5.0
203+
*/
204+
@Parameter(property = "spring-boot.build-image.docker.builderRegistry.password")
205+
String builderRegistryPassword;
206+
207+
/**
208+
* Alias for the builder registry {@link DockerRegistry#username} to support
209+
* configuration through command-line property.
210+
* @since 3.5.0
211+
*/
212+
@Parameter(property = "spring-boot.build-image.docker.builderRegistry.email")
213+
String builderRegistryEmail;
214+
215+
/**
216+
* Alias for the builder registry {@link DockerRegistry#token} to support
217+
* configuration through command-line property.
218+
* @since 3.5.0
219+
*/
220+
@Parameter(property = "spring-boot.build-image.docker.builderRegistry.token")
221+
String builderRegistryToken;
222+
223+
/**
224+
* Alias for the builder registry {@link DockerRegistry#url} to support configuration
225+
* through command-line property.
226+
* @since 3.5.0
227+
*/
228+
@Parameter(property = "spring-boot.build-image.docker.builderRegistry.url")
229+
String builderRegistryUrl;
230+
231+
/**
232+
* Alias for the publish registry {@link DockerRegistry#username} to support
233+
* configuration through command-line property.
234+
* @since 3.5.0
235+
*/
236+
@Parameter(property = "spring-boot.build-image.docker.publishRegistry.username")
237+
String publishRegistryUsername;
238+
239+
/**
240+
* Alias for the publish registry {@link DockerRegistry#password} to support
241+
* configuration through command-line property.
242+
* @since 3.5.0
243+
*/
244+
@Parameter(property = "spring-boot.build-image.docker.publishRegistry.password")
245+
String publishRegistryPassword;
246+
247+
/**
248+
* Alias for the publish registry {@link DockerRegistry#username} to support
249+
* configuration through command-line property.
250+
* @since 3.5.0
251+
*/
252+
@Parameter(property = "spring-boot.build-image.docker.publishRegistry.email")
253+
String publishRegistryEmail;
254+
255+
/**
256+
* Alias for the publish registry {@link DockerRegistry#token} to support
257+
* configuration through command-line property.
258+
* @since 3.5.0
259+
*/
260+
@Parameter(property = "spring-boot.build-image.docker.publishRegistry.token")
261+
String publishRegistryToken;
262+
263+
/**
264+
* Alias for the publish registry {@link DockerRegistry#url} to support configuration
265+
* through command-line property.
266+
* @since 3.5.0
267+
*/
268+
@Parameter(property = "spring-boot.build-image.docker.publishRegistry.url")
269+
String publishRegistryUrl;
270+
190271
/**
191272
* Docker configuration options.
192273
* @since 2.4.0
@@ -262,9 +343,15 @@ private void buildImage() throws MojoExecutionException {
262343
Libraries libraries = getLibraries(Collections.emptySet());
263344
try {
264345
BuildRequest request = getBuildRequest(libraries);
265-
DockerConfiguration dockerConfiguration = (this.docker != null)
266-
? this.docker.asDockerConfiguration(request.isPublish())
267-
: new Docker().asDockerConfiguration(request.isPublish());
346+
if (this.docker == null) {
347+
this.docker = new Docker();
348+
}
349+
DockerRegistry builderRegistry = configureBuilderRegistry(this.docker.getBuilderRegistry());
350+
DockerRegistry publisherRegistry = configurePublishRegistry(this.docker.getPublishRegistry());
351+
this.docker.setBuilderRegistry(builderRegistry);
352+
this.docker.setPublishRegistry(publisherRegistry);
353+
354+
DockerConfiguration dockerConfiguration = this.docker.asDockerConfiguration(request.isPublish());
268355
Builder builder = new Builder(new MojoBuildLog(this::getLog), dockerConfiguration);
269356
builder.build(request);
270357
}
@@ -273,6 +360,40 @@ private void buildImage() throws MojoExecutionException {
273360
}
274361
}
275362

363+
private DockerRegistry configurePublishRegistry(DockerRegistry dockerRegistry) {
364+
if (dockerRegistry == null) {
365+
dockerRegistry = new DockerRegistry();
366+
}
367+
checkAndSetDockerRegistry(this.publishRegistryEmail, dockerRegistry.getEmail(), dockerRegistry::setEmail);
368+
checkAndSetDockerRegistry(this.publishRegistryPassword, dockerRegistry.getPassword(),
369+
dockerRegistry::setPassword);
370+
checkAndSetDockerRegistry(this.publishRegistryToken, dockerRegistry.getToken(), dockerRegistry::setToken);
371+
checkAndSetDockerRegistry(this.publishRegistryUrl, dockerRegistry.getUrl(), dockerRegistry::setUrl);
372+
checkAndSetDockerRegistry(this.publishRegistryUsername, dockerRegistry.getUsername(),
373+
dockerRegistry::setUsername);
374+
return dockerRegistry;
375+
}
376+
377+
private DockerRegistry configureBuilderRegistry(DockerRegistry dockerRegistry) {
378+
if (dockerRegistry == null) {
379+
dockerRegistry = new DockerRegistry();
380+
}
381+
checkAndSetDockerRegistry(this.builderRegistryEmail, dockerRegistry.getEmail(), dockerRegistry::setEmail);
382+
checkAndSetDockerRegistry(this.builderRegistryPassword, dockerRegistry.getPassword(),
383+
dockerRegistry::setPassword);
384+
checkAndSetDockerRegistry(this.builderRegistryToken, dockerRegistry.getToken(), dockerRegistry::setToken);
385+
checkAndSetDockerRegistry(this.builderRegistryUrl, dockerRegistry.getUrl(), dockerRegistry::setUrl);
386+
checkAndSetDockerRegistry(this.builderRegistryUsername, dockerRegistry.getUsername(),
387+
dockerRegistry::setUsername);
388+
return dockerRegistry;
389+
}
390+
391+
private void checkAndSetDockerRegistry(String value, String currentValue, Consumer<String> setter) {
392+
if (StringUtils.hasText(value) && !StringUtils.hasText(currentValue)) {
393+
setter.accept(value);
394+
}
395+
}
396+
276397
private BuildRequest getBuildRequest(Libraries libraries) {
277398
ImagePackager imagePackager = new ImagePackager(getArchiveFile(), getBackupFile());
278399
Function<Owner, TarArchive> content = (owner) -> getApplicationContent(owner, libraries, imagePackager);

0 commit comments

Comments
 (0)