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