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