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.FileOutputStream;
24 import java.io.IOException;
25 import java.io.OutputStreamWriter;
26 import java.io.StringReader;
27 import java.io.Writer;
28 import java.util.Objects;
29
30 import org.apache.commons.lang3.StringUtils;
31 import org.apache.maven.artifact.Artifact;
32 import org.apache.maven.artifact.ArtifactUtils;
33 import org.apache.maven.plugin.logging.Log;
34
35
36
37
38
39
40 public final class DependencyUtil {
41
42
43
44
45
46
47
48
49
50
51 public static String getFormattedFileName(Artifact artifact, boolean removeVersion) {
52 return getFormattedFileName(artifact, removeVersion, false);
53 }
54
55
56
57
58
59
60
61
62
63
64
65 public static String getFormattedFileName(Artifact artifact, boolean removeVersion, boolean prependGroupId) {
66 return getFormattedFileName(artifact, removeVersion, prependGroupId, false);
67 }
68
69
70
71
72
73
74
75
76
77
78
79
80 public static String getFormattedFileName(
81 Artifact artifact, boolean removeVersion, boolean prependGroupId, boolean useBaseVersion) {
82 return getFormattedFileName(artifact, removeVersion, prependGroupId, useBaseVersion, false);
83 }
84
85
86
87
88
89
90
91
92
93
94
95
96
97 public static String getFormattedFileName(
98 Artifact artifact,
99 boolean removeVersion,
100 boolean prependGroupId,
101 boolean useBaseVersion,
102 boolean removeClassifier) {
103 StringBuilder destFileName = new StringBuilder();
104
105 if (prependGroupId) {
106 destFileName.append(artifact.getGroupId()).append(".");
107 }
108
109 String versionString;
110 if (!removeVersion) {
111 if (useBaseVersion) {
112 versionString = "-" + ArtifactUtils.toSnapshotVersion(artifact.getVersion());
113 } else {
114 versionString = "-" + artifact.getVersion();
115 }
116 } else {
117 versionString = "";
118 }
119
120 String classifierString = "";
121
122 if (!removeClassifier && StringUtils.isNotEmpty(artifact.getClassifier())) {
123 classifierString = "-" + artifact.getClassifier();
124 }
125 destFileName.append(artifact.getArtifactId()).append(versionString);
126 destFileName.append(classifierString).append(".");
127 destFileName.append(artifact.getArtifactHandler().getExtension());
128
129 return destFileName.toString();
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 file.getParentFile().mkdirs();
231
232 try (Writer writer = new OutputStreamWriter(new FileOutputStream(file, append), encoding)) {
233 writer.write(string);
234 }
235 }
236
237
238
239
240
241
242
243
244 public static synchronized void log(String string, Log log) throws IOException {
245 BufferedReader reader = new BufferedReader(new StringReader(string));
246
247 String line;
248
249 while ((line = reader.readLine()) != null) {
250 log.info(line);
251 }
252
253 reader.close();
254 }
255
256
257
258
259
260
261
262 public static String[] tokenizer(String str) {
263 return StringUtils.split(cleanToBeTokenizedString(str), ",");
264 }
265
266
267
268
269
270
271
272 public static String cleanToBeTokenizedString(String str) {
273 String ret = "";
274 if (!(str == null || str.isEmpty())) {
275
276 ret = str.trim().replaceAll("[\\s]*,[\\s]*", ",");
277 }
278
279 return ret;
280 }
281 }