View Javadoc
1   package org.apache.maven.plugins.plugin.descriptor;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.plugin.descriptor.MojoDescriptor;
23  import org.apache.maven.plugin.descriptor.Parameter;
24  import org.apache.maven.plugin.descriptor.PluginDescriptor;
25  import org.apache.maven.plugin.descriptor.PluginDescriptorBuilder;
26  import org.apache.maven.rtinfo.RuntimeInformation;
27  import org.apache.maven.tools.plugin.EnhancedParameterWrapper;
28  import org.codehaus.plexus.configuration.PlexusConfiguration;
29  import org.codehaus.plexus.configuration.PlexusConfigurationException;
30  import org.apache.maven.plugin.plugin.report.PluginReport;
31  
32  import java.net.URI;
33  
34  /**
35   * Reads enhanced plugin.xml files as generated by
36   * {@link org.apache.maven.tools.plugin.generator.PluginDescriptorFilesGenerator} and
37   * used by {@link PluginReport}.
38   * Populates the slightly extended {@link Parameter} object {@link EnhancedParameterWrapper}.
39   */
40  public class EnhancedPluginDescriptorBuilder extends PluginDescriptorBuilder
41  {
42      private final boolean requireAddingMissingParameterSinceField;
43      
44      public EnhancedPluginDescriptorBuilder( RuntimeInformation rtInfo )
45      {
46          this( rtInfo.isMavenVersion( "[,3.3.9]" ) );
47      }
48  
49      EnhancedPluginDescriptorBuilder( boolean requireAddingMissingParameterSinceField )
50      {
51          this.requireAddingMissingParameterSinceField = requireAddingMissingParameterSinceField;
52      }
53  
54      @Override
55      public MojoDescriptor buildComponentDescriptor( PlexusConfiguration c, PluginDescriptor pluginDescriptor )
56          throws PlexusConfigurationException
57      {
58          MojoDescriptor mojoDescriptor = super.buildComponentDescriptor( c, pluginDescriptor );
59          
60          // ----------------------------------------------------------------------
61          // Parameters
62          // ----------------------------------------------------------------------
63  
64          PlexusConfiguration[] parameterConfigurations = c.getChild( "parameters" ).getChildren( "parameter" );
65  
66          for ( PlexusConfiguration d : parameterConfigurations )
67          {
68              String parameterName = d.getChild( "name" ).getValue();
69              // don't call getParameterMap() to not populate 
70              Parameter pd = mojoDescriptor.getParameterMap().get( parameterName );
71              if ( requireAddingMissingParameterSinceField )
72              {
73                  addMissingParameterSinceField( pd, d );
74              }
75              PlexusConfiguration configTypeJavadocUrl = d.getChild( "typeJavadocUrl", false );
76              if ( configTypeJavadocUrl != null )
77              {
78                  String parameterTypeJavadocUrl = configTypeJavadocUrl.getValue();
79                  EnhancedParameterWrapper enhancedParameter = new EnhancedParameterWrapper( pd );
80                  enhancedParameter.setTypeJavadocUrl( URI.create( parameterTypeJavadocUrl ) );
81                  mojoDescriptor.getParameters().set( mojoDescriptor.getParameters().indexOf( pd ), enhancedParameter );
82                  mojoDescriptor.getParameterMap().put( parameterName, enhancedParameter );
83              }
84          }
85          return mojoDescriptor;
86      }
87  
88      /**
89       * Reads the plugin descriptor and adds the fix for <a href="https://issues.apache.org/jira/browse/MNG-6109">
90       * MNG-6109</a> when using Maven-3.3.9 and before.
91       * Method can be removed once Maven 3.5.0 is the prerequisite for this plugin.
92       * @throws PlexusConfigurationException 
93       * 
94       * @since 3.5.1
95       * @see <a href="https://issues.apache.org/jira/browse/MNG-6109">MNG-6109</a>
96       * @see <a href="https://issues.apache.org/jira/browse/MPLUGIN-319">MPLUGIN-319</a>
97       */
98       void addMissingParameterSinceField( Parameter pd, PlexusConfiguration d ) throws PlexusConfigurationException
99       {
100          String parameterSince = d.getChild( "since" ).getValue();
101          pd.setSince( parameterSince );
102      }
103 }