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