1 | |
package org.apache.maven.plugin.dependency; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.maven.artifact.Artifact; |
23 | |
import org.apache.maven.plugin.MojoExecutionException; |
24 | |
import org.apache.maven.plugin.dependency.utils.DependencyUtil; |
25 | |
import org.apache.maven.plugins.annotations.Component; |
26 | |
import org.apache.maven.plugins.annotations.LifecyclePhase; |
27 | |
import org.apache.maven.plugins.annotations.Mojo; |
28 | |
import org.apache.maven.plugins.annotations.Parameter; |
29 | |
import org.apache.maven.plugins.annotations.ResolutionScope; |
30 | |
import org.apache.maven.project.MavenProjectHelper; |
31 | |
import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter; |
32 | |
import org.codehaus.plexus.util.IOUtil; |
33 | |
import org.codehaus.plexus.util.StringUtils; |
34 | |
|
35 | |
import java.io.BufferedReader; |
36 | |
import java.io.BufferedWriter; |
37 | |
import java.io.File; |
38 | |
import java.io.FileReader; |
39 | |
import java.io.FileWriter; |
40 | |
import java.io.IOException; |
41 | |
import java.io.Writer; |
42 | |
import java.util.ArrayList; |
43 | |
import java.util.Comparator; |
44 | |
import java.util.Iterator; |
45 | |
import java.util.List; |
46 | |
import java.util.Set; |
47 | |
import java.util.regex.Matcher; |
48 | |
import java.util.regex.Pattern; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | 0 | @Mojo( name = "build-classpath", requiresDependencyResolution = ResolutionScope.TEST, |
58 | |
defaultPhase = LifecyclePhase.GENERATE_SOURCES, threadSafe = true ) |
59 | 2 | public class BuildClasspathMojo |
60 | |
extends AbstractDependencyFilterMojo |
61 | |
implements Comparator<Artifact> |
62 | |
{ |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | 2 | @Parameter( property = "mdep.stripVersion", defaultValue = "false" ) |
68 | |
private boolean stripVersion = false; |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
@Parameter( property = "mdep.prefix" ) |
75 | |
private String prefix; |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
@Parameter( property = "mdep.cpFile" ) |
85 | |
private File cpFile; |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
@Parameter( property = "mdep.outputFile" ) |
91 | |
private File outputFile; |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
@Parameter( property = "mdep.regenerateFile", defaultValue = "false" ) |
97 | |
private boolean regenerateFile; |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
@Parameter( property = "mdep.fileSeparator", defaultValue = "" ) |
107 | |
private String fileSeparator; |
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
@Parameter( property = "mdep.pathSeparator", defaultValue = "" ) |
118 | |
private String pathSeparator; |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
@Parameter( property = "mdep.localRepoProperty", defaultValue = "" ) |
127 | |
private String localRepoProperty; |
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
@Parameter( defaultValue = "false" ) |
135 | |
boolean attach; |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
@Parameter( property = "mdep.outputFilterFile", defaultValue = "false" ) |
143 | |
boolean outputFilterFile; |
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | 2 | @Parameter( property = "mdep.useBaseVersion", defaultValue = "true" ) |
151 | |
protected boolean useBaseVersion = true; |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
@Component |
157 | |
private MavenProjectHelper projectHelper; |
158 | |
|
159 | 2 | boolean isFileSepSet = true; |
160 | |
|
161 | 2 | boolean isPathSepSet = true; |
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
public void execute() |
171 | |
throws MojoExecutionException |
172 | |
{ |
173 | |
|
174 | 3 | if ( cpFile != null ) |
175 | |
{ |
176 | 0 | getLog().warn( "The parameter cpFile is deprecated. Use outputFile instead." ); |
177 | 0 | this.outputFile = cpFile; |
178 | |
} |
179 | |
|
180 | |
|
181 | 3 | isFileSepSet = StringUtils.isNotEmpty( fileSeparator ); |
182 | 3 | isPathSepSet = StringUtils.isNotEmpty( pathSeparator ); |
183 | |
|
184 | |
|
185 | 3 | if ( attach && StringUtils.isEmpty( localRepoProperty ) ) |
186 | |
{ |
187 | 0 | localRepoProperty = "${M2_REPO}"; |
188 | |
} |
189 | |
|
190 | 3 | Set<Artifact> artifacts = getResolvedDependencies( true ); |
191 | |
|
192 | 3 | if ( artifacts == null || artifacts.isEmpty() ) |
193 | |
{ |
194 | 0 | getLog().info( "No dependencies found." ); |
195 | |
} |
196 | |
|
197 | 3 | List<Artifact> artList = new ArrayList<Artifact>( artifacts ); |
198 | |
|
199 | 3 | StringBuilder sb = new StringBuilder(); |
200 | 3 | Iterator<Artifact> i = artList.iterator(); |
201 | |
|
202 | 3 | if ( i.hasNext() ) |
203 | |
{ |
204 | 3 | appendArtifactPath( i.next(), sb ); |
205 | |
|
206 | 21 | while ( i.hasNext() ) |
207 | |
{ |
208 | 18 | sb.append( isPathSepSet ? this.pathSeparator : File.pathSeparator ); |
209 | 18 | appendArtifactPath( (Artifact) i.next(), sb ); |
210 | |
} |
211 | |
} |
212 | |
|
213 | 3 | String cpString = sb.toString(); |
214 | |
|
215 | |
|
216 | |
|
217 | 3 | if ( isFileSepSet ) |
218 | |
{ |
219 | |
|
220 | 1 | final String pattern = Pattern.quote( File.separator ); |
221 | 1 | final String replacement = Matcher.quoteReplacement( fileSeparator ); |
222 | 1 | cpString = cpString.replaceAll( pattern, replacement ); |
223 | |
} |
224 | |
|
225 | |
|
226 | 3 | if ( outputFilterFile ) |
227 | |
{ |
228 | 0 | cpString = "classpath=" + cpString; |
229 | |
} |
230 | |
|
231 | 3 | if ( outputFile == null ) |
232 | |
{ |
233 | 1 | getLog().info( "Dependencies classpath:\n" + cpString ); |
234 | |
} |
235 | |
else |
236 | |
{ |
237 | 2 | if ( regenerateFile || !isUpdToDate( cpString ) ) |
238 | |
{ |
239 | 2 | storeClasspathFile( cpString, outputFile ); |
240 | |
} |
241 | |
else |
242 | |
{ |
243 | 0 | this.getLog().info( "Skipped writing classpath file '" + outputFile + "'. No changes found." ); |
244 | |
} |
245 | |
} |
246 | 3 | if ( attach ) |
247 | |
{ |
248 | 0 | attachFile( cpString ); |
249 | |
} |
250 | 3 | } |
251 | |
|
252 | |
protected void attachFile( String cpString ) |
253 | |
throws MojoExecutionException |
254 | |
{ |
255 | 0 | File attachedFile = new File( project.getBuild().getDirectory(), "classpath" ); |
256 | 0 | storeClasspathFile( cpString, attachedFile ); |
257 | |
|
258 | 0 | projectHelper.attachArtifact( project, attachedFile, "classpath" ); |
259 | 0 | } |
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
protected void appendArtifactPath( Artifact art, StringBuilder sb ) |
268 | |
{ |
269 | 28 | if ( prefix == null ) |
270 | |
{ |
271 | 25 | String file = art.getFile().getPath(); |
272 | |
|
273 | 25 | if ( StringUtils.isNotEmpty( localRepoProperty ) ) |
274 | |
{ |
275 | 3 | file = StringUtils.replace( file, getLocal().getBasedir(), localRepoProperty ); |
276 | |
} |
277 | 25 | sb.append( file ); |
278 | 25 | } |
279 | |
else |
280 | |
{ |
281 | |
|
282 | 3 | sb.append( prefix ); |
283 | 3 | sb.append( File.separator ); |
284 | 3 | sb.append( DependencyUtil.getFormattedFileName( art, this.stripVersion, this.prependGroupId, this.useBaseVersion ) ); |
285 | |
} |
286 | 28 | } |
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
private boolean isUpdToDate( String cpString ) |
296 | |
{ |
297 | |
try |
298 | |
{ |
299 | 2 | String oldCp = readClasspathFile(); |
300 | 2 | return ( cpString == oldCp || ( cpString != null && cpString.equals( oldCp ) ) ); |
301 | |
} |
302 | 0 | catch ( Exception ex ) |
303 | |
{ |
304 | 0 | this.getLog().warn( |
305 | |
"Error while reading old classpath file '" + outputFile + "' for up-to-date check: " + ex ); |
306 | |
|
307 | 0 | return false; |
308 | |
} |
309 | |
} |
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
private void storeClasspathFile( String cpString, File out ) |
318 | |
throws MojoExecutionException |
319 | |
{ |
320 | |
|
321 | 2 | out.getParentFile().mkdirs(); |
322 | |
|
323 | 2 | Writer w = null; |
324 | |
try |
325 | |
{ |
326 | 2 | w = new BufferedWriter( new FileWriter( out ) ); |
327 | 2 | w.write( cpString ); |
328 | 2 | getLog().info( "Wrote classpath file '" + out + "'." ); |
329 | |
} |
330 | 0 | catch ( IOException ex ) |
331 | |
{ |
332 | 0 | throw new MojoExecutionException( "Error while writting to classpath file '" + out + "': " + ex.toString(), |
333 | |
ex ); |
334 | |
} |
335 | |
finally |
336 | |
{ |
337 | 2 | IOUtil.close( w ); |
338 | 2 | } |
339 | 2 | } |
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | |
|
347 | |
|
348 | |
protected String readClasspathFile() |
349 | |
throws IOException |
350 | |
{ |
351 | 5 | if ( outputFile == null ) |
352 | |
{ |
353 | 1 | throw new IllegalArgumentException( |
354 | |
"The outputFile parameter cannot be null if the file is intended to be read." ); |
355 | |
} |
356 | |
|
357 | 4 | if ( !outputFile.isFile() ) |
358 | |
{ |
359 | 1 | return null; |
360 | |
} |
361 | 3 | StringBuilder sb = new StringBuilder(); |
362 | 3 | BufferedReader r = null; |
363 | |
|
364 | |
try |
365 | |
{ |
366 | 3 | r = new BufferedReader( new FileReader( outputFile ) ); |
367 | |
String l; |
368 | 6 | while ( ( l = r.readLine() ) != null ) |
369 | |
{ |
370 | 3 | sb.append( l ); |
371 | |
} |
372 | |
|
373 | 3 | return sb.toString(); |
374 | |
} |
375 | |
finally |
376 | |
{ |
377 | 3 | IOUtil.close( r ); |
378 | |
} |
379 | |
} |
380 | |
|
381 | |
|
382 | |
|
383 | |
|
384 | |
|
385 | |
|
386 | |
|
387 | |
|
388 | |
|
389 | |
|
390 | |
public int compare( Artifact art1, Artifact art2 ) |
391 | |
{ |
392 | 0 | if ( art1 == art2 ) |
393 | |
{ |
394 | 0 | return 0; |
395 | |
} |
396 | 0 | else if ( art1 == null ) |
397 | |
{ |
398 | 0 | return -1; |
399 | |
} |
400 | 0 | else if ( art2 == null ) |
401 | |
{ |
402 | 0 | return +1; |
403 | |
} |
404 | |
|
405 | 0 | String s1 = art1.getGroupId() + art1.getArtifactId() + art1.getVersion(); |
406 | 0 | String s2 = art2.getGroupId() + art2.getArtifactId() + art2.getVersion(); |
407 | |
|
408 | 0 | return s1.compareTo( s2 ); |
409 | |
} |
410 | |
|
411 | |
protected ArtifactsFilter getMarkedArtifactFilter() |
412 | |
{ |
413 | 3 | return null; |
414 | |
} |
415 | |
|
416 | |
|
417 | |
|
418 | |
|
419 | |
public File getCpFile() |
420 | |
{ |
421 | 0 | return this.outputFile; |
422 | |
} |
423 | |
|
424 | |
|
425 | |
|
426 | |
|
427 | |
public void setCpFile( File theCpFile ) |
428 | |
{ |
429 | 1 | this.outputFile = theCpFile; |
430 | 1 | } |
431 | |
|
432 | |
|
433 | |
|
434 | |
|
435 | |
public String getFileSeparator() |
436 | |
{ |
437 | 0 | return this.fileSeparator; |
438 | |
} |
439 | |
|
440 | |
|
441 | |
|
442 | |
|
443 | |
public void setFileSeparator( String theFileSeparator ) |
444 | |
{ |
445 | 1 | this.fileSeparator = theFileSeparator; |
446 | 1 | } |
447 | |
|
448 | |
|
449 | |
|
450 | |
|
451 | |
public String getPathSeparator() |
452 | |
{ |
453 | 0 | return this.pathSeparator; |
454 | |
} |
455 | |
|
456 | |
|
457 | |
|
458 | |
|
459 | |
public void setPathSeparator( String thePathSeparator ) |
460 | |
{ |
461 | 1 | this.pathSeparator = thePathSeparator; |
462 | 1 | } |
463 | |
|
464 | |
|
465 | |
|
466 | |
|
467 | |
public String getPrefix() |
468 | |
{ |
469 | 0 | return this.prefix; |
470 | |
} |
471 | |
|
472 | |
|
473 | |
|
474 | |
|
475 | |
public void setPrefix( String thePrefix ) |
476 | |
{ |
477 | 4 | this.prefix = thePrefix; |
478 | 4 | } |
479 | |
|
480 | |
|
481 | |
|
482 | |
|
483 | |
public boolean isRegenerateFile() |
484 | |
{ |
485 | 0 | return this.regenerateFile; |
486 | |
} |
487 | |
|
488 | |
|
489 | |
|
490 | |
|
491 | |
public void setRegenerateFile( boolean theRegenerateFile ) |
492 | |
{ |
493 | 0 | this.regenerateFile = theRegenerateFile; |
494 | 0 | } |
495 | |
|
496 | |
|
497 | |
|
498 | |
|
499 | |
public boolean isStripVersion() |
500 | |
{ |
501 | 0 | return this.stripVersion; |
502 | |
} |
503 | |
|
504 | |
|
505 | |
|
506 | |
|
507 | |
public void setStripVersion( boolean theStripVersion ) |
508 | |
{ |
509 | 2 | this.stripVersion = theStripVersion; |
510 | 2 | } |
511 | |
|
512 | |
public String getLocalRepoProperty() |
513 | |
{ |
514 | 0 | return localRepoProperty; |
515 | |
} |
516 | |
|
517 | |
public void setLocalRepoProperty( String localRepoProperty ) |
518 | |
{ |
519 | 5 | this.localRepoProperty = localRepoProperty; |
520 | 5 | } |
521 | |
|
522 | |
public boolean isFileSepSet() |
523 | |
{ |
524 | 0 | return isFileSepSet; |
525 | |
} |
526 | |
|
527 | |
public void setFileSepSet( boolean isFileSepSet ) |
528 | |
{ |
529 | 0 | this.isFileSepSet = isFileSepSet; |
530 | 0 | } |
531 | |
|
532 | |
public boolean isPathSepSet() |
533 | |
{ |
534 | 0 | return isPathSepSet; |
535 | |
} |
536 | |
|
537 | |
public void setPathSepSet( boolean isPathSepSet ) |
538 | |
{ |
539 | 0 | this.isPathSepSet = isPathSepSet; |
540 | 0 | } |
541 | |
} |