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 import org.apache.maven.artifact.Artifact;
30 import org.apache.maven.artifact.ArtifactUtils;
31 import org.apache.maven.plugin.logging.Log;
32 import org.codehaus.plexus.util.StringUtils;
33
34
35
36
37
38
39 public final class DependencyUtil {
40
41
42
43
44
45
46
47
48
49
50 public static String getFormattedFileName(Artifact artifact, boolean removeVersion) {
51 return getFormattedFileName(artifact, removeVersion, false);
52 }
53
54
55
56
57
58
59
60
61
62
63
64 public static String getFormattedFileName(Artifact artifact, boolean removeVersion, boolean prependGroupId) {
65 return getFormattedFileName(artifact, removeVersion, prependGroupId, false);
66 }
67
68
69
70
71
72
73
74
75
76
77
78
79 public static String getFormattedFileName(
80 Artifact artifact, boolean removeVersion, boolean prependGroupId, boolean useBaseVersion) {
81 return getFormattedFileName(artifact, removeVersion, prependGroupId, useBaseVersion, false);
82 }
83
84
85
86
87
88
89
90
91
92
93
94
95
96 public static String getFormattedFileName(
97 Artifact artifact,
98 boolean removeVersion,
99 boolean prependGroupId,
100 boolean useBaseVersion,
101 boolean removeClassifier) {
102 StringBuilder destFileName = new StringBuilder();
103
104 if (prependGroupId) {
105 destFileName.append(artifact.getGroupId()).append(".");
106 }
107
108 String versionString;
109 if (!removeVersion) {
110 if (useBaseVersion) {
111 versionString = "-" + ArtifactUtils.toSnapshotVersion(artifact.getVersion());
112 } else {
113 versionString = "-" + artifact.getVersion();
114 }
115 } else {
116 versionString = "";
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 public static File getFormattedOutputDirectory(
146 boolean useSubdirsPerScope,
147 boolean useSubdirsPerType,
148 boolean useSubdirPerArtifact,
149 boolean useRepositoryLayout,
150 boolean removeVersion,
151 boolean removeType,
152 File outputDirectory,
153 Artifact artifact) {
154 StringBuilder sb = new StringBuilder(128);
155 if (useRepositoryLayout) {
156
157 sb.append(artifact.getGroupId().replace('.', File.separatorChar)).append(File.separatorChar);
158
159 sb.append(artifact.getArtifactId()).append(File.separatorChar);
160
161 sb.append(artifact.getBaseVersion()).append(File.separatorChar);
162 } else {
163 if (useSubdirsPerScope) {
164 sb.append(artifact.getScope()).append(File.separatorChar);
165 }
166 if (useSubdirsPerType) {
167 sb.append(artifact.getType()).append("s").append(File.separatorChar);
168 }
169 if (useSubdirPerArtifact) {
170 String artifactString = getDependencyId(artifact, removeVersion, removeType);
171 sb.append(artifactString).append(File.separatorChar);
172 }
173 }
174 return new File(outputDirectory, sb.toString());
175 }
176
177 private static String getDependencyId(Artifact artifact, boolean removeVersion, boolean removeType) {
178 StringBuilder sb = new StringBuilder();
179
180 sb.append(artifact.getArtifactId());
181
182 if (!removeVersion) {
183 sb.append("-");
184 sb.append(artifact.getVersion());
185 }
186
187 if (StringUtils.isNotEmpty(artifact.getClassifier())) {
188 sb.append("-");
189 sb.append(artifact.getClassifier());
190 }
191
192
193
194
195 if (!removeType && !Objects.equals(artifact.getClassifier(), artifact.getType())) {
196 sb.append("-");
197 sb.append(artifact.getType());
198 }
199
200 return sb.toString();
201 }
202
203
204
205
206
207
208
209
210
211
212
213 @Deprecated
214 public static synchronized void write(String string, File file, boolean append, Log log) throws IOException {
215 write(string, file, append, "UTF-8");
216 }
217
218
219
220
221
222
223
224
225
226
227 public static synchronized void write(String string, File file, boolean append, String encoding)
228 throws IOException {
229 file.getParentFile().mkdirs();
230
231 try (Writer writer = new OutputStreamWriter(new FileOutputStream(file, append), encoding)) {
232 writer.write(string);
233 }
234 }
235
236
237
238
239
240
241
242
243 public static synchronized void log(String string, Log log) throws IOException {
244 BufferedReader reader = new BufferedReader(new StringReader(string));
245
246 String line;
247
248 while ((line = reader.readLine()) != null) {
249 log.info(line);
250 }
251
252 reader.close();
253 }
254
255
256
257
258
259
260
261 public static String[] tokenizer(String str) {
262 return StringUtils.split(cleanToBeTokenizedString(str), ",");
263 }
264
265
266
267
268
269
270
271 public static String cleanToBeTokenizedString(String str) {
272 String ret = "";
273 if (!StringUtils.isEmpty(str)) {
274
275 ret = str.trim().replaceAll("[\\s]*,[\\s]*", ",");
276 }
277
278 return ret;
279 }
280 }