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 && StringUtils.isNotEmpty(artifact.getClassifier())) {
122 classifierString = "-" + artifact.getClassifier();
123 }
124 destFileName.append(artifact.getArtifactId()).append(versionString);
125 destFileName.append(classifierString).append(".");
126 destFileName.append(artifact.getArtifactHandler().getExtension());
127
128 return destFileName.toString();
129 }
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146 public static File getFormattedOutputDirectory(
147 boolean useSubdirsPerScope,
148 boolean useSubdirsPerType,
149 boolean useSubdirPerArtifact,
150 boolean useRepositoryLayout,
151 boolean removeVersion,
152 boolean removeType,
153 File outputDirectory,
154 Artifact artifact) {
155 StringBuilder sb = new StringBuilder(128);
156 if (useRepositoryLayout) {
157
158 sb.append(artifact.getGroupId().replace('.', File.separatorChar)).append(File.separatorChar);
159
160 sb.append(artifact.getArtifactId()).append(File.separatorChar);
161
162 sb.append(artifact.getBaseVersion()).append(File.separatorChar);
163 } else {
164 if (useSubdirsPerScope) {
165 sb.append(artifact.getScope()).append(File.separatorChar);
166 }
167 if (useSubdirsPerType) {
168 sb.append(artifact.getType()).append("s").append(File.separatorChar);
169 }
170 if (useSubdirPerArtifact) {
171 String artifactString = getDependencyId(artifact, removeVersion, removeType);
172 sb.append(artifactString).append(File.separatorChar);
173 }
174 }
175 return new File(outputDirectory, sb.toString());
176 }
177
178 private static String getDependencyId(Artifact artifact, boolean removeVersion, boolean removeType) {
179 StringBuilder sb = new StringBuilder();
180
181 sb.append(artifact.getArtifactId());
182
183 if (!removeVersion) {
184 sb.append("-");
185 sb.append(artifact.getVersion());
186 }
187
188 if (StringUtils.isNotEmpty(artifact.getClassifier())) {
189 sb.append("-");
190 sb.append(artifact.getClassifier());
191 }
192
193
194
195
196 if (!removeType && !Objects.equals(artifact.getClassifier(), artifact.getType())) {
197 sb.append("-");
198 sb.append(artifact.getType());
199 }
200
201 return sb.toString();
202 }
203
204
205
206
207
208
209
210
211
212
213
214 @Deprecated
215 public static synchronized void write(String string, File file, boolean append, Log log) throws IOException {
216 write(string, file, append, "UTF-8");
217 }
218
219
220
221
222
223
224
225
226
227
228 public static synchronized void write(String string, File file, boolean append, String encoding)
229 throws IOException {
230 Files.createDirectories(file.getParentFile().toPath());
231
232 OpenOption appendOption = append ? StandardOpenOption.APPEND : StandardOpenOption.TRUNCATE_EXISTING;
233
234 try (Writer writer = Files.newBufferedWriter(
235 file.toPath(),
236 Charset.forName(encoding),
237 appendOption,
238 StandardOpenOption.CREATE,
239 StandardOpenOption.WRITE)) {
240 writer.write(string);
241 }
242 }
243
244
245
246
247
248
249
250
251 public static synchronized void log(String string, Log log) throws IOException {
252 try (BufferedReader reader = new BufferedReader(new StringReader(string))) {
253 reader.lines().forEach(log::info);
254 }
255 }
256
257
258
259
260
261
262
263 public static String[] tokenizer(String str) {
264 String s = cleanToBeTokenizedString(str);
265 if (s.isEmpty()) {
266 return new String[0];
267 }
268 return cleanToBeTokenizedString(str).split(",");
269 }
270
271
272
273
274
275
276
277 public static String cleanToBeTokenizedString(String str) {
278 String ret = "";
279 if (!(str == null || str.isEmpty())) {
280
281 ret = str.trim().replaceAll("\\s*,\\s*", ",");
282 }
283
284 return ret;
285 }
286 }