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.HashMap;
26 import java.util.HashSet;
27 import java.util.Iterator;
28 import java.util.LinkedHashSet;
29 import java.util.Map;
30
31 import org.apache.maven.artifact.Artifact;
32 import org.apache.maven.artifact.factory.ArtifactFactory;
33 import org.apache.maven.artifact.installer.ArtifactInstaller;
34 import org.apache.maven.artifact.repository.ArtifactRepository;
35 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
36 import org.apache.maven.model.Model;
37 import org.apache.maven.model.Parent;
38 import org.apache.maven.plugin.AbstractMojo;
39 import org.apache.maven.plugin.MojoExecutionException;
40 import org.apache.maven.project.MavenProject;
41 import org.codehaus.plexus.util.FileUtils;
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 public class InstallMojo
57 extends AbstractMojo
58 {
59
60
61
62
63
64
65 private ArtifactInstaller installer;
66
67
68
69
70
71
72 private ArtifactFactory artifactFactory;
73
74
75
76
77
78
79 private ArtifactRepositoryFactory repositoryFactory;
80
81
82
83
84
85
86 private ArtifactRepository localRepository;
87
88
89
90
91
92
93
94
95
96 private File localRepositoryPath;
97
98
99
100
101
102
103
104
105 private MavenProject project;
106
107
108
109
110
111
112
113 private Collection reactorProjects;
114
115
116
117
118
119
120
121
122 private boolean skipInstallation;
123
124
125
126
127 private Collection installedArtifacts;
128
129
130
131
132 private Collection copiedArtifacts;
133
134
135
136
137
138
139 public void execute()
140 throws MojoExecutionException
141 {
142 if ( skipInstallation )
143 {
144 getLog().info( "Skipping artifact installation per configuration." );
145 return;
146 }
147
148 ArtifactRepository testRepository = createTestRepository();
149
150 installedArtifacts = new HashSet();
151 copiedArtifacts = new HashSet();
152
153 installProjectDependencies( project, reactorProjects, testRepository );
154 installProjectParents( project, testRepository );
155 installProjectArtifacts( project, testRepository );
156 }
157
158
159
160
161
162
163
164
165
166
167 private ArtifactRepository createTestRepository()
168 throws MojoExecutionException
169 {
170 ArtifactRepository testRepository = localRepository;
171
172 if ( localRepositoryPath != null )
173 {
174 try
175 {
176 if ( !localRepositoryPath.exists() && !localRepositoryPath.mkdirs() )
177 {
178 throw new IOException( "Failed to create directory: " + localRepositoryPath );
179 }
180
181 testRepository =
182 repositoryFactory.createArtifactRepository( localRepository.getId(),
183 localRepositoryPath.toURL().toExternalForm(),
184 localRepository.getLayout(),
185 localRepository.getSnapshots(),
186 localRepository.getReleases() );
187 }
188 catch ( Exception e )
189 {
190 throw new MojoExecutionException( "Failed to create local repository: " + localRepositoryPath, e );
191 }
192 }
193
194 return testRepository;
195 }
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210 private void installArtifact( File file, Artifact artifact, ArtifactRepository testRepository )
211 throws MojoExecutionException
212 {
213 try
214 {
215 if ( file == null )
216 {
217 throw new IllegalStateException( "Artifact has no associated file: " + file );
218 }
219 if ( !file.isFile() )
220 {
221 throw new IllegalStateException( "Artifact is not fully assembled: " + file );
222 }
223
224 if ( installedArtifacts.add( artifact.getId() ) )
225 {
226 installer.install( file, artifact, testRepository );
227 }
228 else
229 {
230 getLog().debug( "Not re-installing " + artifact + ", " + file );
231 }
232 }
233 catch ( Exception e )
234 {
235 throw new MojoExecutionException( "Failed to install artifact: " + artifact, e );
236 }
237 }
238
239
240
241
242
243
244
245
246
247
248
249
250
251 private void copyArtifact( File file, Artifact artifact, ArtifactRepository testRepository )
252 throws MojoExecutionException
253 {
254 try
255 {
256 if ( file == null )
257 {
258 throw new IllegalStateException( "Artifact has no associated file: " + file );
259 }
260 if ( !file.isFile() )
261 {
262 throw new IllegalStateException( "Artifact is not fully assembled: " + file );
263 }
264
265 if ( copiedArtifacts.add( artifact.getId() ) )
266 {
267 File destination = new File( testRepository.getBasedir(), testRepository.pathOf( artifact ) );
268
269 getLog().debug( "Installing " + file + " to " + destination );
270
271 copyFileIfDifferent( file, destination );
272
273 MetadataUtils.createMetadata( destination, artifact );
274 }
275 else
276 {
277 getLog().debug( "Not re-installing " + artifact + ", " + file );
278 }
279 }
280 catch ( Exception e )
281 {
282 throw new MojoExecutionException( "Failed to stage artifact: " + artifact, e );
283 }
284 }
285
286 private void copyFileIfDifferent( File src, File dst )
287 throws IOException
288 {
289 if ( src.lastModified() != dst.lastModified() || src.length() != dst.length() )
290 {
291 FileUtils.copyFile( src, dst );
292 dst.setLastModified( src.lastModified() );
293 }
294 }
295
296
297
298
299
300
301
302
303 private void installProjectArtifacts( MavenProject mvnProject, ArtifactRepository testRepository )
304 throws MojoExecutionException
305 {
306 try
307 {
308
309 installProjectPom( mvnProject, testRepository );
310
311
312 Artifact mainArtifact = mvnProject.getArtifact();
313 if ( mainArtifact.getFile() != null )
314 {
315 installArtifact( mainArtifact.getFile(), mainArtifact, testRepository );
316 }
317
318
319 Collection attachedArtifacts = mvnProject.getAttachedArtifacts();
320 for ( Iterator artifactIter = attachedArtifacts.iterator(); artifactIter.hasNext(); )
321 {
322 Artifact attachedArtifact = (Artifact) artifactIter.next();
323 installArtifact( attachedArtifact.getFile(), attachedArtifact, testRepository );
324 }
325 }
326 catch ( Exception e )
327 {
328 throw new MojoExecutionException( "Failed to install project artifacts: " + mvnProject, e );
329 }
330 }
331
332
333
334
335
336
337
338
339
340 private void installProjectParents( MavenProject mvnProject, ArtifactRepository testRepository )
341 throws MojoExecutionException
342 {
343 try
344 {
345 for ( MavenProject parent = mvnProject.getParent(); parent != null; parent = parent.getParent() )
346 {
347 if ( parent.getFile() == null )
348 {
349 copyParentPoms( parent.getGroupId(), parent.getArtifactId(), parent.getVersion(), testRepository );
350 break;
351 }
352 installProjectPom( parent, testRepository );
353 }
354 }
355 catch ( Exception e )
356 {
357 throw new MojoExecutionException( "Failed to install project parents: " + mvnProject, e );
358 }
359 }
360
361
362
363
364
365
366
367
368 private void installProjectPom( MavenProject mvnProject, ArtifactRepository testRepository )
369 throws MojoExecutionException
370 {
371 try
372 {
373 Artifact pomArtifact = null;
374 if ( "pom".equals( mvnProject.getPackaging() ) )
375 {
376 pomArtifact = mvnProject.getArtifact();
377 }
378 if ( pomArtifact == null )
379 {
380 pomArtifact =
381 artifactFactory.createProjectArtifact( mvnProject.getGroupId(), mvnProject.getArtifactId(),
382 mvnProject.getVersion() );
383 }
384 installArtifact( mvnProject.getFile(), pomArtifact, testRepository );
385 }
386 catch ( Exception e )
387 {
388 throw new MojoExecutionException( "Failed to install POM: " + mvnProject, e );
389 }
390 }
391
392
393
394
395
396
397
398
399
400
401 private void installProjectDependencies( MavenProject mvnProject, Collection reactorProjects,
402 ArtifactRepository testRepository )
403 throws MojoExecutionException
404 {
405
406 Map projects = new HashMap();
407 for ( Iterator it = reactorProjects.iterator(); it.hasNext(); )
408 {
409 MavenProject reactorProject = (MavenProject) it.next();
410
411 String projectId =
412 reactorProject.getGroupId() + ':' + reactorProject.getArtifactId() + ':' + reactorProject.getVersion();
413
414 projects.put( projectId, reactorProject );
415 }
416
417
418 Collection artifacts = mvnProject.getArtifacts();
419
420 Collection dependencyProjects = new LinkedHashSet();
421
422 Collection dependencyArtifacts = new LinkedHashSet();
423 for ( Iterator it = artifacts.iterator(); it.hasNext(); )
424 {
425 Artifact artifact = (Artifact) it.next();
426
427
428 artifact.isSnapshot();
429
430 String projectId = artifact.getGroupId() + ':' + artifact.getArtifactId() + ':' + artifact.getBaseVersion();
431
432 if ( projects.containsKey( projectId ) )
433 {
434 dependencyProjects.add( projectId );
435 }
436 else
437 {
438 dependencyArtifacts.add( artifact );
439 }
440 }
441
442
443 try
444 {
445
446 for ( Iterator it = dependencyArtifacts.iterator(); it.hasNext(); )
447 {
448 Artifact artifact = (Artifact) it.next();
449
450 Artifact depArtifact =
451 artifactFactory.createArtifactWithClassifier( artifact.getGroupId(), artifact.getArtifactId(),
452 artifact.getBaseVersion(), artifact.getType(),
453 artifact.getClassifier() );
454
455 File artifactFile = artifact.getFile();
456
457 Artifact pomArtifact =
458 artifactFactory.createProjectArtifact( depArtifact.getGroupId(), depArtifact.getArtifactId(),
459 depArtifact.getBaseVersion() );
460
461 File pomFile = new File( localRepository.getBasedir(), localRepository.pathOf( pomArtifact ) );
462
463 if ( pomFile.isFile() )
464 {
465 if ( !pomArtifact.getId().equals( depArtifact.getId() ) )
466 {
467 copyArtifact( pomFile, pomArtifact, testRepository );
468 }
469 copyParentPoms( pomFile, testRepository );
470 }
471
472 copyArtifact( artifactFile, depArtifact, testRepository );
473 }
474
475
476 for ( Iterator it = dependencyProjects.iterator(); it.hasNext(); )
477 {
478 String projectId = (String) it.next();
479
480 MavenProject dependencyProject = (MavenProject) projects.get( projectId );
481
482 installProjectArtifacts( dependencyProject, testRepository );
483 installProjectParents( dependencyProject, testRepository );
484 }
485 }
486 catch ( Exception e )
487 {
488 throw new MojoExecutionException( "Failed to install project dependencies: " + mvnProject, e );
489 }
490 }
491
492
493
494
495
496
497
498
499 private void copyParentPoms( File pomFile, ArtifactRepository testRepository )
500 throws MojoExecutionException
501 {
502 Model model = PomUtils.loadPom( pomFile );
503 Parent parent = model.getParent();
504 if ( parent != null )
505 {
506 copyParentPoms( parent.getGroupId(), parent.getArtifactId(), parent.getVersion(), testRepository );
507 }
508 }
509
510
511
512
513
514
515
516
517
518
519 private void copyParentPoms( String groupId, String artifactId, String version, ArtifactRepository testRepository )
520 throws MojoExecutionException
521 {
522 Artifact pomArtifact = artifactFactory.createProjectArtifact( groupId, artifactId, version );
523
524 if ( installedArtifacts.contains( pomArtifact.getId() ) || copiedArtifacts.contains( pomArtifact.getId() ) )
525 {
526 getLog().debug( "Not re-installing " + pomArtifact );
527 return;
528 }
529
530 File pomFile = new File( localRepository.getBasedir(), localRepository.pathOf( pomArtifact ) );
531 if ( pomFile.isFile() )
532 {
533 copyArtifact( pomFile, pomArtifact, testRepository );
534 copyParentPoms( pomFile, testRepository );
535 }
536 }
537
538 }