Skip to content

Commit 237014c

Browse files
committed
Better cleanup
1 parent d580862 commit 237014c

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

shared/src/main/java/eu/maveniverse/maven/toolbox/shared/DirectorySink.java

+16-5
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public final class DirectorySink implements Consumer<Collection<Artifact>> {
3131
* "A[-C]-V.E" and prevents overwrite (what you usually want).
3232
* <p>
3333
* This means that if your set of artifacts have artifacts with different groupIDs but same artifactIDs, this sink
34-
* will fail while accepting them, to prevent overwrite.
34+
* will fail while accepting them, to prevent overwrite. Duplicated artifacts are filtered out.
3535
*/
3636
public static DirectorySink flat(Output output, Path path) throws IOException {
3737
return new DirectorySink(
@@ -40,6 +40,7 @@ public static DirectorySink flat(Output output, Path path) throws IOException {
4040

4141
private final Output output;
4242
private final Path directory;
43+
private final boolean directoryCreated;
4344
private final ArtifactMatcher artifactMatcher;
4445
private final ArtifactMapper artifactMapper;
4546
private final ArtifactNameMapper artifactNameMapper;
@@ -73,6 +74,9 @@ private DirectorySink(
7374
}
7475
if (!Files.exists(directory)) {
7576
Files.createDirectories(directory);
77+
this.directoryCreated = true;
78+
} else {
79+
this.directoryCreated = false;
7680
}
7781

7882
this.artifactMatcher = requireNonNull(artifactMatcher, "artifactMatcher");
@@ -91,7 +95,8 @@ public void accept(Collection<Artifact> artifacts) {
9195
accept(artifact);
9296
}
9397
} catch (IOException e) {
94-
cleanup(artifacts, e);
98+
cleanup();
99+
throw new UncheckedIOException(e);
95100
}
96101
}
97102

@@ -119,15 +124,21 @@ private void accept(Artifact artifact) throws IOException {
119124
}
120125
}
121126

122-
private void cleanup(Collection<Artifact> artifacts, IOException e) {
123-
output.error("IO error, cleaning up: {}", e.getMessage(), e);
127+
private void cleanup() {
128+
output.error("Cleaning up: {}", directory);
124129
writtenPaths.forEach(p -> {
125130
try {
126131
Files.deleteIfExists(p);
127132
} catch (IOException ex) {
128133
// ignore
129134
}
130135
});
131-
throw new UncheckedIOException(e);
136+
if (directoryCreated) {
137+
try {
138+
Files.deleteIfExists(directory);
139+
} catch (IOException e) {
140+
// ignore
141+
}
142+
}
132143
}
133144
}

0 commit comments

Comments
 (0)