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