Skip to content

Commit 885954a

Browse files
wrosenuancerhuss
authored andcommitted
Add the ability to attach a saved image archive as a project artifact (#1210)
This PR is to add the ability to attach an image archive produced by docker:save to the project as an artifact. Currently this is not possible and archives have to be attached using something like build-helper-maven-plugin. To avoid conflict with docker:source, the default attach classifier is `archive` (or `archive-<saveAlias>`), instead of `docker` (or `docker-<alias>`) as used by that mojo.
1 parent 86034f7 commit 885954a

File tree

4 files changed

+304
-5
lines changed

4 files changed

+304
-5
lines changed

doc/changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- Restore ANSI color to Maven logging if disabled during plugin execution and enable color for Windows with Maven 3.5.0 or later. Color logging is enabled by default, but disabled if the Maven CLI disables color (e.g. in batch mode) ([#1108](https://github.com/fabric8io/docker-maven-plugin/issues/1108))
55
- Fix NPE if docker:save is called with -Dfile=file-name-only.tar ([#1203](https://github.com/fabric8io/docker-maven-plugin/issues/1203))
66
- Improve GZIP compression performance for docker:save ([#1205](https://github.com/fabric8io/docker-maven-plugin/issues/1205))
7+
- Allow docker:save to attach image archive as a project artifact ([#1210](https://github.com/fabric8io/docker-maven-plugin/pull/1210))
78
- Use pattern to detect image name in archive loaded during build and tag with image name from the project configuration ([#1207](https://github.com/fabric8io/docker-maven-plugin/issues/1207))
89

910
* **0.29.0** (2019-04-08)

src/main/asciidoc/inc/_docker-save.adoc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,31 @@ If the option `saveFile` is not set, the file name is calculated automatically:
1010

1111
Please note that the exported image contains all image layers and can be quite large (also, it takes a bit to export the image).
1212

13+
.Controlling image compression
14+
The file name extension is used to select a compression method for the output.
15+
[cols="3,2,1"]
16+
|===
17+
| Extensions | Compression | Type
18+
19+
| .tar or unrecognized
20+
| No compression
21+
| .tar
22+
23+
| .tar.gz, .tgz
24+
| GZIP compression
25+
| .tar.gz
26+
27+
| .tar.bz, .tar.bz2, .tar.bzip2
28+
| BZIP2 compression
29+
| .tar.bz
30+
31+
|===
32+
33+
.Attaching the saved image as an artifact
34+
If `saveClassifier` is set, the saved archive will be attached to the project using the provided classifier and the type determined from the file name. The placeholder `%a` will be replaced with the image alias.
35+
36+
Note that using overriding the default to use `docker` or `docker-%a` may lead to a conflict if a source archive is also attached with <<{plugin}:source>>.
37+
1338
.Save options
1439
[cols="1,5,1"]
1540
|===
@@ -27,6 +52,10 @@ Please note that the exported image contains all image layers and can be quite l
2752
| The filename to save.
2853
| `docker.save.file` or `docker.file` or `file`
2954

55+
| *saveClassifier*
56+
| If set, attach the the saved archive to the project with the provided classifier. A placeholder of `%a` will be replaced with the image alias.
57+
| `docker.save.classifier`
58+
3059
| *skipSave*
3160
| A boolean flag whether to skip execution of the goal.
3261
| `docker.skip.save`

src/main/java/io/fabric8/maven/docker/SaveMojo.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public class SaveMojo extends AbstractDockerMojo {
3939
@Parameter(property = "docker.skip.save", defaultValue = "false")
4040
private boolean skipSave;
4141

42+
@Parameter(property = "docker.save.classifier")
43+
private String saveClassifier;
44+
4245
@Override
4346
protected void executeInternal(ServiceHub serviceHub) throws DockerAccessException, MojoExecutionException {
4447

@@ -47,7 +50,8 @@ protected void executeInternal(ServiceHub serviceHub) throws DockerAccessExcepti
4750
return;
4851
}
4952

50-
String imageName = getImageName(images);
53+
ImageConfiguration image = getImageToSave(images);
54+
String imageName = image.getName();
5155
String fileName = getFileName(imageName);
5256
ensureSaveDir(fileName);
5357
log.info("Saving image %s to %s", imageName, fileName);
@@ -56,8 +60,14 @@ protected void executeInternal(ServiceHub serviceHub) throws DockerAccessExcepti
5660
}
5761

5862
long time = System.currentTimeMillis();
59-
serviceHub.getDockerAccess().saveImage(imageName, fileName, ArchiveCompression.fromFileName(fileName));
63+
ArchiveCompression compression = ArchiveCompression.fromFileName(fileName);
64+
serviceHub.getDockerAccess().saveImage(imageName, fileName, compression);
6065
log.info("%s: Saved image to %s in %s", imageName, fileName, EnvUtil.formatDurationTill(time));
66+
67+
String classifier = getClassifier(image);
68+
if(classifier != null) {
69+
projectHelper.attachArtifact(project, compression.getFileSuffix(), classifier, new File(fileName));
70+
}
6171
}
6272

6373
private boolean skipSaveFor(List<ImageConfiguration> images) {
@@ -117,12 +127,12 @@ private void ensureSaveDir(String fileName) throws MojoExecutionException {
117127
}
118128
}
119129

120-
private String getImageName(List<ImageConfiguration> images) throws MojoExecutionException {
130+
private ImageConfiguration getImageToSave(List<ImageConfiguration> images) throws MojoExecutionException {
121131
// specify image by name or alias
122132
if (saveName == null && saveAlias == null) {
123133
List<ImageConfiguration> buildImages = getImagesWithBuildConfig(images);
124134
if (buildImages.size() == 1) {
125-
return buildImages.get(0).getName();
135+
return buildImages.get(0);
126136
}
127137
throw new MojoExecutionException("More than one image with build configuration is defined. Please specify the image with 'docker.name' or 'docker.alias'.");
128138
}
@@ -131,7 +141,7 @@ private String getImageName(List<ImageConfiguration> images) throws MojoExecutio
131141
}
132142
for (ImageConfiguration ic : images) {
133143
if (equalName(ic) || equalAlias(ic)) {
134-
return ic.getName();
144+
return ic;
135145
}
136146
}
137147
throw new MojoExecutionException(saveName != null ?
@@ -149,6 +159,15 @@ private List<ImageConfiguration> getImagesWithBuildConfig(List<ImageConfiguratio
149159
return ret;
150160
}
151161

162+
private String getClassifier(ImageConfiguration image) {
163+
if(saveClassifier == null || saveClassifier.length() == 0) {
164+
return null;
165+
}
166+
167+
return saveClassifier.replace("%a", image.getAlias() == null ? "" : image.getAlias());
168+
}
169+
170+
152171
private boolean equalAlias(ImageConfiguration ic) {
153172
return saveAlias != null && saveAlias.equals(ic.getAlias());
154173
}

0 commit comments

Comments
 (0)