Skip to content

Commit

Permalink
ensure entire directories are copied for ADDs
Browse files Browse the repository at this point in the history
Prior to this change, a mojo configuration like

```
<resource>
  <directory>some/directory</directory>
  <targetPath>/data</targetPath>
</resource>
```

would generate an `ADD /data /data` instruction but the `some/directory`
directory is never copied into the Docker build context directory, so
the actual build will fail.

With this change, the directory is copied to the build context
directory.

Note: it might seem weird that the `targetPath` is used as the directory
name to use *inside* the build context as opposed to just the target
path inside the container, but this is to mirror behavior when copying
individual files.

Fixes fabric8io#146.
  • Loading branch information
mattnworb committed Nov 12, 2015
1 parent bdb777c commit 994930e
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/main/java/com/spotify/docker/BuildMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
import org.codehaus.plexus.util.DirectoryScanner;
import org.codehaus.plexus.util.FileUtils;
import org.eclipse.jgit.api.errors.GitAPIException;

import java.io.File;
Expand Down Expand Up @@ -604,6 +605,9 @@ private void createDockerFile(final String directory, final List<String> filesTo
}
}

getLog().debug("Writing Dockerfile:" + System.lineSeparator() +
Joiner.on(System.lineSeparator()).join(commands));

// this will overwrite an existing file
Files.createDirectories(Paths.get(directory));
Files.write(Paths.get(directory, "Dockerfile"), commands, UTF_8);
Expand Down Expand Up @@ -641,6 +645,11 @@ private List<String> copyResources(String destination) throws IOException {
final String targetPath = resource.getTargetPath() == null ? "" : resource.getTargetPath();

if (copyWholeDir) {
final Path destPath = Paths.get(destination, targetPath);
getLog().info(String.format("Copying dir %s -> %s", source, destPath));

Files.createDirectories(destPath);
FileUtils.copyDirectoryStructure(source, destPath.toFile());
copiedPaths.add(separatorsToUnix(targetPath));
} else {
for (String included : includedFiles) {
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/com/spotify/docker/BuildMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

package com.spotify.docker;

import com.google.common.collect.ImmutableList;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.spotify.docker.client.AnsiProgressHandler;
Expand Down Expand Up @@ -229,6 +231,29 @@ public void testBuildWithGeneratedDockerfile() throws Exception {
Files.readAllLines(Paths.get("target/docker/Dockerfile"), UTF_8));
}

public void testBuildGeneratedDockerFile_CopiesEntireDirectory() throws Exception {
final File pom = getTestFile("src/test/resources/pom-build-copy-entire-directory.xml");

final BuildMojo mojo = setupMojo(pom);
final DockerClient docker = mock(DockerClient.class);
mojo.execute(docker);

verify(docker).build(eq(Paths.get("target/docker")), eq("test-copied-directory"),
any(AnsiProgressHandler.class));

List<String> expectedDockerFileContents = ImmutableList.of(
"FROM busybox",
"ADD /data /data",
"ENTRYPOINT echo"
);

assertEquals("wrong dockerfile contents", expectedDockerFileContents,
Files.readAllLines(Paths.get("target/docker/Dockerfile"), UTF_8));

assertFileExists("target/docker/data/file.txt");
assertFileExists("target/docker/data/nested/file2");
}

public void testBuildWithProfile() throws Exception {
final File pom = getTestFile("src/test/resources/pom-build-with-profile.xml");
assertNotNull("Null pom.xml", pom);
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/copy-wholedir-data/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello!
1 change: 1 addition & 0 deletions src/test/resources/copy-wholedir-data/nested/file2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hi
33 changes: 33 additions & 0 deletions src/test/resources/pom-build-copy-entire-directory.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<name>Docker Maven Plugin Test Pom</name>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.1-SNAPSHOT</version>
<configuration>
<baseImage>busybox</baseImage>
<imageName>test-copied-directory</imageName>
<entryPoint>echo</entryPoint>
<resources>
<resource>
<targetPath>/data</targetPath>
<directory>src/test/resources/copy-wholedir-data</directory>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
</project>

0 comments on commit 994930e

Please sign in to comment.