From 49dedf7b0bc6b798f3cb96566a4bbba21173df42 Mon Sep 17 00:00:00 2001 From: John W Date: Fri, 2 Sep 2016 17:26:09 +0100 Subject: [PATCH] Add ability to send --rm param on build (seems needed for CircleCI) --- .../java/com/spotify/docker/BuildMojo.java | 7 +++++ .../com/spotify/docker/BuildMojoTest.java | 12 ++++++++ src/test/resources/pom-build-rm-false.xml | 29 +++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 src/test/resources/pom-build-rm-false.xml diff --git a/src/main/java/com/spotify/docker/BuildMojo.java b/src/main/java/com/spotify/docker/BuildMojo.java index b35dcd73d..df91822cf 100644 --- a/src/main/java/com/spotify/docker/BuildMojo.java +++ b/src/main/java/com/spotify/docker/BuildMojo.java @@ -127,6 +127,10 @@ public class BuildMojo extends AbstractDockerMojo { @Parameter(property = "noCache", defaultValue = "false") private boolean noCache; + /** Set to false to pass the `--rm` flag to the Docker daemon when building an image. */ + @Parameter(property = "rm", defaultValue = "true") + private boolean rm; + /** Flag to push image after it is built. Defaults to false. */ @Parameter(property = "pushImage", defaultValue = "false") private boolean pushImage; @@ -799,6 +803,9 @@ private DockerClient.BuildParam[] buildParams() if (noCache) { buildParams.add(DockerClient.BuildParam.noCache()); } + if (!rm) { + buildParams.add(DockerClient.BuildParam.rm(false)); + } if (!buildArgs.isEmpty()) { buildParams.add(DockerClient.BuildParam.create("buildargs", URLEncoder.encode(OBJECT_MAPPER.writeValueAsString(buildArgs), "UTF-8"))); diff --git a/src/test/java/com/spotify/docker/BuildMojoTest.java b/src/test/java/com/spotify/docker/BuildMojoTest.java index b72902da7..8dc973486 100644 --- a/src/test/java/com/spotify/docker/BuildMojoTest.java +++ b/src/test/java/com/spotify/docker/BuildMojoTest.java @@ -527,6 +527,18 @@ public void testNoCache() throws Exception { eq(BuildParam.noCache())); } + public void testRmFalse() throws Exception { + final BuildMojo mojo = setupMojo(getPom("/pom-build-rm-false.xml")); + final DockerClient docker = mock(DockerClient.class); + + mojo.execute(docker); + + verify(docker).build(any(Path.class), + anyString(), + any(ProgressHandler.class), + eq(BuildParam.rm(false))); + } + public void testBuildWithSkipDockerBuild() throws Exception { final BuildMojo mojo = setupMojo(getPom("/pom-build-skip-build.xml")); assertThat(mojo.isSkipDockerBuild()).isTrue(); diff --git a/src/test/resources/pom-build-rm-false.xml b/src/test/resources/pom-build-rm-false.xml new file mode 100644 index 000000000..821f92fe0 --- /dev/null +++ b/src/test/resources/pom-build-rm-false.xml @@ -0,0 +1,29 @@ + + + 4.0.0 + + Docker Maven Plugin Test Pom + com.spotify + docker-maven-plugin-test + 0.0.1-SNAPSHOT + jar + + + + + com.spotify + docker-maven-plugin + 0.1-SNAPSHOT + + http://host:2375 + src/test/resources/dockerDirectory + busybox + + false + + + + +