@@ -31,7 +31,7 @@ public final class DirectorySink implements Consumer<Collection<Artifact>> {
31
31
* "A[-C]-V.E" and prevents overwrite (what you usually want).
32
32
* <p>
33
33
* 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.
35
35
*/
36
36
public static DirectorySink flat (Output output , Path path ) throws IOException {
37
37
return new DirectorySink (
@@ -40,6 +40,7 @@ public static DirectorySink flat(Output output, Path path) throws IOException {
40
40
41
41
private final Output output ;
42
42
private final Path directory ;
43
+ private final boolean directoryCreated ;
43
44
private final ArtifactMatcher artifactMatcher ;
44
45
private final ArtifactMapper artifactMapper ;
45
46
private final ArtifactNameMapper artifactNameMapper ;
@@ -73,6 +74,9 @@ private DirectorySink(
73
74
}
74
75
if (!Files .exists (directory )) {
75
76
Files .createDirectories (directory );
77
+ this .directoryCreated = true ;
78
+ } else {
79
+ this .directoryCreated = false ;
76
80
}
77
81
78
82
this .artifactMatcher = requireNonNull (artifactMatcher , "artifactMatcher" );
@@ -91,7 +95,8 @@ public void accept(Collection<Artifact> artifacts) {
91
95
accept (artifact );
92
96
}
93
97
} catch (IOException e ) {
94
- cleanup (artifacts , e );
98
+ cleanup ();
99
+ throw new UncheckedIOException (e );
95
100
}
96
101
}
97
102
@@ -119,15 +124,21 @@ private void accept(Artifact artifact) throws IOException {
119
124
}
120
125
}
121
126
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 );
124
129
writtenPaths .forEach (p -> {
125
130
try {
126
131
Files .deleteIfExists (p );
127
132
} catch (IOException ex ) {
128
133
// ignore
129
134
}
130
135
});
131
- throw new UncheckedIOException (e );
136
+ if (directoryCreated ) {
137
+ try {
138
+ Files .deleteIfExists (directory );
139
+ } catch (IOException e ) {
140
+ // ignore
141
+ }
142
+ }
132
143
}
133
144
}
0 commit comments