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