View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * @author Brian Fox
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              // null or true means it is most assumed a Maven default
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       * Whether the version is coming from the default lifecycle bindings.
71       * Cannot be determined before Maven 3.6.1
72       *
73       * @param o either Plugin or ReportPlugin
74       * @return null if untraceable, otherwise its matching value
75       * @see <a href="https://issues.apache.org/jira/browse/MNG-6600">MNG-6600</a>
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       * Whether the version is coming from the super POM.
90       * Cannot be determined before Maven 3.6.1
91       *
92       * @param o either Plugin or ReportPlugin
93       * @return null if untraceable, otherwise its matching value
94       * @see <a href="https://issues.apache.org/jira/browse/MNG-6593">MNG-6593</a>
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             // most likely super-pom or default-lifecycle-bindings in Maven 3.6.0 or before (MNG-6593 / MNG-6600)
138             return "unknown";
139         } else {
140             return inputLocation.getSource().getLocation();
141         }
142     }
143 }