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 StringBuilder destFileName = new StringBuilder();
97
98 if ( prependGroupId )
99 {
100 destFileName.append( artifact.getGroupId() ).append( "." );
101 }
102
103 String versionString = null;
104 if ( !removeVersion )
105 {
106 if ( useBaseVersion )
107 {
108 versionString = "-" + artifact.getBaseVersion();
109 }
110 else
111 {
112 versionString = "-" + artifact.getVersion();
113 }
114 }
115 else
116 {
117 versionString = "";
118 }
119
120 String classifierString = "";
121
122 if ( StringUtils.isNotEmpty( artifact.getClassifier() ) )
123 {
124 classifierString = "-" + artifact.getClassifier();
125 }
126 destFileName.append( artifact.getArtifactId() ).append( versionString );
127 destFileName.append( classifierString ).append( "." );
128 destFileName.append( artifact.getArtifactHandler().getExtension() );
129
130 return destFileName.toString();
131 }
132
133
134
135
136
137
138
139
140
141
142
143
144
145 public static File getFormattedOutputDirectory( boolean useSubdirsPerScope, boolean useSubdirsPerType,
146 boolean useSubdirPerArtifact, boolean useRepositoryLayout,
147 boolean removeVersion, File outputDirectory, Artifact artifact )
148 {
149 StringBuilder sb = new StringBuilder( 128 );
150 if ( useRepositoryLayout )
151 {
152
153 sb.append( artifact.getGroupId().replace( '.', File.separatorChar ) ).append( File.separatorChar );
154
155 sb.append( artifact.getArtifactId() ).append( File.separatorChar );
156
157 sb.append( artifact.getBaseVersion() ).append( File.separatorChar );
158 }
159 else
160 {
161 if ( useSubdirsPerScope )
162 {
163 sb.append( artifact.getScope() ).append( File.separatorChar );
164 }
165 if ( useSubdirsPerType )
166 {
167 sb.append( artifact.getType() ).append( "s" ).append( File.separatorChar );
168 }
169 if ( useSubdirPerArtifact )
170 {
171 String artifactString = getDependencyId( artifact, removeVersion );
172 sb.append( artifactString ).append( File.separatorChar );
173 }
174 }
175 return new File( outputDirectory, sb.toString() );
176 }
177
178 private static String getDependencyId( Artifact artifact, boolean removeVersion )
179 {
180 StringBuilder sb = new StringBuilder();
181
182 sb.append( artifact.getArtifactId() );
183
184 if ( StringUtils.isNotEmpty( artifact.getClassifier() ) )
185 {
186 sb.append( "-" );
187 sb.append( artifact.getClassifier() );
188 }
189
190 if ( !removeVersion )
191 {
192 sb.append( "-" );
193 sb.append( artifact.getVersion() );
194 sb.append( "-" );
195 sb.append( artifact.getType() );
196 }
197 else
198 {
199
200
201
202 if ( !StringUtils.equals( artifact.getClassifier(), artifact.getType() ) )
203 {
204 sb.append( "-" );
205 sb.append( artifact.getType() );
206 }
207 }
208 return sb.toString();
209 }
210
211
212
213
214
215
216
217
218 public static synchronized void write( String string, File file, boolean append, Log log )
219 throws IOException
220 {
221 file.getParentFile().mkdirs();
222
223 FileWriter writer = null;
224
225 try
226 {
227 writer = new FileWriter( file, append );
228
229 writer.write( string );
230 }
231 finally
232 {
233 IOUtil.close( writer );
234 }
235 }
236
237
238
239
240
241
242
243 public static synchronized void log( String string, Log log )
244 throws IOException
245 {
246 BufferedReader reader = new BufferedReader( new StringReader( string ) );
247
248 String line;
249
250 while ( ( line = reader.readLine() ) != null )
251 {
252 log.info( line );
253 }
254
255 reader.close();
256 }
257
258
259
260
261 public static String[] tokenizer( String str )
262 {
263 return StringUtils.split( cleanToBeTokenizedString( str ), "," );
264 }
265
266
267
268
269 public static String cleanToBeTokenizedString( String str )
270 {
271 String ret = "";
272 if ( !StringUtils.isEmpty( str ) )
273 {
274
275 ret = str.trim().replaceAll( "[\\s]*,[\\s]*", "," );
276 }
277
278 return ret;
279 }
280 }