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