Skip to content

Commit

Permalink
Refactor BuildImageConfiguration in order to handle contextDir
Browse files Browse the repository at this point in the history
  • Loading branch information
rohanKanojia committed Apr 7, 2019
1 parent f54795d commit b7d93e3
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 14 deletions.
4 changes: 4 additions & 0 deletions doc/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
- Support docker SHELL setting for runCmds (#1157)
- Added 'autoRemove' option for running containers (#1179)
- Added support for AWS EC2 instance roles when pushing to AWS ECR (#1186)
- Introduce `contextDir` configuration option which would be used to specify docker build context (#1189)
- Add support for auto-pulling multiple base image for multi stage builds (#1057)
- Fix usage of credential helper that do not support 'version' command (#1159)

Please note that `dockerFileDir` is now deprecated in favor of `contextDir` and it would be removed in 1.0.0.
It's still supported in this release but users are suggested to migrate to `contextDir` instead.

* **0.28.0** (2018-12-13)
- Update to JMockit 1.43
Expand Down
1 change: 1 addition & 0 deletions samples/dockerfile/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<build>
<!-- filter>@</filter-->
<dockerFileDir>${project.basedir}/src/main/docker</dockerFileDir>
<contextDir>${project.basedir}/src/main/docker</contextDir>
<assembly>
<descriptorRef>rootWar</descriptorRef>
</assembly>
Expand Down
6 changes: 2 additions & 4 deletions src/main/asciidoc/inc/build/_overview.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ When using this mode, the Dockerfile is created on the fly with all instructions
.External Dockerfile or Docker archive
Alternatively an external Dockerfile template or Docker archive can be used. This mode is switched 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. The name of the Dockerfile is `Dockerfile` by default but can be also set with the option `dockerFile` (see below).
* *dockerFileDir* *(This option is deprecated, please use contextDir instead)* 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.
* *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 an external Dockerfile located outside of Docker build context you need to explicitly specify Docker build context by following configuration option
* *contextDir* specifies a directory used as Docker build context. If not specified, Dockerfile's parent directory is used as build context.
* *contextDir* specifies docker build context if an external dockerfile is located outside of Docker build context. If not specified, Dockerfile's parent directory is used as build context.
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.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ public File createDockerTarArchive(String imageName, final MojoParameters params
archiveCustomizers.add(new ArchiverCustomizer() {
@Override
public TarArchiver customize(TarArchiver archiver) throws IOException {
DefaultFileSet fileSet = DefaultFileSet.fileSet(buildConfig.getAbsoluteContextDirPath(params));
DefaultFileSet fileSet = DefaultFileSet.fileSet(buildConfig.getContextDir() != null
? buildConfig.getAbsoluteContextDirPath(params)
: dockerFile.getParentFile());
addDockerIncludesExcludesIfPresent(fileSet, params);
// Exclude non-interpolated dockerfile from source tree
// Interpolated Dockerfile is already added as it was created into the output directory when
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package io.fabric8.maven.docker.config;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;

import io.fabric8.maven.docker.util.*;
Expand Down Expand Up @@ -30,6 +33,7 @@ public class BuildImageConfiguration implements Serializable {
* image. This Dockerfile will be enriched by the addition build configuration
*/
@Parameter
@Deprecated
private String dockerFileDir;

/**
Expand Down Expand Up @@ -572,20 +576,48 @@ private void initDockerFileFile(Logger log) {

private File findDockerFileFile(Logger log) {
if (dockerFile != null) {
if (EnvUtil.isWindows() && !EnvUtil.isValidWindowsFileName(dockerFile)) {
throw new IllegalArgumentException(String.format("Invalid Windows file name %s for <dockerFile>", dockerFile));
}

File dFile = new File(dockerFile);
if (dockerFileDir == null) {
if (dockerFileDir == null && contextDir == null) {
return dFile;
} else {
if (dFile.isAbsolute()) {
throw new IllegalArgumentException("<dockerFile> can not be absolute path if <dockerFileDir> also set.");
if(contextDir != null) {
if(dockerFileDir != null) {
log.warn("using contextDir as it has more precedence for Dockerfile");
}

if (dFile.isAbsolute()) {
try {
Files.copy(dFile.toPath(), Paths.get(contextDir));
} catch (IOException exception) {
log.warn("Error during copying Dockerfile to contextDir: ", exception.getMessage());
}
return new File(contextDir, "Dockerfile");
}
return new File(contextDir, dockerFile);
}
if (EnvUtil.isWindows() && !EnvUtil.isValidWindowsFileName(dockerFile)) {
throw new IllegalArgumentException(String.format("Invalid Windows file name %s for <dockerFile>", dockerFile));

if (dockerFileDir != null) {
if (dFile.isAbsolute()) {
throw new IllegalArgumentException("<dockerFile> can not be absolute path if <dockerFileDir> also set.");
}
log.warn("dockerFileDir parameter is deprecated, please migrate to contextDir");
return new File(dockerFileDir, dockerFile);
}
return new File(dockerFileDir, dockerFile);
}
}


if (contextDir != null) {
if(dockerFileDir != null) {
log.warn("using contextDir as it has more precedence for Dockerfile");
}
return new File(contextDir, "Dockerfile");
}

if (dockerFileDir != null) {
return new File(dockerFileDir, "Dockerfile");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public static String getExternalConfigActivationProperty(MavenProject project) {
*
* @param images the images to check
* @param apiVersion the original API version intended to use
* @param nameFormatter formmatter for image names
* @param nameFormatter formatter for image names
* @param log a logger for printing out diagnostic messages
* @return the minimal API Docker API required to be used for the given configuration.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ private boolean buildConfigured(BuildImageConfiguration config, ValueProvider va
return true;
}

if (isStringValueNull(valueProvider, config, CONTEXT_DIR, () -> config.getContextDirRaw())) {
return true;
}

if (isStringValueNull(valueProvider, config, DOCKER_FILE_DIR, () -> config.getDockerFileDirRaw())) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

import java.io.File;
import java.io.IOException;

import io.fabric8.maven.docker.util.Logger;
import mockit.Expectations;
Expand Down Expand Up @@ -105,7 +106,7 @@ public void contextDirAndDockerfile() {
dockerFile("src/docker/Dockerfile").
contextDir("target").build();
config.initAndValidate(logger);
assertEquals(new File("src/docker/Dockerfile"), config.getDockerFile());
assertEquals(new File("target/src/docker/Dockerfile"), config.getDockerFile());
assertEquals(new File("target"), config.getContextDir());
}

Expand All @@ -116,7 +117,23 @@ public void contextDirAndDockerfileDir() {
dockerFileDir("src/docker").
contextDir("target").build();
config.initAndValidate(logger);
assertEquals(new File("src/docker/Dockerfile"), config.getDockerFile());
assertEquals(new File("target/Dockerfile"), config.getDockerFile());
assertEquals(new File("target"), config.getContextDir());
}

@Test
public void contextDirAndAbsoluteDockerfile() throws IOException {
File tempDockerFile = File.createTempFile("Dockerfile", "");
tempDockerFile.deleteOnExit();
BuildImageConfiguration config = new BuildImageConfiguration.Builder()
.dockerFile(tempDockerFile.getAbsolutePath())
.contextDir("target")
.build();

// If contextDir is given and the dockerFile is an absolute path.
// The Dockerfile should then be copied over.
config.initAndValidate(logger);
assertEquals(new File("target/Dockerfile"), config.getDockerFile());
assertEquals(new File("target"), config.getContextDir());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,14 @@ public void testDockerFileDir() {
assertNotNull(config.getBuildConfiguration());
}

@Test
public void testContextDir() {
String[] testData = new String[] {k(ConfigKey.NAME), "image", k(ConfigKey.CONTEXT_DIR), "dir" };

ImageConfiguration config = resolveExternalImageConfig(testData);
assertNotNull(config.getBuildConfiguration());
}

@Test
public void testFilterDefault() {
String[] testData = new String[] {k(ConfigKey.NAME), "image", k(ConfigKey.FROM), "base" };
Expand Down

0 comments on commit b7d93e3

Please sign in to comment.