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.plugins.plugin.descriptor;
20  
21  import java.io.Reader;
22  import java.net.URI;
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.LinkedHashMap;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Optional;
29  
30  import org.apache.maven.plugin.descriptor.MojoDescriptor;
31  import org.apache.maven.plugin.descriptor.Parameter;
32  import org.apache.maven.plugin.descriptor.PluginDescriptor;
33  import org.apache.maven.plugin.descriptor.PluginDescriptorBuilder;
34  import org.apache.maven.plugin.plugin.report.PluginReport;
35  import org.apache.maven.rtinfo.RuntimeInformation;
36  import org.apache.maven.tools.plugin.EnhancedParameterWrapper;
37  import org.apache.maven.tools.plugin.ExtendedPluginDescriptor;
38  import org.codehaus.plexus.configuration.PlexusConfiguration;
39  import org.codehaus.plexus.configuration.PlexusConfigurationException;
40  
41  /**
42   * Reads enhanced plugin.xml files as generated by
43   * {@code PluginDescriptorFilesGenerator} and used by {@link PluginReport}.
44   * Populates the slightly extended {@link Parameter} object {@link EnhancedParameterWrapper}.
45   * In addition populates all (optional) elements added after Maven Plugin API 3.2.5.
46   */
47  public class EnhancedPluginDescriptorBuilder extends PluginDescriptorBuilder {
48      private final boolean requireAddingMissingParameterSinceField;
49      private PlexusConfiguration configuration;
50  
51      public EnhancedPluginDescriptorBuilder(RuntimeInformation rtInfo) {
52          this(rtInfo.isMavenVersion("[,3.3.9]"));
53      }
54  
55      EnhancedPluginDescriptorBuilder(boolean requireAddingMissingParameterSinceField) {
56          this.requireAddingMissingParameterSinceField = requireAddingMissingParameterSinceField;
57      }
58  
59      /**
60       * Cache the returned configuration for additional evaluation in {@link #build(Reader, String)}.
61       */
62      @Override
63      public PlexusConfiguration buildConfiguration(Reader reader) throws PlexusConfigurationException {
64          configuration = super.buildConfiguration(reader);
65          return configuration;
66      }
67  
68      @Override
69      public PluginDescriptor build(Reader reader, String source) throws PlexusConfigurationException {
70          PluginDescriptor pluginDescriptor = super.build(reader, source);
71          // elements added in plugin descriptor 1.1
72          ExtendedPluginDescriptor extendedPluginDescriptor = new ExtendedPluginDescriptor(pluginDescriptor);
73          extendedPluginDescriptor.setRequiredJavaVersion(
74                  configuration.getChild("requiredJavaVersion").getValue());
75          extendedPluginDescriptor.setRequiredMavenVersion(
76                  configuration.getChild("requiredMavenVersion").getValue());
77          return extendedPluginDescriptor;
78      }
79  
80      @Override
81      public MojoDescriptor buildComponentDescriptor(PlexusConfiguration c, PluginDescriptor pluginDescriptor)
82              throws PlexusConfigurationException {
83          MojoDescriptor mojoDescriptor = super.buildComponentDescriptor(c, pluginDescriptor);
84  
85          // ----------------------------------------------------------------------
86          // Parameters
87          // ----------------------------------------------------------------------
88  
89          PlexusConfiguration[] parameterConfigurations = c.getChild("parameters").getChildren("parameter");
90  
91          List<Parameter> parameters = new ArrayList<>(
92                  Optional.ofNullable(mojoDescriptor.getParameters()).orElseGet(Collections::emptyList));
93          Map<String, Parameter> parameterMap = new LinkedHashMap<>(mojoDescriptor.getParameterMap());
94  
95          for (PlexusConfiguration d : parameterConfigurations) {
96              String parameterName = d.getChild("name").getValue();
97              // don't call getParameterMap() to not populate
98              Parameter pd = parameterMap.get(parameterName);
99              if (requireAddingMissingParameterSinceField) {
100                 addMissingParameterSinceField(pd, d);
101             }
102             PlexusConfiguration configTypeJavadocUrl = d.getChild("typeJavadocUrl", false);
103             if (configTypeJavadocUrl != null) {
104                 String parameterTypeJavadocUrl = configTypeJavadocUrl.getValue();
105                 EnhancedParameterWrapper enhancedParameter = new EnhancedParameterWrapper(pd);
106                 enhancedParameter.setTypeJavadocUrl(URI.create(parameterTypeJavadocUrl));
107                 parameters.set(mojoDescriptor.getParameters().indexOf(pd), enhancedParameter);
108                 parameterMap.put(parameterName, enhancedParameter);
109             }
110         }
111 
112         // TODO This cruft has been resolved in Maven 3.8.9/3.9.7/4.0.0-alpha-1 with MNG-6776/MNG-7309
113         if (mojoDescriptor.getParameters() != null) {
114             mojoDescriptor.getParameters().clear();
115         }
116         // set parameters
117         mojoDescriptor.setParameters(parameters);
118         mojoDescriptor.getParameterMap().putAll(parameterMap);
119 
120         return mojoDescriptor;
121     }
122 
123     /**
124      * Reads the plugin descriptor and adds the fix for <a href="https://issues.apache.org/jira/browse/MNG-6109">
125      * MNG-6109</a> when using Maven-3.3.9 and before.
126      * Method can be removed once Maven 3.5.0 is the prerequisite for this plugin.
127      * @throws PlexusConfigurationException
128      *
129      * @since 3.5.1
130      * @see <a href="https://issues.apache.org/jira/browse/MNG-6109">MNG-6109</a>
131      * @see <a href="https://issues.apache.org/jira/browse/MPLUGIN-319">MPLUGIN-319</a>
132      */
133     void addMissingParameterSinceField(Parameter pd, PlexusConfiguration d) throws PlexusConfigurationException {
134         String parameterSince = d.getChild("since").getValue();
135         pd.setSince(parameterSince);
136     }
137 }