From def90cccd92c5a1d71329ad7fd4104d798d64127 Mon Sep 17 00:00:00 2001 From: Qingyang Chen Date: Wed, 14 Mar 2018 16:38:39 -0400 Subject: [PATCH] Fixes DockerfileTemplate resource resolution. --- CHANGELOG.md | 1 + .../tools/jib/maven/DockerContextMojo.java | 39 +++++++++---------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 31c228737d..437bf1c6c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file. ### Changed ### Fixed +- jib:dockercontext not generating Dockerfile - Null tag validation generating NullPointerException ([#125](https://github.com/google/jib/issues/125)) - Build failure on project with no dependencies ([#126](https://github.com/google/jib/issues/126)) diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/DockerContextMojo.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/DockerContextMojo.java index cb2e2d98c1..cf9ec140f6 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/DockerContextMojo.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/DockerContextMojo.java @@ -21,10 +21,10 @@ import com.google.cloud.tools.jib.filesystem.DirectoryWalker; import com.google.cloud.tools.jib.filesystem.PathConsumer; import com.google.common.annotations.VisibleForTesting; +import com.google.common.io.InsecureRecursiveDeleteException; import com.google.common.io.MoreFiles; import com.google.common.io.Resources; import java.io.IOException; -import java.net.URISyntaxException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; @@ -130,22 +130,17 @@ DockerContextMojo setFrom(String baseImage) { @VisibleForTesting /** Makes a {@code Dockerfile} from the {@code DockerfileTemplate}. */ - String makeDockerfile(SourceFilesConfiguration sourceFilesConfiguration) - throws IOException, URISyntaxException { - Path dockerfileTemplate = Paths.get(Resources.getResource("DockerfileTemplate").toURI()); - - String dockerfile = new String(Files.readAllBytes(dockerfileTemplate), StandardCharsets.UTF_8); - dockerfile = - dockerfile - .replace("@@BASE_IMAGE@@", from) - .replace( - "@@DEPENDENCIES_PATH_ON_IMAGE@@", - sourceFilesConfiguration.getDependenciesPathOnImage()) - .replace( - "@@RESOURCES_PATH_ON_IMAGE@@", sourceFilesConfiguration.getResourcesPathOnImage()) - .replace("@@CLASSES_PATH_ON_IMAGE@@", sourceFilesConfiguration.getClassesPathOnImage()) - .replace("@@ENTRYPOINT@@", getEntrypoint(sourceFilesConfiguration)); - return dockerfile; + String makeDockerfile(SourceFilesConfiguration sourceFilesConfiguration) throws IOException { + String dockerfileTemplate = + Resources.toString(Resources.getResource("DockerfileTemplate"), StandardCharsets.UTF_8); + + return dockerfileTemplate + .replace("@@BASE_IMAGE@@", from) + .replace( + "@@DEPENDENCIES_PATH_ON_IMAGE@@", sourceFilesConfiguration.getDependenciesPathOnImage()) + .replace("@@RESOURCES_PATH_ON_IMAGE@@", sourceFilesConfiguration.getResourcesPathOnImage()) + .replace("@@CLASSES_PATH_ON_IMAGE@@", sourceFilesConfiguration.getClassesPathOnImage()) + .replace("@@ENTRYPOINT@@", getEntrypoint(sourceFilesConfiguration)); } /** @@ -214,11 +209,15 @@ private void createDockerContext(ProjectProperties projectProperties) projectProperties.getLog().info("Created Docker context at " + targetDir); + } catch (InsecureRecursiveDeleteException ex) { + throwMojoExecutionExceptionWithHelpMessage( + ex, + "cannot clear directory '" + + targetDir + + "' safely - clear it manually before creating the Docker context"); + } catch (IOException ex) { throwMojoExecutionExceptionWithHelpMessage(ex, "check if `targetDir` is set correctly"); - - } catch (URISyntaxException ex) { - throw new MojoFailureException("Unexpected URISyntaxException", ex); } }