diff --git a/src/main/asciidoc/inc/build/_overview.adoc b/src/main/asciidoc/inc/build/_overview.adoc index 086e3bbc2..223c7d2f4 100644 --- a/src/main/asciidoc/inc/build/_overview.adoc +++ b/src/main/asciidoc/inc/build/_overview.adoc @@ -9,14 +9,13 @@ When using this mode, the Dockerfile is created on the fly with all instructions [[external-dockerfile]] .External Dockerfile or docker archive -Alternatively an external Dockerfile template or docker archive can be used. This mode is switch on by using one of these three configuration options within -the `` configuration section. +Alternatively an external Dockerfile template or Docker archive can be used. This mode is switch on by using one of these three configuration options within -* *dockerFileDir* specifies a directory containing a `Dockerfile` that will be used to create the image. -* *dockerFile* specifies a specific Dockerfile. The `dockerFileDir` is set to the directory containing the file. -* *dockerArchive* specifies a previously saved image archive to load directly. +* *dockerFileDir* specifies a directory containing a Dockerfile that will be used to create the image. The name of the Dockerfile is `Dockerfile` by default but can be also set with the option `dockerFile` (see below). +* *dockerFile* specifies a specific Dockerfile path. The Docker build context directory is set to `dockerFileDir` if given. If not the directory by default is the directory in which the Dockerfile is stored. +* *dockerArchive* specifies a previously saved image archive to load directly. Such a tar archive can be created with `docker save`. If a `dockerArchive` is provided, no `dockerFile` or `dockerFileDir` must be given. -If `dockerFileDir` is a relative path it is looked up in `${project.basedir}/src/main/docker`. You can make easily an absolute path by prefixing with `${project.basedir}`. +All paths can be either absolute or relative paths (except when both `dockerFileDir` and `dockerFile` are provided in which case `dockerFile` must not be absolute). A relative path is looked up in `${project.basedir}/src/main/docker` by default. You can make it easily an absolute path by using `${project.basedir} in your configuration. Any additional files located in the `dockerFileDir` directory will also be added to the build context as well as any files specified by an assembly. However, you still need to insert `ADD` or `COPY` directives yourself into the Dockerfile. diff --git a/src/main/java/io/fabric8/maven/docker/AbstractBuildSupportMojo.java b/src/main/java/io/fabric8/maven/docker/AbstractBuildSupportMojo.java index dda8f256e..b3fd62ccc 100644 --- a/src/main/java/io/fabric8/maven/docker/AbstractBuildSupportMojo.java +++ b/src/main/java/io/fabric8/maven/docker/AbstractBuildSupportMojo.java @@ -100,7 +100,7 @@ private void autoPullBaseImage(ServiceHub hub, ImageConfiguration imageConfig) BuildImageConfiguration buildConfig = imageConfig.getBuildConfiguration(); if (buildConfig.getDockerArchive() != null) { - // No autopull need in archive mode + // No auto pull needed in archive mode return; } diff --git a/src/main/java/io/fabric8/maven/docker/access/DockerAccess.java b/src/main/java/io/fabric8/maven/docker/access/DockerAccess.java index b7ed6c758..2b3b1fa0a 100644 --- a/src/main/java/io/fabric8/maven/docker/access/DockerAccess.java +++ b/src/main/java/io/fabric8/maven/docker/access/DockerAccess.java @@ -153,10 +153,10 @@ void copyArchive(String containerId, File archive, String targetPath) * Load an image from an archive. * * @param image the image to pull. - * @param filepath a URL string of the archive + * @param tarArchive archive file * @throws DockerAccessException if the image couldn't be loaded. */ - void loadImage(String image, String filepath) throws DockerAccessException; + void loadImage(String image, File tarArchive) throws DockerAccessException; /** * Pull an image from a remote registry and store it locally. diff --git a/src/main/java/io/fabric8/maven/docker/access/hc/DockerAccessWithHcClient.java b/src/main/java/io/fabric8/maven/docker/access/hc/DockerAccessWithHcClient.java index d48fe0344..5eca8552e 100644 --- a/src/main/java/io/fabric8/maven/docker/access/hc/DockerAccessWithHcClient.java +++ b/src/main/java/io/fabric8/maven/docker/access/hc/DockerAccessWithHcClient.java @@ -362,13 +362,13 @@ public void removeContainer(String containerId, boolean removeVolumes) } @Override - public void loadImage(String image, String filepath) throws DockerAccessException { + public void loadImage(String image, File tarArchive) throws DockerAccessException { String url = urlBuilder.loadImage(); try { - delegate.post(url, new File(filepath), new BodyAndStatusResponseHandler(), HTTP_OK); + delegate.post(url, tarArchive, new BodyAndStatusResponseHandler(), HTTP_OK); } catch (IOException e) { - throw new DockerAccessException(e, "Unable to load %s", filepath); + throw new DockerAccessException(e, "Unable to load %s", tarArchive); } } diff --git a/src/main/java/io/fabric8/maven/docker/config/BuildImageConfiguration.java b/src/main/java/io/fabric8/maven/docker/config/BuildImageConfiguration.java index ff197f30e..5c4a61ded 100644 --- a/src/main/java/io/fabric8/maven/docker/config/BuildImageConfiguration.java +++ b/src/main/java/io/fabric8/maven/docker/config/BuildImageConfiguration.java @@ -153,7 +153,7 @@ public class BuildImageConfiguration implements Serializable { private BuildTarArchiveCompression compression = BuildTarArchiveCompression.none; // Path to Dockerfile to use, initialized lazily .... - private File dockerFileFile; + private File dockerFileFile, dockerArchiveFile; public BuildImageConfiguration() {} @@ -165,8 +165,8 @@ public File getDockerFile() { return dockerFileFile; } - public String getDockerArchive() { - return dockerArchive; + public File getDockerArchive() { + return dockerArchiveFile; } public String getFrom() { @@ -269,6 +269,10 @@ public File getAbsoluteDockerFilePath(MojoParameters mojoParams) { return EnvUtil.prepareAbsoluteSourceDirPath(mojoParams, getDockerFile().getPath()); } + public File getAbsoluteDockerTarPath(MojoParameters mojoParams) { + return EnvUtil.prepareAbsoluteSourceDirPath(mojoParams, getDockerArchive().getPath()); + } + public static class Builder { private final BuildImageConfiguration config; @@ -478,11 +482,23 @@ private void initDockerFileFile(Logger log) { "Only one of them can be specified."); } dockerFileFile = findDockerFileFile(log); + + if (dockerArchive != null) { + dockerArchiveFile = new File(dockerArchive); + } } private File findDockerFileFile(Logger log) { if (dockerFile != null) { - return new File(dockerFile); + File dFile = new File(dockerFile); + if (dockerFileDir == null) { + return dFile; + } else { + if (dFile.isAbsolute()) { + throw new IllegalArgumentException(" can not be absolute path if also set."); + } + return new File(dockerFileDir, dockerFile); + } } if (dockerFileDir != null) { diff --git a/src/main/java/io/fabric8/maven/docker/service/BuildService.java b/src/main/java/io/fabric8/maven/docker/service/BuildService.java index 39f59c93f..49ad6e95a 100644 --- a/src/main/java/io/fabric8/maven/docker/service/BuildService.java +++ b/src/main/java/io/fabric8/maven/docker/service/BuildService.java @@ -31,7 +31,7 @@ public class BuildService { /** * Build an image - * + * * @param imageConfig the image configuration * @param params mojo params for the project * @param noCache if not null, dictate the caching behaviour. Otherwise its taken from the build configuration @@ -57,7 +57,7 @@ public void buildImage(ImageConfiguration imageConfig, MojoParameters params, bo long time = System.currentTimeMillis(); if (buildConfig.getDockerArchive() != null) { - docker.loadImage(imageName, buildConfig.getDockerArchive()); + docker.loadImage(imageName, buildConfig.getAbsoluteDockerTarPath(params)); log.info("%s: Loaded tarball in %s", buildConfig.getDockerArchive(), EnvUtil.formatDurationTill(time)); return; } diff --git a/src/test/java/integration/DockerAccessIT.java b/src/test/java/integration/DockerAccessIT.java index bea5d0916..abc272413 100644 --- a/src/test/java/integration/DockerAccessIT.java +++ b/src/test/java/integration/DockerAccessIT.java @@ -89,7 +89,7 @@ public void testPullStartStopRemove() throws DockerAccessException, InterruptedE @Test public void testLoadImage() throws DockerAccessException { testDoesNotHave(); - dockerClient.loadImage(IMAGE_LATEST, "integration/busybox-image.tar.gz"); + dockerClient.loadImage(IMAGE_LATEST, new File("integration/busybox-image.tar.gz")); assertTrue(hasImage(IMAGE_LATEST)); testRemoveImage(IMAGE_LATEST); } diff --git a/src/test/java/io/fabric8/maven/docker/access/hc/DockerAccessWithHcClientTest.java b/src/test/java/io/fabric8/maven/docker/access/hc/DockerAccessWithHcClientTest.java index 2ca32ea6a..73b2b2e99 100644 --- a/src/test/java/io/fabric8/maven/docker/access/hc/DockerAccessWithHcClientTest.java +++ b/src/test/java/io/fabric8/maven/docker/access/hc/DockerAccessWithHcClientTest.java @@ -4,6 +4,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import java.io.File; import java.io.IOException; import java.util.Map; @@ -140,7 +141,7 @@ private void whenPushImage() { } private void whenLoadImage() { try { - client.loadImage(imageName, archiveFile); + client.loadImage(imageName, new File(archiveFile)); } catch (Exception e) { thrownException = e; } diff --git a/src/test/java/io/fabric8/maven/docker/config/BuildImageConfigurationTest.java b/src/test/java/io/fabric8/maven/docker/config/BuildImageConfigurationTest.java index 05d8c4d99..dd129d341 100644 --- a/src/test/java/io/fabric8/maven/docker/config/BuildImageConfigurationTest.java +++ b/src/test/java/io/fabric8/maven/docker/config/BuildImageConfigurationTest.java @@ -1,6 +1,6 @@ package io.fabric8.maven.docker.config; /* - * + * * Copyright 2016 Roland Huss * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -123,7 +123,7 @@ public void dockerArchive() { config.initAndValidate(logger); assertFalse(config.isDockerFileMode()); - assertEquals("this", config.getDockerArchive()); + assertEquals(new File("this"), config.getDockerArchive()); } diff --git a/src/test/java/io/fabric8/maven/docker/config/handler/property/PropertyConfigHandlerTest.java b/src/test/java/io/fabric8/maven/docker/config/handler/property/PropertyConfigHandlerTest.java index 69db29568..e1b0289cd 100644 --- a/src/test/java/io/fabric8/maven/docker/config/handler/property/PropertyConfigHandlerTest.java +++ b/src/test/java/io/fabric8/maven/docker/config/handler/property/PropertyConfigHandlerTest.java @@ -20,6 +20,7 @@ import io.fabric8.maven.docker.config.*; import io.fabric8.maven.docker.config.handler.AbstractConfigHandlerTest; +import io.fabric8.maven.docker.util.MojoParameters; import mockit.Expectations; import mockit.Mocked; import mockit.integration.junit4.JMockit; @@ -167,7 +168,7 @@ public void testDockerArchive() { ImageConfiguration config = resolveExternalImageConfig(testData); config.initAndValidate(ConfigHelper.NameFormatter.IDENTITY, null); assertFalse(config.getBuildConfiguration().isDockerFileMode()); - assertEquals("dockerLoad.tar", config.getBuildConfiguration().getDockerArchive()); + assertEquals(new File("dockerLoad.tar"), config.getBuildConfiguration().getDockerArchive()); } @Test(expected = IllegalArgumentException.class) diff --git a/src/test/java/io/fabric8/maven/docker/service/LoadImageTest.java b/src/test/java/io/fabric8/maven/docker/service/LoadImageTest.java index e3c458545..aacab2d24 100644 --- a/src/test/java/io/fabric8/maven/docker/service/LoadImageTest.java +++ b/src/test/java/io/fabric8/maven/docker/service/LoadImageTest.java @@ -4,18 +4,18 @@ import io.fabric8.maven.docker.access.DockerAccessException; import io.fabric8.maven.docker.assembly.DockerAssemblyManager; import io.fabric8.maven.docker.config.BuildImageConfiguration; +import io.fabric8.maven.docker.config.ConfigHelper; import io.fabric8.maven.docker.config.ImageConfiguration; import io.fabric8.maven.docker.util.Logger; import io.fabric8.maven.docker.util.MojoParameters; -import mockit.Injectable; -import mockit.Mocked; -import mockit.Tested; -import mockit.Verifications; +import mockit.*; import mockit.integration.junit4.JMockit; import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.project.MavenProject; import org.junit.Test; import org.junit.runner.RunWith; +import java.io.File; import java.util.Collections; @RunWith(JMockit.class) @@ -31,6 +31,9 @@ public class LoadImageTest { @Injectable private Logger log; + @Mocked + private MavenProject project; + @Mocked private MojoParameters params; @@ -42,15 +45,23 @@ public class LoadImageTest { private String dockerArchive; - @Test public void testLoadImage() throws DockerAccessException, MojoExecutionException { + givenMojoParameters(); givenAnImageConfiguration(); givenDockerArchive("test.tar"); whenBuildImage(); thenImageIsBuilt(); } + private void givenMojoParameters() { + new Expectations() {{ + params.getProject(); + project.getBasedir(); result = "/maven-project"; + params.getSourceDirectory(); result = "src/main/docker"; + }}; + } + private void givenDockerArchive(String s) { dockerArchive = s; } @@ -65,6 +76,7 @@ private void givenAnImageConfiguration() { .alias("build-alias") .buildConfig(buildConfig) .build(); + imageConfig.initAndValidate(ConfigHelper.NameFormatter.IDENTITY,log); } private void whenBuildImage() throws DockerAccessException, MojoExecutionException { @@ -72,8 +84,9 @@ private void whenBuildImage() throws DockerAccessException, MojoExecutionExcepti } private void thenImageIsBuilt() throws DockerAccessException { + final File targetFile = new File("/maven-project/src/main/docker/test.tar"); new Verifications() {{ - docker.loadImage("build-image", dockerArchive); + docker.loadImage("build-image", withEqual(targetFile)); }}; }