1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.enforcer.rules.utils;
20
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.Optional;
25
26 import org.apache.maven.model.InputLocation;
27 import org.apache.maven.model.InputLocationTracker;
28 import org.apache.maven.model.Plugin;
29 import org.apache.maven.model.ReportPlugin;
30
31
32
33
34
35 public class PluginWrapper {
36 private final String groupId;
37
38 private final String artifactId;
39
40 private final String version;
41
42 private final InputLocationTracker locationTracker;
43
44 public static List<PluginWrapper> addAll(List<? extends InputLocationTracker> plugins, boolean banMavenDefaults) {
45 if (plugins.isEmpty()) {
46 return Collections.emptyList();
47 }
48
49 List<PluginWrapper> results = new ArrayList<>(plugins.size());
50 for (InputLocationTracker o : plugins) {
51
52 if (banMavenDefaults
53 && (isVersionFromDefaultLifecycleBindings(o).orElse(true)
54 || isVersionFromSuperpom(o).orElse(true))) {
55 continue;
56 }
57
58 if (o instanceof Plugin) {
59 results.add(new PluginWrapper((Plugin) o));
60 } else {
61 if (o instanceof ReportPlugin) {
62 results.add(new PluginWrapper((ReportPlugin) o));
63 }
64 }
65 }
66 return results;
67 }
68
69
70
71
72
73
74
75
76
77 public static Optional<Boolean> isVersionFromDefaultLifecycleBindings(InputLocationTracker o) {
78 InputLocation versionLocation = o.getLocation("version");
79 if (versionLocation == null) {
80 return Optional.empty();
81 }
82
83 String modelId = versionLocation.getSource().getModelId();
84 return Optional.of(
85 modelId.startsWith("org.apache.maven:maven-core:") && modelId.endsWith(":default-lifecycle-bindings"));
86 }
87
88
89
90
91
92
93
94
95
96 public static Optional<Boolean> isVersionFromSuperpom(InputLocationTracker o) {
97 InputLocation versionLocation = o.getLocation("version");
98 if (versionLocation == null) {
99 return Optional.empty();
100 }
101
102 String modelId = versionLocation.getSource().getModelId();
103 return Optional.of(
104 modelId.startsWith("org.apache.maven:maven-model-builder:") && modelId.endsWith(":super-pom"));
105 }
106
107 private PluginWrapper(Plugin plugin) {
108 this.groupId = plugin.getGroupId();
109 this.artifactId = plugin.getArtifactId();
110 this.version = plugin.getVersion();
111 this.locationTracker = plugin;
112 }
113
114 private PluginWrapper(ReportPlugin plugin) {
115 this.groupId = plugin.getGroupId();
116 this.artifactId = plugin.getArtifactId();
117 this.version = plugin.getVersion();
118 this.locationTracker = plugin;
119 }
120
121 public String getGroupId() {
122 return groupId;
123 }
124
125 public String getArtifactId() {
126 return artifactId;
127 }
128
129 public String getVersion() {
130 return version;
131 }
132
133 public String getSource() {
134 InputLocation inputLocation = locationTracker.getLocation("version");
135
136 if (inputLocation == null) {
137
138 return "unknown";
139 } else {
140 return inputLocation.getSource().getLocation();
141 }
142 }
143 }