Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Automatically create parent directories of portPropertyFile path #1761

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading