1 | |
package org.apache.maven.plugin.invoker; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.File; |
23 | |
import java.io.IOException; |
24 | |
import java.util.Collection; |
25 | |
import java.util.Collections; |
26 | |
import java.util.HashMap; |
27 | |
import java.util.HashSet; |
28 | |
import java.util.LinkedHashSet; |
29 | |
import java.util.List; |
30 | |
import java.util.Map; |
31 | |
import java.util.Set; |
32 | |
|
33 | |
import org.apache.maven.artifact.Artifact; |
34 | |
import org.apache.maven.artifact.factory.ArtifactFactory; |
35 | |
import org.apache.maven.artifact.installer.ArtifactInstaller; |
36 | |
import org.apache.maven.artifact.metadata.ArtifactMetadataSource; |
37 | |
import org.apache.maven.artifact.repository.ArtifactRepository; |
38 | |
import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; |
39 | |
import org.apache.maven.artifact.resolver.ArtifactResolutionResult; |
40 | |
import org.apache.maven.artifact.resolver.ArtifactResolver; |
41 | |
import org.apache.maven.model.Model; |
42 | |
import org.apache.maven.model.Parent; |
43 | |
import org.apache.maven.plugin.AbstractMojo; |
44 | |
import org.apache.maven.plugin.MojoExecutionException; |
45 | |
import org.apache.maven.plugins.annotations.Component; |
46 | |
import org.apache.maven.plugins.annotations.LifecyclePhase; |
47 | |
import org.apache.maven.plugins.annotations.Mojo; |
48 | |
import org.apache.maven.plugins.annotations.Parameter; |
49 | |
import org.apache.maven.plugins.annotations.ResolutionScope; |
50 | |
import org.apache.maven.project.MavenProject; |
51 | |
import org.codehaus.plexus.util.FileUtils; |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
@Mojo( name = "install", defaultPhase = LifecyclePhase.PRE_INTEGRATION_TEST, |
64 | |
requiresDependencyResolution = ResolutionScope.RUNTIME, threadSafe = true ) |
65 | 0 | public class InstallMojo |
66 | |
extends AbstractMojo |
67 | |
{ |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
@Component |
73 | |
private ArtifactInstaller installer; |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
@Component |
79 | |
private ArtifactFactory artifactFactory; |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
@Component |
85 | |
private ArtifactRepositoryFactory repositoryFactory; |
86 | |
|
87 | |
|
88 | |
|
89 | |
@Parameter( property = "localRepository", required = true, readonly = true ) |
90 | |
private ArtifactRepository localRepository; |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
@Parameter( property = "invoker.localRepositoryPath" ) |
99 | |
private File localRepositoryPath; |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
@Component |
105 | |
private MavenProject project; |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
@Parameter( defaultValue = "${reactorProjects}", readonly = true ) |
111 | |
private Collection<MavenProject> reactorProjects; |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
@Parameter( property = "invoker.skip", defaultValue = "false" ) |
120 | |
private boolean skipInstallation; |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
private Collection<String> installedArtifacts; |
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
private Collection<String> copiedArtifacts; |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
@Parameter |
153 | |
private String[] extraArtifacts; |
154 | |
|
155 | |
|
156 | |
|
157 | |
@Component |
158 | |
private ArtifactResolver resolver; |
159 | |
|
160 | |
|
161 | |
|
162 | |
@Parameter( defaultValue = "${project.remoteArtifactRepositories}", readonly = true ) |
163 | |
private List<ArtifactRepository> remoteArtifactRepositories; |
164 | |
|
165 | |
|
166 | |
|
167 | |
@Parameter( defaultValue = "${project.pluginArtifactRepositories}", readonly = true ) |
168 | |
private List<ArtifactRepository> remotePluginRepositories; |
169 | |
|
170 | |
|
171 | |
|
172 | |
@Component |
173 | |
private ArtifactMetadataSource artifactMetadataSource; |
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
public void execute() |
181 | |
throws MojoExecutionException |
182 | |
{ |
183 | 0 | if ( skipInstallation ) |
184 | |
{ |
185 | 0 | getLog().info( "Skipping artifact installation per configuration." ); |
186 | 0 | return; |
187 | |
} |
188 | |
|
189 | 0 | ArtifactRepository testRepository = createTestRepository(); |
190 | |
|
191 | 0 | installedArtifacts = new HashSet<String>(); |
192 | 0 | copiedArtifacts = new HashSet<String>(); |
193 | |
|
194 | 0 | installProjectDependencies( project, reactorProjects, testRepository ); |
195 | 0 | installProjectParents( project, testRepository ); |
196 | 0 | installProjectArtifacts( project, testRepository ); |
197 | |
|
198 | 0 | installExtraArtifacts( testRepository, extraArtifacts ); |
199 | 0 | } |
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
private ArtifactRepository createTestRepository() |
211 | |
throws MojoExecutionException |
212 | |
{ |
213 | 0 | ArtifactRepository testRepository = localRepository; |
214 | |
|
215 | 0 | if ( localRepositoryPath != null ) |
216 | |
{ |
217 | |
try |
218 | |
{ |
219 | 0 | if ( !localRepositoryPath.exists() && !localRepositoryPath.mkdirs() ) |
220 | |
{ |
221 | 0 | throw new IOException( "Failed to create directory: " + localRepositoryPath ); |
222 | |
} |
223 | |
|
224 | 0 | testRepository = |
225 | |
repositoryFactory.createArtifactRepository( localRepository.getId(), |
226 | |
localRepositoryPath.toURL().toExternalForm(), |
227 | |
localRepository.getLayout(), |
228 | |
localRepository.getSnapshots(), |
229 | |
localRepository.getReleases() ); |
230 | |
} |
231 | 0 | catch ( Exception e ) |
232 | |
{ |
233 | 0 | throw new MojoExecutionException( "Failed to create local repository: " + localRepositoryPath, e ); |
234 | 0 | } |
235 | |
} |
236 | |
|
237 | 0 | return testRepository; |
238 | |
} |
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
private void installArtifact( File file, Artifact artifact, ArtifactRepository testRepository ) |
254 | |
throws MojoExecutionException |
255 | |
{ |
256 | |
try |
257 | |
{ |
258 | 0 | if ( file == null ) |
259 | |
{ |
260 | 0 | throw new IllegalStateException( "Artifact has no associated file: " + file ); |
261 | |
} |
262 | 0 | if ( !file.isFile() ) |
263 | |
{ |
264 | 0 | throw new IllegalStateException( "Artifact is not fully assembled: " + file ); |
265 | |
} |
266 | |
|
267 | 0 | if ( installedArtifacts.add( artifact.getId() ) ) |
268 | |
{ |
269 | 0 | installer.install( file, artifact, testRepository ); |
270 | |
} |
271 | |
else |
272 | |
{ |
273 | 0 | getLog().debug( "Not re-installing " + artifact + ", " + file ); |
274 | |
} |
275 | |
} |
276 | 0 | catch ( Exception e ) |
277 | |
{ |
278 | 0 | throw new MojoExecutionException( "Failed to install artifact: " + artifact, e ); |
279 | 0 | } |
280 | 0 | } |
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
private void copyArtifact( File file, Artifact artifact, ArtifactRepository testRepository ) |
295 | |
throws MojoExecutionException |
296 | |
{ |
297 | |
try |
298 | |
{ |
299 | 0 | if ( file == null ) |
300 | |
{ |
301 | 0 | throw new IllegalStateException( "Artifact has no associated file: " + file ); |
302 | |
} |
303 | 0 | if ( !file.isFile() ) |
304 | |
{ |
305 | 0 | throw new IllegalStateException( "Artifact is not fully assembled: " + file ); |
306 | |
} |
307 | |
|
308 | 0 | if ( copiedArtifacts.add( artifact.getId() ) ) |
309 | |
{ |
310 | 0 | File destination = new File( testRepository.getBasedir(), testRepository.pathOf( artifact ) ); |
311 | |
|
312 | 0 | getLog().debug( "Installing " + file + " to " + destination ); |
313 | |
|
314 | 0 | copyFileIfDifferent( file, destination ); |
315 | |
|
316 | 0 | MetadataUtils.createMetadata( destination, artifact ); |
317 | 0 | } |
318 | |
else |
319 | |
{ |
320 | 0 | getLog().debug( "Not re-installing " + artifact + ", " + file ); |
321 | |
} |
322 | |
} |
323 | 0 | catch ( Exception e ) |
324 | |
{ |
325 | 0 | throw new MojoExecutionException( "Failed to stage artifact: " + artifact, e ); |
326 | 0 | } |
327 | 0 | } |
328 | |
|
329 | |
private void copyFileIfDifferent( File src, File dst ) |
330 | |
throws IOException |
331 | |
{ |
332 | 0 | if ( src.lastModified() != dst.lastModified() || src.length() != dst.length() ) |
333 | |
{ |
334 | 0 | FileUtils.copyFile( src, dst ); |
335 | 0 | dst.setLastModified( src.lastModified() ); |
336 | |
} |
337 | 0 | } |
338 | |
|
339 | |
|
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | |
private void installProjectArtifacts( MavenProject mvnProject, ArtifactRepository testRepository ) |
347 | |
throws MojoExecutionException |
348 | |
{ |
349 | |
try |
350 | |
{ |
351 | |
|
352 | 0 | installProjectPom( mvnProject, testRepository ); |
353 | |
|
354 | |
|
355 | 0 | Artifact mainArtifact = mvnProject.getArtifact(); |
356 | 0 | if ( mainArtifact.getFile() != null ) |
357 | |
{ |
358 | 0 | installArtifact( mainArtifact.getFile(), mainArtifact, testRepository ); |
359 | |
} |
360 | |
|
361 | |
|
362 | 0 | Collection<Artifact> attachedArtifacts = (Collection<Artifact>) mvnProject.getAttachedArtifacts(); |
363 | 0 | for ( Artifact attachedArtifact : attachedArtifacts ) |
364 | |
{ |
365 | 0 | installArtifact( attachedArtifact.getFile(), attachedArtifact, testRepository ); |
366 | |
} |
367 | |
} |
368 | 0 | catch ( Exception e ) |
369 | |
{ |
370 | 0 | throw new MojoExecutionException( "Failed to install project artifacts: " + mvnProject, e ); |
371 | 0 | } |
372 | 0 | } |
373 | |
|
374 | |
|
375 | |
|
376 | |
|
377 | |
|
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | |
private void installProjectParents( MavenProject mvnProject, ArtifactRepository testRepository ) |
383 | |
throws MojoExecutionException |
384 | |
{ |
385 | |
try |
386 | |
{ |
387 | 0 | for ( MavenProject parent = mvnProject.getParent(); parent != null; parent = parent.getParent() ) |
388 | |
{ |
389 | 0 | if ( parent.getFile() == null ) |
390 | |
{ |
391 | 0 | copyParentPoms( parent.getGroupId(), parent.getArtifactId(), parent.getVersion(), testRepository ); |
392 | 0 | break; |
393 | |
} |
394 | 0 | installProjectPom( parent, testRepository ); |
395 | |
} |
396 | |
} |
397 | 0 | catch ( Exception e ) |
398 | |
{ |
399 | 0 | throw new MojoExecutionException( "Failed to install project parents: " + mvnProject, e ); |
400 | 0 | } |
401 | 0 | } |
402 | |
|
403 | |
|
404 | |
|
405 | |
|
406 | |
|
407 | |
|
408 | |
|
409 | |
|
410 | |
private void installProjectPom( MavenProject mvnProject, ArtifactRepository testRepository ) |
411 | |
throws MojoExecutionException |
412 | |
{ |
413 | |
try |
414 | |
{ |
415 | 0 | Artifact pomArtifact = null; |
416 | 0 | if ( "pom".equals( mvnProject.getPackaging() ) ) |
417 | |
{ |
418 | 0 | pomArtifact = mvnProject.getArtifact(); |
419 | |
} |
420 | 0 | if ( pomArtifact == null ) |
421 | |
{ |
422 | 0 | pomArtifact = |
423 | |
artifactFactory.createProjectArtifact( mvnProject.getGroupId(), mvnProject.getArtifactId(), |
424 | |
mvnProject.getVersion() ); |
425 | |
} |
426 | 0 | installArtifact( mvnProject.getFile(), pomArtifact, testRepository ); |
427 | |
} |
428 | 0 | catch ( Exception e ) |
429 | |
{ |
430 | 0 | throw new MojoExecutionException( "Failed to install POM: " + mvnProject, e ); |
431 | 0 | } |
432 | 0 | } |
433 | |
|
434 | |
|
435 | |
|
436 | |
|
437 | |
|
438 | |
|
439 | |
|
440 | |
|
441 | |
|
442 | |
|
443 | |
private void installProjectDependencies( MavenProject mvnProject, Collection<MavenProject> reactorProjects, |
444 | |
ArtifactRepository testRepository ) |
445 | |
throws MojoExecutionException |
446 | |
{ |
447 | |
|
448 | 0 | Map<String, MavenProject> projects = new HashMap<String, MavenProject>(); |
449 | 0 | for ( MavenProject reactorProject : reactorProjects ) |
450 | |
{ |
451 | 0 | String projectId = |
452 | |
reactorProject.getGroupId() + ':' + reactorProject.getArtifactId() + ':' + reactorProject.getVersion(); |
453 | |
|
454 | 0 | projects.put( projectId, reactorProject ); |
455 | 0 | } |
456 | |
|
457 | |
|
458 | 0 | Collection<Artifact> artifacts = (Collection<Artifact>) mvnProject.getArtifacts(); |
459 | |
|
460 | 0 | Collection<String> dependencyProjects = new LinkedHashSet<String>(); |
461 | |
|
462 | 0 | Collection<Artifact> dependencyArtifacts = new LinkedHashSet<Artifact>(); |
463 | 0 | for ( Artifact artifact : artifacts ) |
464 | |
{ |
465 | |
|
466 | 0 | artifact.isSnapshot(); |
467 | |
|
468 | 0 | String projectId = artifact.getGroupId() + ':' + artifact.getArtifactId() + ':' + artifact.getBaseVersion(); |
469 | |
|
470 | 0 | if ( projects.containsKey( projectId ) ) |
471 | |
{ |
472 | 0 | dependencyProjects.add( projectId ); |
473 | |
} |
474 | |
else |
475 | |
{ |
476 | 0 | dependencyArtifacts.add( artifact ); |
477 | |
} |
478 | 0 | } |
479 | |
|
480 | |
|
481 | |
try |
482 | |
{ |
483 | |
|
484 | 0 | for ( Artifact artifact : dependencyArtifacts ) |
485 | |
{ |
486 | 0 | copyArtifact( artifact, testRepository ); |
487 | |
} |
488 | |
|
489 | |
|
490 | 0 | for ( String projectId : dependencyProjects ) |
491 | |
{ |
492 | 0 | MavenProject dependencyProject = projects.get( projectId ); |
493 | |
|
494 | 0 | installProjectArtifacts( dependencyProject, testRepository ); |
495 | 0 | installProjectParents( dependencyProject, testRepository ); |
496 | 0 | } |
497 | |
} |
498 | 0 | catch ( Exception e ) |
499 | |
{ |
500 | 0 | throw new MojoExecutionException( "Failed to install project dependencies: " + mvnProject, e ); |
501 | 0 | } |
502 | 0 | } |
503 | |
|
504 | |
private void copyArtifact( Artifact artifact, ArtifactRepository testRepository ) |
505 | |
throws MojoExecutionException |
506 | |
{ |
507 | 0 | copyPoms( artifact, testRepository ); |
508 | |
|
509 | 0 | Artifact depArtifact = |
510 | |
artifactFactory.createArtifactWithClassifier( artifact.getGroupId(), artifact.getArtifactId(), |
511 | |
artifact.getBaseVersion(), artifact.getType(), |
512 | |
artifact.getClassifier() ); |
513 | |
|
514 | 0 | File artifactFile = artifact.getFile(); |
515 | |
|
516 | 0 | copyArtifact( artifactFile, depArtifact, testRepository ); |
517 | 0 | } |
518 | |
|
519 | |
private void copyPoms( Artifact artifact, ArtifactRepository testRepository ) |
520 | |
throws MojoExecutionException |
521 | |
{ |
522 | 0 | Artifact pomArtifact = |
523 | |
artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(), |
524 | |
artifact.getBaseVersion() ); |
525 | |
|
526 | 0 | File pomFile = new File( localRepository.getBasedir(), localRepository.pathOf( pomArtifact ) ); |
527 | |
|
528 | 0 | if ( pomFile.isFile() ) |
529 | |
{ |
530 | 0 | copyArtifact( pomFile, pomArtifact, testRepository ); |
531 | 0 | copyParentPoms( pomFile, testRepository ); |
532 | |
} |
533 | 0 | } |
534 | |
|
535 | |
|
536 | |
|
537 | |
|
538 | |
|
539 | |
|
540 | |
|
541 | |
|
542 | |
private void copyParentPoms( File pomFile, ArtifactRepository testRepository ) |
543 | |
throws MojoExecutionException |
544 | |
{ |
545 | 0 | Model model = PomUtils.loadPom( pomFile ); |
546 | 0 | Parent parent = model.getParent(); |
547 | 0 | if ( parent != null ) |
548 | |
{ |
549 | 0 | copyParentPoms( parent.getGroupId(), parent.getArtifactId(), parent.getVersion(), testRepository ); |
550 | |
} |
551 | 0 | } |
552 | |
|
553 | |
|
554 | |
|
555 | |
|
556 | |
|
557 | |
|
558 | |
|
559 | |
|
560 | |
|
561 | |
|
562 | |
private void copyParentPoms( String groupId, String artifactId, String version, ArtifactRepository testRepository ) |
563 | |
throws MojoExecutionException |
564 | |
{ |
565 | 0 | Artifact pomArtifact = artifactFactory.createProjectArtifact( groupId, artifactId, version ); |
566 | |
|
567 | 0 | if ( installedArtifacts.contains( pomArtifact.getId() ) || copiedArtifacts.contains( pomArtifact.getId() ) ) |
568 | |
{ |
569 | 0 | getLog().debug( "Not re-installing " + pomArtifact ); |
570 | 0 | return; |
571 | |
} |
572 | |
|
573 | 0 | File pomFile = new File( localRepository.getBasedir(), localRepository.pathOf( pomArtifact ) ); |
574 | 0 | if ( pomFile.isFile() ) |
575 | |
{ |
576 | 0 | copyArtifact( pomFile, pomArtifact, testRepository ); |
577 | 0 | copyParentPoms( pomFile, testRepository ); |
578 | |
} |
579 | 0 | } |
580 | |
|
581 | |
private void installExtraArtifacts( ArtifactRepository testRepository, String[] extraArtifacts ) |
582 | |
throws MojoExecutionException |
583 | |
{ |
584 | 0 | if ( extraArtifacts == null ) |
585 | |
{ |
586 | 0 | return; |
587 | |
} |
588 | |
|
589 | 0 | Artifact originatingArtifact = project.getArtifact(); |
590 | |
|
591 | 0 | for ( int i = 0; i < extraArtifacts.length; i++ ) |
592 | |
{ |
593 | 0 | String[] gav = extraArtifacts[i].split( ":" ); |
594 | 0 | if ( gav.length < 3 || gav.length > 5 ) |
595 | |
{ |
596 | 0 | throw new MojoExecutionException( "Invalid artifact " + extraArtifacts[i] ); |
597 | |
} |
598 | |
|
599 | 0 | String groupId = gav[0]; |
600 | 0 | String artifactId = gav[1]; |
601 | 0 | String version = gav[2]; |
602 | |
|
603 | 0 | String type = "jar"; |
604 | 0 | if ( gav.length > 3 ) |
605 | |
{ |
606 | 0 | type = gav[3]; |
607 | |
} |
608 | |
|
609 | 0 | String classifier = null; |
610 | 0 | if ( gav.length == 5 ) |
611 | |
{ |
612 | 0 | classifier = gav[4]; |
613 | |
} |
614 | |
|
615 | |
List<ArtifactRepository> remoteRepositories; |
616 | 0 | if ( "maven-plugin".equals( type ) ) |
617 | |
{ |
618 | 0 | remoteRepositories = this.remotePluginRepositories; |
619 | |
} |
620 | |
else |
621 | |
{ |
622 | 0 | remoteRepositories = this.remoteArtifactRepositories; |
623 | |
} |
624 | |
|
625 | 0 | Artifact artifact = null; |
626 | |
try |
627 | |
{ |
628 | 0 | artifact = artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, type, classifier ); |
629 | |
|
630 | 0 | ArtifactResolutionResult arr = |
631 | |
resolver.resolveTransitively( Collections.singleton( artifact ), originatingArtifact, |
632 | |
remoteRepositories, localRepository, artifactMetadataSource ); |
633 | |
|
634 | 0 | if ( !groupId.equals( artifact.getGroupId() ) || !artifactId.equals( artifact.getArtifactId() ) |
635 | |
|| !version.equals( artifact.getVersion() ) ) |
636 | |
{ |
637 | 0 | artifact = |
638 | |
artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, type, classifier ); |
639 | 0 | copyPoms( artifact, testRepository ); |
640 | |
} |
641 | |
|
642 | 0 | for ( Artifact arrArtifact : (Set<Artifact>) arr.getArtifacts() ) |
643 | |
{ |
644 | 0 | copyArtifact( arrArtifact, testRepository ); |
645 | |
} |
646 | |
} |
647 | 0 | catch ( Exception e ) |
648 | |
{ |
649 | 0 | throw new MojoExecutionException( "Unable to resolve dependencies for: " + artifact, e ); |
650 | 0 | } |
651 | |
} |
652 | 0 | } |
653 | |
|
654 | |
} |