1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.dependency.utils;
20
21 import java.io.BufferedReader;
22 import java.io.File;
23 import java.io.IOException;
24 import java.io.StringReader;
25 import java.io.Writer;
26 import java.nio.charset.Charset;
27 import java.nio.file.Files;
28 import java.nio.file.OpenOption;
29 import java.nio.file.StandardOpenOption;
30 import java.util.Objects;
31
32 import org.apache.maven.artifact.Artifact;
33 import org.apache.maven.artifact.ArtifactUtils;
34 import org.apache.maven.plugin.logging.Log;
35
36
37
38
39
40
41 public final class DependencyUtil {
42
43
44
45
46
47
48
49
50
51
52 public static String getFormattedFileName(Artifact artifact, boolean removeVersion) {
53 return getFormattedFileName(artifact, removeVersion, false);
54 }
55
56
57
58
59
60
61
62
63
64
65
66 public static String getFormattedFileName(Artifact artifact, boolean removeVersion, boolean prependGroupId) {
67 return getFormattedFileName(artifact, removeVersion, prependGroupId, false);
68 }
69
70
71
72
73
74
75
76
77
78
79
80
81 public static String getFormattedFileName(
82 Artifact artifact, boolean removeVersion, boolean prependGroupId, boolean useBaseVersion) {
83 return getFormattedFileName(artifact, removeVersion, prependGroupId, useBaseVersion, false);
84 }
85
86
87
88
89
90
91
92
93
94
95
96
97
98 public static String getFormattedFileName(
99 Artifact artifact,
100 boolean removeVersion,
101 boolean prependGroupId,
102 boolean useBaseVersion,
103 boolean removeClassifier) {
104 StringBuilder destFileName = new StringBuilder();
105
106 if (prependGroupId) {
107 destFileName.append(artifact.getGroupId()).append(".");
108 }
109
110 String versionString = "";
111 if (!removeVersion) {
112 if (useBaseVersion) {
113 versionString = "-" + ArtifactUtils.toSnapshotVersion(artifact.getVersion());
114 } else {
115 versionString = "-" + artifact.getVersion();
116 }
117 }
118
119 String classifierString = "";
120
121 if (!removeClassifier
122 && artifact.getClassifier() != null
123 && !artifact.getClassifier().isEmpty()) {
124 classifierString = "-" + artifact.getClassifier();
125 }
126 destFileName.append(artifact.getArtifactId()).append(versionString);
127 destFileName.append(classifierString).append(".");
128 destFileName.append(artifact.getArtifactHandler().getExtension());
129
130 return destFileName.toString();
131 }
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147 public static File getFormattedOutputDirectory(
148 boolean useSubdirsPerScope,
149 boolean useSubdirsPerType,
150 boolean useSubdirPerArtifact,
151 boolean useRepositoryLayout,
152 boolean removeVersion,
153 boolean removeType,
154 File outputDirectory,
155 Artifact artifact) {
156 StringBuilder sb = new StringBuilder(128);
157 if (useRepositoryLayout) {
158
159 sb.append(artifact.getGroupId().replace('.', File.separatorChar)).append(File.separatorChar);
160
161 sb.append(artifact.getArtifactId()).append(File.separatorChar);
162
163 sb.append(artifact.getBaseVersion()).append(File.separatorChar);
164 } else {
165 if (useSubdirsPerScope) {
166 sb.append(artifact.getScope()).append(File.separatorChar);
167 }
168 if (useSubdirsPerType) {
169 sb.append(artifact.getType()).append("s").append(File.separatorChar);
170 }
171 if (useSubdirPerArtifact) {
172 String artifactString = getDependencyId(artifact, removeVersion, removeType);
173 sb.append(artifactString).append(File.separatorChar);
174 }
175 }
176 return new File(outputDirectory, sb.toString());
177 }
178
179 private static String getDependencyId(Artifact artifact, boolean removeVersion, boolean removeType) {
180 StringBuilder sb = new StringBuilder();
181
182 sb.append(artifact.getArtifactId());
183
184 if (!removeVersion) {
185 sb.append("-");
186 sb.append(artifact.getVersion());
187 }
188
189 if (artifact.getClassifier() != null && !artifact.getClassifier().isEmpty()) {
190 sb.append("-");
191 sb.append(artifact.getClassifier());
192 }
193
194
195
196
197 if (!removeType && !Objects.equals(artifact.getClassifier(), artifact.getType())) {
198 sb.append("-");
199 sb.append(artifact.getType());
200 }
201
202 return sb.toString();
203 }
204
205
206
207
208
209
210
211
212
213
214
215 @Deprecated
216 public static synchronized void write(String string, File file, boolean append, Log log) throws IOException {
217 write(string, file, append, "UTF-8");
218 }
219
220
221
222
223
224
225
226
227
228
229 public static synchronized void write(String string, File file, boolean append, String encoding)
230 throws IOException {
231 Files.createDirectories(file.getParentFile().toPath());
232
233 OpenOption appendOption = append ? StandardOpenOption.APPEND : StandardOpenOption.TRUNCATE_EXISTING;
234
235 try (Writer writer = Files.newBufferedWriter(
236 file.toPath(),
237 Charset.forName(encoding),
238 appendOption,
239 StandardOpenOption.CREATE,
240 StandardOpenOption.WRITE)) {
241 writer.write(string);
242 }
243 }
244
245
246
247
248
249
250
251
252
253
254
255
256 public static synchronized void log(String string, Log log) throws IOException {
257 try (BufferedReader reader = new BufferedReader(new StringReader(string))) {
258 reader.lines().forEach(log::info);
259 }
260 }
261
262
263
264
265
266
267
268 public static String cleanToBeTokenizedString(String str) {
269 String ret = "";
270 if (!(str == null || str.isEmpty())) {
271
272 ret = str.trim().replaceAll("\\s*,\\s*", ",");
273 }
274
275 return ret;
276 }
277 }