Skip to content

Commit

Permalink
PR #1210 Remove saveAttach and just use saveClassifier.
Browse files Browse the repository at this point in the history
Signed-off-by: William Rose <william.rose@nuance.com>
  • Loading branch information
wrosenuance committed Apr 18, 2019
1 parent 9e2bf69 commit bbf7773
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 23 deletions.
31 changes: 26 additions & 5 deletions src/main/asciidoc/inc/_docker-save.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,31 @@ If the option `saveFile` is not set, the file name is calculated automatically:

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

.Controlling image compression
The file name extension is used to select a compression method for the output.
[cols="3,2,1"]
|===
| Extensions | Compression | Type

| .tar or unrecognized
| No compression
| .tar

| .tar.gz, .tgz
| GZIP compression
| .tar.gz

| .tar.bz, .tar.bz2, .tar.bzip2
| BZIP2 compression
| .tar.bz

|===

.Attaching the saved image as an artifact
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.

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>>.

.Save options
[cols="1,5,1"]
|===
Expand All @@ -27,12 +52,8 @@ Please note that the exported image contains all image layers and can be quite l
| The filename to save.
| `docker.save.file` or `docker.file` or `file`

| *saveAttach*
| Whether to attach the saved archive to the project (`false` by default).
| `docker.save.attach`

| *saveClassifier*
| Sets the artifact classifier when attaching the saved archive to the project. If `saveAlias` is set, defaults to `archive-<saveAlias>`, otherwise defaults to `archive`. Note that using overriding the default to use `docker` or `docker-<saveAlias>` may lead to a conflict if a source archive is also attached with <<{plugin}:source>>.
| 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.
| `docker.save.classifier`

| *skipSave*
Expand Down
27 changes: 13 additions & 14 deletions src/main/java/io/fabric8/maven/docker/SaveMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ public class SaveMojo extends AbstractDockerMojo {
@Parameter(property = "docker.skip.save", defaultValue = "false")
private boolean skipSave;

@Parameter(property = "docker.save.attach", defaultValue = "false")
private boolean saveAttach;

@Parameter(property = "docker.save.classifier")
private String saveClassifier;

Expand All @@ -53,7 +50,8 @@ protected void executeInternal(ServiceHub serviceHub) throws DockerAccessExcepti
return;
}

String imageName = getImageName(images);
ImageConfiguration image = getImageToSave(images);
String imageName = image.getName();
String fileName = getFileName(imageName);
ensureSaveDir(fileName);
log.info("Saving image %s to %s", imageName, fileName);
Expand All @@ -66,8 +64,9 @@ protected void executeInternal(ServiceHub serviceHub) throws DockerAccessExcepti
serviceHub.getDockerAccess().saveImage(imageName, fileName, compression);
log.info("%s: Saved image to %s in %s", imageName, fileName, EnvUtil.formatDurationTill(time));

if(saveAttach) {
projectHelper.attachArtifact(project, compression.getFileSuffix(), getClassifier(), new File(fileName));
String classifier = getClassifier(image);
if(classifier != null) {
projectHelper.attachArtifact(project, compression.getFileSuffix(), classifier, new File(fileName));
}
}

Expand Down Expand Up @@ -128,12 +127,12 @@ private void ensureSaveDir(String fileName) throws MojoExecutionException {
}
}

private String getImageName(List<ImageConfiguration> images) throws MojoExecutionException {
private ImageConfiguration getImageToSave(List<ImageConfiguration> images) throws MojoExecutionException {
// specify image by name or alias
if (saveName == null && saveAlias == null) {
List<ImageConfiguration> buildImages = getImagesWithBuildConfig(images);
if (buildImages.size() == 1) {
return buildImages.get(0).getName();
return buildImages.get(0);
}
throw new MojoExecutionException("More than one image with build configuration is defined. Please specify the image with 'docker.name' or 'docker.alias'.");
}
Expand All @@ -142,7 +141,7 @@ private String getImageName(List<ImageConfiguration> images) throws MojoExecutio
}
for (ImageConfiguration ic : images) {
if (equalName(ic) || equalAlias(ic)) {
return ic.getName();
return ic;
}
}
throw new MojoExecutionException(saveName != null ?
Expand All @@ -160,12 +159,12 @@ private List<ImageConfiguration> getImagesWithBuildConfig(List<ImageConfiguratio
return ret;
}

private String getClassifier() {
if(saveClassifier == null) {
return saveAlias == null ? "archive" : "archive-" + saveAlias;
} else {
return saveClassifier;
private String getClassifier(ImageConfiguration image) {
if(saveClassifier == null || saveClassifier.length() == 0) {
return null;
}

return saveClassifier.replace("%a", image.getAlias() == null ? "" : image.getAlias());
}


Expand Down
7 changes: 3 additions & 4 deletions src/test/java/io/fabric8/maven/docker/SaveMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void saveAndAttachWithoutNameAliasOrFile() throws DockerAccessException,

Deencapsulation.setField(saveMojo, "images", Collections.singletonList(image));
Deencapsulation.setField(saveMojo, "resolvedImages", Collections.singletonList(image));
Deencapsulation.setField(saveMojo, "saveAttach", true);
Deencapsulation.setField(saveMojo, "saveClassifier", "archive");
Deencapsulation.setField(saveMojo, "project", mavenProject);
Deencapsulation.setField(saveMojo, "projectHelper", mavenProjectHelper);

Expand Down Expand Up @@ -153,7 +153,7 @@ public void saveAndAttachWithFile() throws DockerAccessException, MojoExecutionE
Deencapsulation.setField(saveMojo, "images", Collections.singletonList(image));
Deencapsulation.setField(saveMojo, "resolvedImages", Collections.singletonList(image));
Deencapsulation.setField(saveMojo, "saveFile", "destination/archive-name.tar.bz2");
Deencapsulation.setField(saveMojo, "saveAttach", true);
Deencapsulation.setField(saveMojo, "saveClassifier", "archive");
Deencapsulation.setField(saveMojo, "project", mavenProject);
Deencapsulation.setField(saveMojo, "projectHelper", mavenProjectHelper);

Expand Down Expand Up @@ -231,7 +231,7 @@ public void saveAndAttachWithAlias() throws DockerAccessException, MojoExecution
Deencapsulation.setField(saveMojo, "images", Arrays.asList(image1, image2));
Deencapsulation.setField(saveMojo, "resolvedImages", Arrays.asList(image1, image2));
Deencapsulation.setField(saveMojo, "saveAlias", "example2");
Deencapsulation.setField(saveMojo, "saveAttach", true);
Deencapsulation.setField(saveMojo, "saveClassifier", "archive-%a");
Deencapsulation.setField(saveMojo, "project", mavenProject);
Deencapsulation.setField(saveMojo, "projectHelper", mavenProjectHelper);

Expand Down Expand Up @@ -272,7 +272,6 @@ public void saveAndAttachWithAliasButAlsoClassifier() throws DockerAccessExcepti
Deencapsulation.setField(saveMojo, "resolvedImages", Arrays.asList(image1, image2));
Deencapsulation.setField(saveMojo, "saveAlias", "example2");
Deencapsulation.setField(saveMojo, "saveClassifier", "preferred");
Deencapsulation.setField(saveMojo, "saveAttach", true);
Deencapsulation.setField(saveMojo, "project", mavenProject);
Deencapsulation.setField(saveMojo, "projectHelper", mavenProjectHelper);

Expand Down

0 comments on commit bbf7773

Please sign in to comment.