From 4db1e84251b9ccba310f05a3607cd775f076efec Mon Sep 17 00:00:00 2001 From: Guillaume Villena Date: Wed, 28 Feb 2024 00:49:40 +0100 Subject: [PATCH] Feat: Automatically create parent directories of portPropertyFile path Signed-off-by: Guillaume Villena --- doc/changelog.md | 3 +- .../maven/docker/access/PortMapping.java | 4 +++ .../PortMappingPropertyWriteHelperTest.java | 32 +++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/doc/changelog.md b/doc/changelog.md index 070fc3575..5d47db3b0 100644 --- a/doc/changelog.md +++ b/doc/changelog.md @@ -1,6 +1,7 @@ # ChangeLog * **0.45-SNAPSHOT**: - + - Automatically create parent directories of portPropertyFile path + * **0.44.0** (2024-02-17): - Add new option "useDefaultExclusion" for build configuration to handle exclusion of hidden files ([1708](https://github.com/fabric8io/docker-maven-plugin/issues/1708)) - The option is now propagated down to the buildx command, if it is set in the section. ([1717](https://github.com/fabric8io/docker-maven-plugin/pull/1717)) diff --git a/src/main/java/io/fabric8/maven/docker/access/PortMapping.java b/src/main/java/io/fabric8/maven/docker/access/PortMapping.java index 3355dfcb3..2f335c321 100644 --- a/src/main/java/io/fabric8/maven/docker/access/PortMapping.java +++ b/src/main/java/io/fabric8/maven/docker/access/PortMapping.java @@ -403,6 +403,10 @@ public void write() throws IOException { private void writeProperties(Properties props, String file) throws IOException { File propFile = new File(file); + File parent = propFile.getParentFile(); + if (!parent.exists()) { + parent.mkdirs(); + } try (OutputStream os = new FileOutputStream(propFile)) { props.store(os, "Docker ports"); } catch (IOException e) { diff --git a/src/test/java/io/fabric8/maven/docker/access/PortMappingPropertyWriteHelperTest.java b/src/test/java/io/fabric8/maven/docker/access/PortMappingPropertyWriteHelperTest.java index 661c6c2af..5963ada82 100644 --- a/src/test/java/io/fabric8/maven/docker/access/PortMappingPropertyWriteHelperTest.java +++ b/src/test/java/io/fabric8/maven/docker/access/PortMappingPropertyWriteHelperTest.java @@ -8,6 +8,7 @@ import java.io.File; import java.io.IOException; import java.nio.file.Files; +import java.nio.file.Paths; import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -84,6 +85,37 @@ void testWriteImageAndGlobal() throws IOException { thenPropsContains("other.ip1", "1.2.3.4"); } + @Test + void testWriteCreatePaths() throws IOException { + File dir = Files.createTempDirectory("dmp-").toFile(); + Assertions.assertTrue(dir.delete()); + String globalFile = Paths.get(dir.getAbsolutePath(), "dmp-tmp.properties").toFile().getAbsolutePath(); + PortMapping mapping = createPortMapping("jolokia.port:8080", "18181:8181", "127.0.0.1:9090:9090", "127.0.0.1:other.port:5678"); + + // check that we can write on non-existant path with create set to "true" + givenAPortMappingWriter(globalFile); + whenUpdateDynamicMapping(mapping, "0.0.0.0", 8080, 49900); + whenUpdateDynamicMapping(mapping, "127.0.0.1", 5678, 49901); + whenWritePortMappings(null, mapping); + thenPropsFileExists(globalFile); + thenPropsSizeIs(2); + thenPropsContains("jolokia.port", 49900); + thenPropsContains("other.port", 49901); + + // Check that we can still write in an existing path + String globalFile2 = Paths.get(dir.getAbsolutePath(), "dmp-tmp2.properties").toFile().getAbsolutePath(); + givenAPortMappingWriter(globalFile2); + whenUpdateDynamicMapping(mapping, "0.0.0.0", 8080, 49900); + whenUpdateDynamicMapping(mapping, "127.0.0.1", 5678, 49901); + whenWritePortMappings(null, mapping); + thenPropsFileExists(globalFile2); + thenPropsSizeIs(2); + thenPropsContains("jolokia.port", 49900); + thenPropsContains("other.port", 49901); + + + } + private void givenADockerHostAddress(String host) { projProperties.setProperty("docker.host.address", host); }