diff --git a/doc/changelog.md b/doc/changelog.md index db192ea8c..16ee2fbd6 100644 --- a/doc/changelog.md +++ b/doc/changelog.md @@ -3,6 +3,7 @@ * **0.29-SNAPSHOT** * **0.29.0** (2019-04-08) + - Avoid failing docker:save when no images with build configuration are present ([#1185](https://github.com/fabric8io/docker-maven-plugin/issues/1185)) - Reintroduce minimal API-VERSION parameter in order to support docker versions below apiVersion 1.25 - docs: Correct default image naming - Proxy settings are being ignored ([#1148](https://github.com/fabric8io/docker-maven-plugin/issues/1148)) diff --git a/src/main/java/io/fabric8/maven/docker/SaveMojo.java b/src/main/java/io/fabric8/maven/docker/SaveMojo.java index 7a89c2b2e..d3b6d8dda 100644 --- a/src/main/java/io/fabric8/maven/docker/SaveMojo.java +++ b/src/main/java/io/fabric8/maven/docker/SaveMojo.java @@ -43,7 +43,27 @@ protected void executeInternal(ServiceHub serviceHub) throws DockerAccessExcepti if (skipSave) { return; } - String imageName = getImageName(); + + List images = getResolvedImages(); + List buildImages = getImagesWithBuildConfig(images); + String imageName; + + if (saveName == null && saveAlias == null) { + if (buildImages.isEmpty()) { + log.info("No images have a build configuration defined: save skipped"); + return; + } + if (buildImages.size() == 1) { + imageName = buildImages.get(0).getName(); + } else { + throw new MojoExecutionException("More than one image with build configuration is defined. Please specify the image with 'docker.name' or 'docker.alias'."); + } + } else if (saveName != null && saveAlias != null) { + throw new MojoExecutionException("Cannot specify both name and alias."); + } else { + imageName = getImageName(images); + } + String fileName = getFileName(imageName); ensureSaveDir(fileName); log.info("Saving image %s to %s", imageName, fileName); @@ -52,7 +72,6 @@ protected void executeInternal(ServiceHub serviceHub) throws DockerAccessExcepti } serviceHub.getDockerAccess().saveImage(imageName, fileName, ArchiveCompression.fromFileName(fileName)); - } private String getFileName(String iName) throws MojoExecutionException { @@ -96,19 +115,7 @@ private void ensureSaveDir(String fileName) throws MojoExecutionException { } } - private String getImageName() throws MojoExecutionException { - List images = getResolvedImages(); - // specify image by name or alias - if (saveName == null && saveAlias == null) { - List buildImages = getImagesWithBuildConfig(images); - if (buildImages.size() == 1) { - return buildImages.get(0).getName(); - } - throw new MojoExecutionException("More than one image with build configuration is defined. Please specify the image with 'docker.name' or 'docker.alias'."); - } - if (saveName != null && saveAlias != null) { - throw new MojoExecutionException("Cannot specify both name and alias."); - } + private String getImageName(List images) throws MojoExecutionException { for (ImageConfiguration ic : images) { if (equalName(ic) || equalAlias(ic)) { return ic.getName(); diff --git a/src/test/java/io/fabric8/maven/docker/SaveMojoTest.java b/src/test/java/io/fabric8/maven/docker/SaveMojoTest.java new file mode 100644 index 000000000..aa3b483e5 --- /dev/null +++ b/src/test/java/io/fabric8/maven/docker/SaveMojoTest.java @@ -0,0 +1,81 @@ +package io.fabric8.maven.docker; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import org.apache.maven.plugin.MojoExecutionException; +import org.junit.Test; + +import io.fabric8.maven.docker.access.DockerAccessException; +import io.fabric8.maven.docker.config.BuildImageConfiguration; +import io.fabric8.maven.docker.config.ImageConfiguration; +import io.fabric8.maven.docker.service.ServiceHub; +import io.fabric8.maven.docker.util.Logger; +import mockit.Deencapsulation; +import mockit.Injectable; +import mockit.Mocked; +import mockit.Tested; + +public class SaveMojoTest { + + @Injectable + Logger log; + + @Tested(fullyInitialized = false) + private SaveMojo saveMojo; + + @Mocked + ServiceHub serviceHub; + + @Test + public void noFailureWithEmptyImageList() throws DockerAccessException, MojoExecutionException { + Deencapsulation.setField(saveMojo, "images", Collections.emptyList()); + Deencapsulation.setField(saveMojo, "resolvedImages", Collections.emptyList()); + + saveMojo.executeInternal(serviceHub); + } + + @Test + public void noFailureWithEmptyBuildImageList() throws DockerAccessException, MojoExecutionException { + ImageConfiguration image = new ImageConfiguration.Builder() + .name("example:latest") + .build(); + Deencapsulation.setField(saveMojo, "images", Collections.singletonList(image)); + Deencapsulation.setField(saveMojo, "resolvedImages", Collections.singletonList(image)); + + saveMojo.executeInternal(serviceHub); + } + + @Test(expected = MojoExecutionException.class) + public void failureWithMultipleBuildImageList() throws DockerAccessException, MojoExecutionException { + ImageConfiguration image1 = new ImageConfiguration.Builder() + .name("example1:latest") + .buildConfig(new BuildImageConfiguration.Builder() + .from("scratch") + .build()) + .build(); + ImageConfiguration image2 = new ImageConfiguration.Builder() + .name("example2:latest") + .buildConfig(new BuildImageConfiguration.Builder() + .from("scratch") + .build()) + .build(); + + List images = Arrays.asList(image1, image2); + Deencapsulation.setField(saveMojo, "images", images); + Deencapsulation.setField(saveMojo, "resolvedImages", images); + + saveMojo.executeInternal(serviceHub); + } + + @Test(expected = MojoExecutionException.class) + public void failureWithSaveAliasAndName() throws DockerAccessException, MojoExecutionException { + Deencapsulation.setField(saveMojo, "saveAlias", "not-null"); + Deencapsulation.setField(saveMojo, "saveName", "not-null"); + Deencapsulation.setField(saveMojo, "images", Collections.singletonList(new ImageConfiguration())); + Deencapsulation.setField(saveMojo, "resolvedImages", Collections.singletonList(new ImageConfiguration())); + + saveMojo.executeInternal(serviceHub); + } +}