1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugin.ide;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Set;
31 import java.util.TreeSet;
32 import java.util.jar.Attributes;
33 import java.util.jar.JarFile;
34 import java.util.jar.Manifest;
35 import java.util.zip.ZipFile;
36
37 import org.apache.maven.artifact.Artifact;
38 import org.apache.maven.artifact.factory.ArtifactFactory;
39 import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
40 import org.apache.maven.artifact.repository.ArtifactRepository;
41 import org.apache.maven.artifact.resolver.ArtifactCollector;
42 import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
43 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
44 import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
45 import org.apache.maven.artifact.resolver.ArtifactResolver;
46 import org.apache.maven.artifact.resolver.DebugResolutionListener;
47 import org.apache.maven.artifact.resolver.ResolutionNode;
48 import org.apache.maven.artifact.resolver.WarningResolutionListener;
49 import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
50 import org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter;
51 import org.apache.maven.artifact.versioning.ArtifactVersion;
52 import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
53 import org.apache.maven.artifact.versioning.VersionRange;
54 import org.apache.maven.execution.RuntimeInformation;
55 import org.apache.maven.model.Dependency;
56 import org.apache.maven.model.DependencyManagement;
57 import org.apache.maven.model.Exclusion;
58 import org.apache.maven.plugin.AbstractMojo;
59 import org.apache.maven.plugin.MojoExecutionException;
60 import org.apache.maven.plugin.MojoFailureException;
61 import org.apache.maven.plugin.eclipse.Constants;
62 import org.apache.maven.plugin.eclipse.Messages;
63 import org.apache.maven.project.MavenProject;
64 import org.codehaus.plexus.logging.LogEnabled;
65 import org.codehaus.plexus.logging.Logger;
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84 public abstract class AbstractIdeSupportMojo
85 extends AbstractMojo
86 implements LogEnabled
87 {
88
89
90
91
92
93
94
95
96 protected MavenProject project;
97
98
99
100
101
102
103
104 protected MavenProject executedProject;
105
106
107
108
109
110
111 protected String packaging;
112
113
114
115
116
117
118
119
120 protected ArtifactFactory artifactFactory;
121
122
123
124
125
126
127
128
129 protected ArtifactResolver artifactResolver;
130
131
132
133
134
135
136
137
138 protected ArtifactCollector artifactCollector;
139
140
141
142
143 protected ArtifactMetadataSource artifactMetadataSource;
144
145
146
147
148
149
150 private RuntimeInformation runtimeInformation;
151
152
153
154
155
156
157
158
159 protected List remoteArtifactRepositories;
160
161
162
163
164
165
166
167
168 protected ArtifactRepository localRepository;
169
170
171
172
173
174
175
176
177 protected List reactorProjects;
178
179
180
181
182
183
184 private boolean skip;
185
186
187
188
189
190
191
192
193
194 protected boolean downloadSources;
195
196
197
198
199
200
201
202
203
204 protected boolean downloadJavadocs;
205
206
207
208
209
210
211
212
213
214 protected boolean forceRecheck;
215
216
217
218
219 protected Logger logger;
220
221
222
223
224
225
226 public ArtifactMetadataSource getArtifactMetadataSource()
227 {
228 return artifactMetadataSource;
229 }
230
231
232
233
234
235
236 public void setArtifactMetadataSource( ArtifactMetadataSource artifactMetadataSource )
237 {
238 this.artifactMetadataSource = artifactMetadataSource;
239 }
240
241
242
243
244
245
246 public MavenProject getProject()
247 {
248 return project;
249 }
250
251
252
253
254
255
256 public void setProject( MavenProject project )
257 {
258 this.project = project;
259 }
260
261
262
263
264
265
266 public List getReactorProjects()
267 {
268 return reactorProjects;
269 }
270
271
272
273
274
275
276 public void setReactorProjects( List reactorProjects )
277 {
278 this.reactorProjects = reactorProjects;
279 }
280
281
282
283
284
285
286 public List getRemoteArtifactRepositories()
287 {
288 return remoteArtifactRepositories;
289 }
290
291
292
293
294
295
296 public void setRemoteArtifactRepositories( List remoteArtifactRepositories )
297 {
298 this.remoteArtifactRepositories = remoteArtifactRepositories;
299 }
300
301
302
303
304
305
306 public ArtifactFactory getArtifactFactory()
307 {
308 return artifactFactory;
309 }
310
311
312
313
314
315
316 public void setArtifactFactory( ArtifactFactory artifactFactory )
317 {
318 this.artifactFactory = artifactFactory;
319 }
320
321
322
323
324
325
326 public ArtifactResolver getArtifactResolver()
327 {
328 return artifactResolver;
329 }
330
331
332
333
334
335
336 public void setArtifactResolver( ArtifactResolver artifactResolver )
337 {
338 this.artifactResolver = artifactResolver;
339 }
340
341
342
343
344
345
346 public MavenProject getExecutedProject()
347 {
348 return executedProject;
349 }
350
351
352
353
354
355
356 public void setExecutedProject( MavenProject executedProject )
357 {
358 this.executedProject = executedProject;
359 }
360
361
362
363
364
365
366 public ArtifactRepository getLocalRepository()
367 {
368 return localRepository;
369 }
370
371
372
373
374
375
376 public void setLocalRepository( ArtifactRepository localRepository )
377 {
378 this.localRepository = localRepository;
379 }
380
381
382
383
384
385
386 public boolean getDownloadJavadocs()
387 {
388 return downloadJavadocs;
389 }
390
391
392
393
394
395
396 public void setDownloadJavadocs( boolean downloadJavadoc )
397 {
398 downloadJavadocs = downloadJavadoc;
399 }
400
401
402
403
404
405
406 public boolean getDownloadSources()
407 {
408 return downloadSources;
409 }
410
411
412
413
414
415
416 public void setDownloadSources( boolean downloadSources )
417 {
418 this.downloadSources = downloadSources;
419 }
420
421 protected void setResolveDependencies( boolean resolveDependencies )
422 {
423 this.resolveDependencies = resolveDependencies;
424 }
425
426 protected boolean isResolveDependencies()
427 {
428 return resolveDependencies;
429 }
430
431
432
433
434
435
436
437 protected abstract boolean getUseProjectReferences();
438
439
440
441
442
443
444
445 protected abstract boolean setup()
446 throws MojoExecutionException;
447
448
449
450
451
452
453
454 protected abstract void writeConfiguration( IdeDependency[] deps )
455 throws MojoExecutionException;
456
457
458
459
460 private List missingSourceDependencies = new ArrayList();
461
462
463
464
465
466 private List missingJavadocDependencies = new ArrayList();
467
468
469
470
471 private IdeDependency[] ideDeps;
472
473
474
475
476
477 private boolean resolveDependencies = true;
478
479
480
481
482 public void enableLogging( Logger logger )
483 {
484 this.logger = logger;
485 }
486
487
488
489
490 public final void execute()
491 throws MojoExecutionException, MojoFailureException
492 {
493 if ( skip )
494 {
495 return;
496 }
497
498 boolean processProject = setup();
499 if ( !processProject )
500 {
501 return;
502 }
503
504
505 IdeDependency[] deps = doDependencyResolution();
506
507 resolveSourceAndJavadocArtifacts( deps );
508
509 writeConfiguration( deps );
510
511 reportMissingArtifacts();
512
513 }
514
515
516
517
518
519
520
521
522
523 protected IdeDependency[] doDependencyResolution()
524 throws MojoExecutionException
525 {
526 if ( ideDeps == null )
527 {
528 if ( resolveDependencies )
529 {
530 MavenProject project = getProject();
531 ArtifactRepository localRepo = getLocalRepository();
532
533 List deps = getProject().getDependencies();
534
535
536 List dependencies = new ArrayList();
537
538 if ( deps != null )
539 {
540 Map managedVersions =
541 createManagedVersionMap( getArtifactFactory(), project.getId(),
542 project.getDependencyManagement() );
543
544 ArtifactResolutionResult artifactResolutionResult = null;
545
546 try
547 {
548
549 List listeners = new ArrayList();
550
551 if ( logger.isDebugEnabled() )
552 {
553 listeners.add( new DebugResolutionListener( logger ) );
554 }
555
556 listeners.add( new WarningResolutionListener( logger ) );
557
558 artifactResolutionResult =
559 artifactCollector.collect( getProjectArtifacts(), project.getArtifact(), managedVersions,
560 localRepo, project.getRemoteArtifactRepositories(),
561 getArtifactMetadataSource(), null, listeners );
562 }
563 catch ( ArtifactResolutionException e )
564 {
565 getLog().debug( e.getMessage(), e );
566 getLog().error(
567 Messages.getString( "AbstractIdeSupportMojo.artifactresolution", new Object[] {
568 e.getGroupId(), e.getArtifactId(), e.getVersion(),
569 e.getMessage() } ) );
570
571
572
573
574
575 return new IdeDependency[0];
576 }
577
578
579 Set emittedReactorProjectId = new HashSet();
580
581 for ( Iterator i = artifactResolutionResult.getArtifactResolutionNodes().iterator(); i.hasNext(); )
582 {
583
584 ResolutionNode node = (ResolutionNode) i.next();
585 int dependencyDepth = node.getDepth();
586 Artifact art = node.getArtifact();
587
588 if ( hasToResolveJar( art ) )
589 {
590 try
591 {
592 artifactResolver.resolve( art, node.getRemoteRepositories(), localRepository );
593 }
594 catch ( ArtifactNotFoundException e )
595 {
596 getLog().debug( e.getMessage(), e );
597 getLog().warn(
598 Messages.getString( "AbstractIdeSupportMojo.artifactdownload", new Object[] {
599 e.getGroupId(), e.getArtifactId(), e.getVersion(),
600 e.getMessage() } ) );
601 }
602 catch ( ArtifactResolutionException e )
603 {
604 getLog().debug( e.getMessage(), e );
605 getLog().warn(
606 Messages.getString( "AbstractIdeSupportMojo.artifactresolution", new Object[] {
607 e.getGroupId(), e.getArtifactId(), e.getVersion(),
608 e.getMessage() } ) );
609 }
610 }
611
612 boolean includeArtifact = true;
613 if ( getExcludes() != null )
614 {
615 String artifactFullId = art.getGroupId() + ":" + art.getArtifactId();
616 if ( getExcludes().contains( artifactFullId ) )
617 {
618 getLog().info( "excluded: " + artifactFullId );
619 includeArtifact = false;
620 }
621 }
622
623 if ( includeArtifact
624 && ( !( getUseProjectReferences() && isAvailableAsAReactorProject( art ) ) || emittedReactorProjectId.add( art.getGroupId()
625 + '-' + art.getArtifactId() ) ) )
626 {
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641 boolean isOsgiBundle = false;
642 String osgiSymbolicName = null;
643 if ( art.getFile() != null )
644 {
645 JarFile jarFile = null;
646 try
647 {
648 jarFile = new JarFile( art.getFile(), false, ZipFile.OPEN_READ );
649
650 Manifest manifest = jarFile.getManifest();
651 if ( manifest != null )
652 {
653 osgiSymbolicName =
654 manifest.getMainAttributes().getValue(
655 new Attributes.Name(
656 "Bundle-SymbolicName" ) );
657 }
658 }
659 catch ( IOException e )
660 {
661 getLog().info( "Unable to read jar manifest from " + art.getFile() );
662 }
663 finally
664 {
665 if ( jarFile != null )
666 {
667 try
668 {
669 jarFile.close();
670 }
671 catch ( IOException e )
672 {
673
674 }
675 }
676 }
677 }
678
679 isOsgiBundle = osgiSymbolicName != null;
680
681 IdeDependency dep =
682 new IdeDependency( art.getGroupId(), art.getArtifactId(), art.getVersion(),
683 art.getClassifier(), useProjectReference( art ),
684 Artifact.SCOPE_TEST.equals( art.getScope() ),
685 Artifact.SCOPE_SYSTEM.equals( art.getScope() ),
686 Artifact.SCOPE_PROVIDED.equals( art.getScope() ),
687 art.getArtifactHandler().isAddedToClasspath(), art.getFile(),
688 art.getType(), isOsgiBundle, osgiSymbolicName, dependencyDepth,
689 getProjectNameForArifact( art ) );
690
691 if ( !dependencies.contains( dep ) )
692 {
693 dependencies.add( dep );
694 }
695 }
696
697 }
698
699
700
701
702 }
703
704 ideDeps = (IdeDependency[]) dependencies.toArray( new IdeDependency[dependencies.size()] );
705 }
706 else
707 {
708 ideDeps = new IdeDependency[0];
709 }
710 }
711
712 return ideDeps;
713 }
714
715
716
717
718
719
720
721 abstract public String getProjectNameForArifact( Artifact artifact );
722
723
724
725
726
727
728
729
730 private Set getProjectArtifacts()
731 throws MojoExecutionException
732 {
733
734 Set artifacts = new TreeSet();
735
736 for ( Iterator dependencies = getProject().getDependencies().iterator(); dependencies.hasNext(); )
737 {
738 Dependency dependency = (Dependency) dependencies.next();
739
740 String groupId = dependency.getGroupId();
741 String artifactId = dependency.getArtifactId();
742 VersionRange versionRange;
743 try
744 {
745 versionRange = VersionRange.createFromVersionSpec( dependency.getVersion() );
746 }
747 catch ( InvalidVersionSpecificationException e )
748 {
749 throw new MojoExecutionException(
750 Messages.getString(
751 "AbstractIdeSupportMojo.unabletoparseversion", new Object[] {
752 dependency.getArtifactId(),
753 dependency.getVersion(),
754 dependency.getManagementKey(), e.getMessage() } ),
755 e );
756 }
757
758 String type = dependency.getType();
759 if ( type == null )
760 {
761 type = Constants.PROJECT_PACKAGING_JAR;
762 }
763 String classifier = dependency.getClassifier();
764 boolean optional = dependency.isOptional();
765 String scope = dependency.getScope();
766 if ( scope == null )
767 {
768 scope = Artifact.SCOPE_COMPILE;
769 }
770
771 Artifact art =
772 getArtifactFactory().createDependencyArtifact( groupId, artifactId, versionRange, type, classifier,
773 scope, optional );
774
775 if ( scope.equalsIgnoreCase( Artifact.SCOPE_SYSTEM ) )
776 {
777 art.setFile( new File( dependency.getSystemPath() ) );
778 }
779
780 List exclusions = new ArrayList();
781 for ( Iterator j = dependency.getExclusions().iterator(); j.hasNext(); )
782 {
783 Exclusion e = (Exclusion) j.next();
784 exclusions.add( e.getGroupId() + ":" + e.getArtifactId() );
785 }
786
787 ArtifactFilter newFilter = new ExcludesArtifactFilter( exclusions );
788
789 art.setDependencyFilter( newFilter );
790
791 artifacts.add( art );
792 }
793
794 return artifacts;
795 }
796
797
798
799
800
801
802
803 protected boolean isAvailableAsAReactorProject( Artifact artifact )
804 {
805 return getReactorProject( artifact ) != null;
806 }
807
808
809
810
811
812
813
814 protected MavenProject getReactorProject( Artifact artifact ) {
815 if ( reactorProjects != null )
816 {
817 for ( Iterator iter = reactorProjects.iterator(); iter.hasNext(); )
818 {
819 MavenProject reactorProject = (MavenProject) iter.next();
820
821 if ( reactorProject.getGroupId().equals( artifact.getGroupId() )
822 && reactorProject.getArtifactId().equals( artifact.getArtifactId() ) )
823 {
824 if ( reactorProject.getVersion().equals( artifact.getVersion() ) )
825 {
826 return reactorProject;
827 }
828 else
829 {
830 getLog().info(
831 "Artifact "
832 + artifact.getId()
833 + " already available as a reactor project, but with different version. Expected: "
834 + artifact.getVersion() + ", found: " + reactorProject.getVersion() );
835 }
836 }
837 }
838 }
839 return null;
840 }
841
842
843
844
845 protected IdeDependency[] getWorkspaceArtefacts()
846 {
847 return new IdeDependency[0];
848 }
849
850 private Map createManagedVersionMap( ArtifactFactory artifactFactory, String projectId,
851 DependencyManagement dependencyManagement )
852 throws MojoExecutionException
853 {
854 Map map;
855 if ( dependencyManagement != null && dependencyManagement.getDependencies() != null )
856 {
857 map = new HashMap();
858 for ( Iterator i = dependencyManagement.getDependencies().iterator(); i.hasNext(); )
859 {
860 Dependency d = (Dependency) i.next();
861
862 try
863 {
864 VersionRange versionRange = VersionRange.createFromVersionSpec( d.getVersion() );
865 Artifact artifact =
866 artifactFactory.createDependencyArtifact( d.getGroupId(), d.getArtifactId(), versionRange,
867 d.getType(), d.getClassifier(), d.getScope(),
868 d.isOptional() );
869 map.put( d.getManagementKey(), artifact );
870 }
871 catch ( InvalidVersionSpecificationException e )
872 {
873 throw new MojoExecutionException( Messages.getString( "AbstractIdeSupportMojo.unabletoparseversion", new Object[] {
874 projectId, d.getVersion(),
875 d.getManagementKey(), e.getMessage() } ),
876 e );
877 }
878 }
879 }
880 else
881 {
882 map = Collections.EMPTY_MAP;
883 }
884 return map;
885 }
886
887
888
889
890
891
892
893
894
895 private void resolveSourceAndJavadocArtifacts( IdeDependency[] deps )
896 {
897 final List missingSources =
898 resolveDependenciesWithClassifier( deps, "sources", getDownloadSources() );
899 missingSourceDependencies.addAll( missingSources );
900
901 final List missingJavadocs =
902 resolveDependenciesWithClassifier( deps, "javadoc", getDownloadJavadocs() );
903 missingJavadocDependencies.addAll( missingJavadocs );
904 }
905
906
907
908
909
910
911
912
913
914
915 private List resolveDependenciesWithClassifier( IdeDependency[] deps, String inClassifier,
916 boolean includeRemoteRepositories )
917 {
918 List missingClassifierDependencies = new ArrayList();
919
920
921
922 List remoteRepos = includeRemoteRepositories ? getRemoteArtifactRepositories() : Collections.EMPTY_LIST;
923
924 for ( int j = 0; j < deps.length; j++ )
925 {
926 IdeDependency dependency = deps[j];
927
928 if ( dependency.isReferencedProject() || dependency.isSystemScoped() )
929 {
930
931 continue;
932 }
933
934 if ( getLog().isDebugEnabled() )
935 {
936 getLog().debug(
937 "Searching for sources for " + dependency.getId() + ":" + dependency.getClassifier()
938 + " at " + dependency.getId() + ":" + inClassifier );
939 }
940
941 Artifact baseArtifact =
942 artifactFactory.createArtifactWithClassifier( dependency.getGroupId(), dependency.getArtifactId(),
943 dependency.getVersion(), dependency.getType(),
944 dependency.getClassifier() );
945 baseArtifact =
946 IdeUtils.resolveArtifact( artifactResolver, baseArtifact, remoteRepos, localRepository, getLog() );
947 if (!baseArtifact.isResolved()) {
948
949 continue;
950 }
951
952 Artifact artifact =
953 IdeUtils.createArtifactWithClassifier( dependency.getGroupId(), dependency.getArtifactId(),
954 dependency.getVersion(), dependency.getClassifier(),
955 inClassifier, artifactFactory );
956 File notAvailableMarkerFile = IdeUtils.getNotAvailableMarkerFile( localRepository, artifact );
957
958 if (forceRecheck && notAvailableMarkerFile.exists()) {
959 if (!notAvailableMarkerFile.delete()) {
960 getLog().warn( Messages.getString( "AbstractIdeSupportMojo.unabletodeletenotavailablemarkerfile", notAvailableMarkerFile ) );
961 }
962 }
963
964 if ( !notAvailableMarkerFile.exists() )
965 {
966 artifact =
967 IdeUtils.resolveArtifact( artifactResolver, artifact, remoteRepos, localRepository, getLog() );
968 if ( artifact.isResolved() )
969 {
970 if ( "sources".equals( inClassifier ) )
971 {
972 dependency.setSourceAttachment( artifact.getFile() );
973 }
974 else if ( "javadoc".equals( inClassifier ) )
975 {
976 dependency.setJavadocAttachment( artifact.getFile() );
977 }
978 }
979 else
980 {
981 if ( includeRemoteRepositories )
982 {
983 try
984 {
985 notAvailableMarkerFile.createNewFile();
986 getLog().debug( Messages.getString( "AbstractIdeSupportMojo.creatednotavailablemarkerfile", notAvailableMarkerFile ) );
987 }
988 catch ( IOException e )
989 {
990 getLog().warn( Messages.getString( "AbstractIdeSupportMojo.failedtocreatenotavailablemarkerfile",
991 notAvailableMarkerFile ) );
992 }
993 }
994
995
996
997 missingClassifierDependencies.add( dependency );
998 }
999 }
1000 }
1001
1002
1003
1004 return missingClassifierDependencies;
1005
1006 }
1007
1008
1009
1010
1011 private void reportMissingArtifacts()
1012 {
1013 StringBuffer msg = new StringBuffer();
1014
1015 if ( !missingSourceDependencies.isEmpty() )
1016 {
1017 if ( getDownloadSources() )
1018 {
1019 msg.append( Messages.getString( "AbstractIdeSupportMojo.sourcesnotavailable" ) );
1020 }
1021 else
1022 {
1023 msg.append( Messages.getString( "AbstractIdeSupportMojo.sourcesnotdownloaded" ) );
1024 }
1025
1026 for ( Iterator it = missingSourceDependencies.iterator(); it.hasNext(); )
1027 {
1028 IdeDependency art = (IdeDependency) it.next();
1029 msg.append( Messages.getString( "AbstractIdeSupportMojo.sourcesmissingitem", art.getId() ) );
1030 }
1031 msg.append( "\n" );
1032 }
1033
1034 if ( !missingJavadocDependencies.isEmpty() )
1035 {
1036 if ( getDownloadJavadocs() )
1037 {
1038 msg.append( Messages.getString( "AbstractIdeSupportMojo.javadocnotavailable" ) );
1039 }
1040 else
1041 {
1042 msg.append( Messages.getString( "AbstractIdeSupportMojo.javadocnotdownloaded" ) );
1043 }
1044
1045 for ( Iterator it = missingJavadocDependencies.iterator(); it.hasNext(); )
1046 {
1047 IdeDependency art = (IdeDependency) it.next();
1048 msg.append( Messages.getString( "AbstractIdeSupportMojo.javadocmissingitem", art.getId() ) );
1049 }
1050 msg.append( "\n" );
1051 }
1052 getLog().info( msg );
1053 }
1054
1055
1056
1057
1058
1059 public abstract List getExcludes();
1060
1061
1062
1063
1064
1065
1066
1067 protected boolean hasToResolveJar( Artifact art )
1068 {
1069 return !( getUseProjectReferences() && isAvailableAsAReactorProject( art ) );
1070 }
1071
1072
1073
1074
1075
1076
1077
1078 protected boolean useProjectReference( Artifact art )
1079 {
1080 return getUseProjectReferences() && isAvailableAsAReactorProject( art );
1081 }
1082
1083
1084
1085
1086
1087
1088
1089
1090 protected boolean isMavenVersion( String version )
1091 {
1092 try
1093 {
1094 VersionRange versionRange = VersionRange.createFromVersionSpec( version );
1095 ArtifactVersion mavenVersion = runtimeInformation.getApplicationVersion();
1096 return versionRange.containsVersion( mavenVersion );
1097 }
1098 catch ( InvalidVersionSpecificationException e )
1099 {
1100 throw new IllegalArgumentException( e.getMessage() );
1101 }
1102 }
1103
1104 }