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