From 52aed4f0f58454b135853521f4cca2e8f52b8e5f Mon Sep 17 00:00:00 2001 From: Rohan Kumar Date: Tue, 29 Sep 2020 02:00:26 +0530 Subject: [PATCH] Follow up PR for https://github.com/fabric8io/docker-maven-plugin/pull/1373 Use regex in order to resolve ARG value --- .../fabric8/maven/docker/util/DockerFileUtil.java | 14 ++++++++++---- .../maven/docker/util/DockerFileUtilTest.java | 6 ++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/fabric8/maven/docker/util/DockerFileUtil.java b/src/main/java/io/fabric8/maven/docker/util/DockerFileUtil.java index 4caf3583f..14836c5ea 100644 --- a/src/main/java/io/fabric8/maven/docker/util/DockerFileUtil.java +++ b/src/main/java/io/fabric8/maven/docker/util/DockerFileUtil.java @@ -43,6 +43,8 @@ */ public class DockerFileUtil { + private static final String ARG_PATTERN_REGEX = "\\$(?:\\{(.*)\\}|(.*))"; + private DockerFileUtil() {} /** @@ -166,10 +168,14 @@ static Map extractArgsFromLines(List argLines, Map args) { - if (argString.startsWith("$") && args.containsKey(argString.substring(1))) { - return args.get(argString.substring(1)); - } else if (argString.startsWith("${") && argString.endsWith("}") && args.containsKey(argString.substring(2, argString.length() - 1))) { - return args.get(argString.substring(2, argString.length() - 1)); + Pattern argPattern = Pattern.compile(ARG_PATTERN_REGEX); + Matcher matcher = argPattern.matcher(argString); + if (matcher.matches()) { + if (matcher.group(1) != null) { + return args.get(matcher.group(1)); + } else if (matcher.group(2) != null) { + return args.get(matcher.group(2)); + } } return null; } diff --git a/src/test/java/io/fabric8/maven/docker/util/DockerFileUtilTest.java b/src/test/java/io/fabric8/maven/docker/util/DockerFileUtilTest.java index 10de9e6c9..e8b2cdbce 100644 --- a/src/test/java/io/fabric8/maven/docker/util/DockerFileUtilTest.java +++ b/src/test/java/io/fabric8/maven/docker/util/DockerFileUtilTest.java @@ -32,6 +32,7 @@ import static io.fabric8.maven.docker.util.PathTestUtil.createTmpFile; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; /** * @author roland @@ -133,6 +134,11 @@ public void testMultiStageNamedWithDuplicates() throws Exception { public void testResolveArgValueFromStrContainingArgKey() { assertEquals("latest", DockerFileUtil.resolveArgValueFromStrContainingArgKey("$VERSION", Collections.singletonMap("VERSION", "latest"))); assertEquals("test", DockerFileUtil.resolveArgValueFromStrContainingArgKey("${project.scope}", Collections.singletonMap("project.scope", "test"))); + assertEquals("test", DockerFileUtil.resolveArgValueFromStrContainingArgKey("$ad", Collections.singletonMap("ad", "test"))); + assertNull(DockerFileUtil.resolveArgValueFromStrContainingArgKey("bla$ad", Collections.singletonMap("ad", "test"))); + assertNull(DockerFileUtil.resolveArgValueFromStrContainingArgKey("${foo}bar", Collections.singletonMap("foo", "test"))); + assertNull(DockerFileUtil.resolveArgValueFromStrContainingArgKey("bar${foo}", Collections.singletonMap("foo", "test"))); + assertNull(DockerFileUtil.resolveArgValueFromStrContainingArgKey("$ad", Collections.emptyMap())); } private File copyToTempDir(String resource) throws IOException {