Skip to content

Commit

Permalink
Merge pull request fabric8io#198 from lrolaz/add-buildargs
Browse files Browse the repository at this point in the history
Support for docker command line option --build-arg
  • Loading branch information
mattnworb committed Mar 4, 2016
2 parents ac71b7d + 16958f6 commit 08428af
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/main/java/com/spotify/docker/BuildMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

package com.spotify.docker;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -52,6 +54,8 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -92,6 +96,11 @@ public class BuildMojo extends AbstractDockerMojo {
*/
private static final char WINDOWS_SEPARATOR = '\\';

/**
* Json Object Mapper to encode arguments map
*/
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

/**
* Directory containing the Dockerfile. If the value is not set, the plugin will generate a
* Dockerfile using the required baseImage value, plus the optional entryPoint, cmd and maintainer
Expand Down Expand Up @@ -233,6 +242,9 @@ public class BuildMojo extends AbstractDockerMojo {
@Parameter(defaultValue = "${project}")
private MavenProject mavenProject;

@Parameter(property = "dockerBuildArgs")
private Map<String, String> buildArgs;

private PluginParameterExpressionEvaluator expressionEvaluator;

public BuildMojo() {
Expand Down Expand Up @@ -771,14 +783,19 @@ public static String separatorsToUnix(final String path) {
return path.replace(WINDOWS_SEPARATOR, UNIX_SEPARATOR);
}

private DockerClient.BuildParam[] buildParams() {
private DockerClient.BuildParam[] buildParams()
throws UnsupportedEncodingException, JsonProcessingException {
final List<DockerClient.BuildParam> buildParams = Lists.newArrayList();
if (pullOnBuild) {
buildParams.add(DockerClient.BuildParam.pullNewerImage());
}
if (noCache) {
buildParams.add(DockerClient.BuildParam.noCache());
}
if (!buildArgs.isEmpty()) {
buildParams.add(DockerClient.BuildParam.create("buildargs",
URLEncoder.encode(OBJECT_MAPPER.writeValueAsString(buildArgs), "UTF-8")));
}
return buildParams.toArray(new DockerClient.BuildParam[buildParams.size()]);
}
}
15 changes: 15 additions & 0 deletions src/test/java/com/spotify/docker/BuildMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,21 @@ public void testBuildWithDockerDirectory() throws Exception {
assertFilesCopied();
}

public void testBuildWithDockerDirectoryWithArgs() throws Exception {
final File pom = getTestFile("src/test/resources/pom-build-docker-directory-args.xml");
assertNotNull("Null pom.xml", pom);
assertTrue("pom.xml does not exist", pom.exists());

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

mojo.execute(docker);
verify(docker).build(eq(Paths.get("target/docker")), eq("busybox"),
any(AnsiProgressHandler.class),
eq(DockerClient.BuildParam.create("buildargs", "%7B%22VERSION%22%3A%220.1%22%7D")));
assertFilesCopied();
}

public void testBuildWithPush() throws Exception {
final File pom = getTestFile("src/test/resources/pom-build-push.xml");
assertNotNull("Null pom.xml", pom);
Expand Down
44 changes: 44 additions & 0 deletions src/test/resources/pom-build-docker-directory-args.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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>
<dockerHost>http://host:2375</dockerHost>
<!-- test that dockerDirectory works correctly -->
<dockerDirectory>src/test/resources/dockerDirectory</dockerDirectory>
<imageName>busybox</imageName>
<buildArgs>
<VERSION>0.1</VERSION>
</buildArgs>
<resources>
<resource>
<!-- test we handle all elements correctly -->
<targetPath>resources</targetPath>
<directory>src/test/resources/copy1</directory>
<include>**/*.xml</include>
<exclude>**/*exclude*</exclude>
</resource>
<resource>
<!-- test we handle missing elements correctly -->
<directory>src/test/resources/copy2</directory>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
</project>

0 comments on commit 08428af

Please sign in to comment.