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