Skip to content

Commit

Permalink
Feat: Automatically create parent directories of portPropertyFile path
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume Villena <[email protected]>
  • Loading branch information
Willena committed Mar 6, 2024
1 parent 110a8f8 commit 4db1e84
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
3 changes: 2 additions & 1 deletion doc/changelog.md
Original file line number Diff line number Diff line change
@@ -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 <noCache> option is now propagated down to the buildx command, if it is set in the <build> section. ([1717](https://github.com/fabric8io/docker-maven-plugin/pull/1717))
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/io/fabric8/maven/docker/access/PortMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 4db1e84

Please sign in to comment.