Skip to content

Commit ed13910

Browse files
committed
Support system properties for Docker registries in Maven plguin
1 parent 414a751 commit ed13910

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

@@ -428,21 +433,13 @@ When using the `publish` option on the command line with authentication, you can
428433
[source,shell]
429434
----
430435
$ mvn spring-boot:build-image \
431-
-Ddocker.publishRegistry.username=user \
432-
-Ddocker.publishRegistry.password=secret \
433-
-Ddocker.publishRegistry.url=docker.example.com \
436+
-Dspring-boot.build-image.docker.publishRegistry.username=user \
437+
-Dspring-boot.build-image.docker.publishRegistry.password=secret \
438+
-Dspring-boot.build-image.docker.publishRegistry.url=docker.example.com \
434439
-Dspring-boot.build-image.publish=true \
435440
-Dspring-boot.build-image.imageName=docker.example.com/library/my-app:v1
436441
----
437442

438-
and reference the properties in the XML configuration:
439-
440-
[source,xml,indent=0,subs="verbatim,attributes"]
441-
----
442-
include::example$packaging-oci-image/docker-pom-authentication-command-line.xml[tags=docker]
443-
----
444-
445-
446443

447444
[[build-image.examples.caches]]
448445
=== Builder Cache and Workspace Configuration
@@ -543,3 +540,21 @@ If the builder or run image is stored in a private Docker registry that supports
543540
----
544541
include::example$packaging-oci-image/docker-token-authentication-pom.xml[tags=docker-token-authentication]
545542
----
543+
544+
In addition to configuring credentials directly in the POM you can set these same credentials as system properties.
545+
546+
[source,shell]
547+
----
548+
$ mvn spring-boot:build-image \
549+
-Dspring-boot.build-image.docker.builderRegistry.username=user \
550+
-Dspring-boot.build-image.docker.builderRegistry.password=secret \
551+
-Dspring-boot.build-image.docker.builderRegistry.url=docker.example.com \
552+
----
553+
554+
Another option to externalizing credentials is to set the credentials in your global Maven `settings.xml` file.
555+
556+
[source,xml,indent=0,subs="verbatim,attributes"]
557+
----
558+
include::example$packaging-oci-image/docker-registry-global-settings.xml[tags=docker-registry-global-settings]
559+
----
560+

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
/**
@@ -172,6 +173,86 @@ public abstract class BuildImageMojo extends AbstractPackagerMojo {
172173
@Parameter(property = "spring-boot.build-image.applicationDirectory", readonly = true)
173174
String applicationDirectory;
174175

176+
/**
177+
* Alias for the builder registry {@link DockerRegistry#username} to support
178+
* configuration through command-line property
179+
* @since 3.3.11
180+
*/
181+
@Parameter(property = "spring-boot.build-image.docker.builderRegistry.username", readonly = true)
182+
String builderRegistryUsername;
183+
184+
/**
185+
* Alias for the builder registry {@link DockerRegistry#password} to support
186+
* configuration through command-line property
187+
* @since 3.3.11
188+
*/
189+
@Parameter(property = "spring-boot.build-image.docker.builderRegistry.password", readonly = true)
190+
String builderRegistryPassword;
191+
192+
/**
193+
* Alias for the builder registry {@link DockerRegistry#username} to support
194+
* configuration through command-line property
195+
* @since 3.3.11
196+
*/
197+
@Parameter(property = "spring-boot.build-image.docker.builderRegistry.email", readonly = true)
198+
String builderRegistryEmail;
199+
200+
/**
201+
* Alias for the builder registry {@link DockerRegistry#token} to support
202+
* configuration through command-line property
203+
* @since 3.3.11
204+
*/
205+
@Parameter(property = "spring-boot.build-image.docker.builderRegistry.token", readonly = true)
206+
String builderRegistryToken;
207+
208+
/**
209+
* Alias for the builder registry {@link DockerRegistry#url} to support configuration
210+
* through command-line property
211+
* @since 3.3.11
212+
*/
213+
@Parameter(property = "spring-boot.build-image.docker.builderRegistry.url", readonly = true)
214+
String builderRegistryUrl;
215+
216+
/**
217+
* Alias for the publish registry {@link DockerRegistry#username} to support
218+
* configuration through command-line property
219+
* @since 3.3.11
220+
*/
221+
@Parameter(property = "spring-boot.build-image.docker.publishRegistry.username", readonly = true)
222+
String publishRegistryUsername;
223+
224+
/**
225+
* Alias for the publish registry {@link DockerRegistry#password} to support
226+
* configuration through command-line property
227+
* @since 3.3.11
228+
*/
229+
@Parameter(property = "spring-boot.build-image.docker.publishRegistry.password", readonly = true)
230+
String publishRegistryPassword;
231+
232+
/**
233+
* Alias for the publish registry {@link DockerRegistry#username} to support
234+
* configuration through command-line property
235+
* @since 3.3.11
236+
*/
237+
@Parameter(property = "spring-boot.build-image.docker.publishRegistry.email", readonly = true)
238+
String publishRegistryEmail;
239+
240+
/**
241+
* Alias for the publish registry {@link DockerRegistry#token} to support
242+
* configuration through command-line property
243+
* @since 3.3.11
244+
*/
245+
@Parameter(property = "spring-boot.build-image.docker.publishRegistry.token", readonly = true)
246+
String publishRegistryToken;
247+
248+
/**
249+
* Alias for the publish registry {@link DockerRegistry#url} to support configuration
250+
* through command-line property
251+
* @since 3.3.11
252+
*/
253+
@Parameter(property = "spring-boot.build-image.docker.publishRegistry.url", readonly = true)
254+
String publishRegistryUrl;
255+
175256
/**
176257
* Docker configuration options.
177258
* @since 2.4.0
@@ -247,9 +328,15 @@ private void buildImage() throws MojoExecutionException {
247328
Libraries libraries = getLibraries(Collections.emptySet());
248329
try {
249330
BuildRequest request = getBuildRequest(libraries);
250-
DockerConfiguration dockerConfiguration = (this.docker != null)
251-
? this.docker.asDockerConfiguration(request.isPublish())
252-
: new Docker().asDockerConfiguration(request.isPublish());
331+
if (this.docker == null) {
332+
this.docker = new Docker();
333+
}
334+
DockerRegistry builderRegistry = configureBuilderRegistry(this.docker.getBuilderRegistry());
335+
DockerRegistry publisherRegistry = configurePublishRegistry(this.docker.getPublishRegistry());
336+
this.docker.setBuilderRegistry(builderRegistry);
337+
this.docker.setPublishRegistry(publisherRegistry);
338+
339+
DockerConfiguration dockerConfiguration = this.docker.asDockerConfiguration(request.isPublish());
253340
Builder builder = new Builder(new MojoBuildLog(this::getLog), dockerConfiguration);
254341
builder.build(request);
255342
}
@@ -258,6 +345,40 @@ private void buildImage() throws MojoExecutionException {
258345
}
259346
}
260347

348+
private DockerRegistry configurePublishRegistry(DockerRegistry dockerRegistry) {
349+
if (dockerRegistry == null) {
350+
dockerRegistry = new DockerRegistry();
351+
}
352+
checkAndSetDockerRegistry(this.publishRegistryEmail, dockerRegistry.getEmail(), dockerRegistry::setEmail);
353+
checkAndSetDockerRegistry(this.publishRegistryPassword, dockerRegistry.getPassword(),
354+
dockerRegistry::setPassword);
355+
checkAndSetDockerRegistry(this.publishRegistryToken, dockerRegistry.getToken(), dockerRegistry::setToken);
356+
checkAndSetDockerRegistry(this.publishRegistryUrl, dockerRegistry.getUrl(), dockerRegistry::setUrl);
357+
checkAndSetDockerRegistry(this.publishRegistryUsername, dockerRegistry.getUsername(),
358+
dockerRegistry::setUsername);
359+
return dockerRegistry;
360+
}
361+
362+
private DockerRegistry configureBuilderRegistry(DockerRegistry dockerRegistry) {
363+
if (dockerRegistry == null) {
364+
dockerRegistry = new DockerRegistry();
365+
}
366+
checkAndSetDockerRegistry(this.builderRegistryEmail, dockerRegistry.getEmail(), dockerRegistry::setEmail);
367+
checkAndSetDockerRegistry(this.builderRegistryPassword, dockerRegistry.getPassword(),
368+
dockerRegistry::setPassword);
369+
checkAndSetDockerRegistry(this.builderRegistryToken, dockerRegistry.getToken(), dockerRegistry::setToken);
370+
checkAndSetDockerRegistry(this.builderRegistryUrl, dockerRegistry.getUrl(), dockerRegistry::setUrl);
371+
checkAndSetDockerRegistry(this.builderRegistryUsername, dockerRegistry.getUsername(),
372+
dockerRegistry::setUsername);
373+
return dockerRegistry;
374+
}
375+
376+
private void checkAndSetDockerRegistry(String value, String currentValue, Consumer<String> setter) {
377+
if (StringUtils.hasText(value) && !StringUtils.hasText(currentValue)) {
378+
setter.accept(value);
379+
}
380+
}
381+
261382
private BuildRequest getBuildRequest(Libraries libraries) {
262383
ImagePackager imagePackager = new ImagePackager(getArchiveFile(), getBackupFile());
263384
Function<Owner, TarArchive> content = (owner) -> getApplicationContent(owner, libraries, imagePackager);

0 commit comments

Comments
 (0)