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