View Javadoc
1   package org.apache.maven.tools.plugin.extractor.model;
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.DuplicateParameterException;
23  import org.apache.maven.plugin.descriptor.MojoDescriptor;
24  import org.apache.maven.plugin.descriptor.Parameter;
25  import org.apache.maven.tools.plugin.extractor.model.io.xpp3.PluginMetadataXpp3Reader;
26  import org.codehaus.plexus.component.repository.ComponentRequirement;
27  import org.codehaus.plexus.util.ReaderFactory;
28  import org.codehaus.plexus.util.StringUtils;
29  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
30  
31  import java.io.File;
32  import java.io.IOException;
33  import java.io.Reader;
34  import java.util.HashSet;
35  import java.util.List;
36  import java.util.Set;
37  
38  /**
39   * Parser for plugin metadata.
40   *
41   * @deprecated Scripting support for mojos is deprecated and is planned tp be removed in maven 4.0
42   */
43  @Deprecated
44  public class PluginMetadataParser
45  {
46      /**
47       * Default implementation path which will be replaced in
48       * AbstractScriptedMojoDescriptorExtractor#extractMojoDescriptorsFromMetadata(Map, PluginDescriptor)
49       */
50      public static final String IMPL_BASE_PLACEHOLDER = "<REPLACE-WITH-MOJO-PATH>";
51  
52      /**
53       * @param metadataFile the metadata file to be parse
54       * @return a set of <code>MojoDescriptor</code>
55       * @throws PluginMetadataParseException if any
56       */
57      public Set<MojoDescriptor> parseMojoDescriptors( File metadataFile )
58          throws PluginMetadataParseException
59      {
60          Set<MojoDescriptor> descriptors = new HashSet<>();
61  
62          try ( Reader reader = ReaderFactory.newXmlReader( metadataFile ) )
63          {
64  
65              PluginMetadataXpp3Reader metadataReader = new PluginMetadataXpp3Reader();
66  
67              PluginMetadata pluginMetadata = metadataReader.read( reader );
68  
69              List<Mojo> mojos = pluginMetadata.getMojos();
70  
71              if ( mojos != null )
72              {
73                  for ( Mojo mojo : mojos )
74                  {
75                      MojoDescriptor descriptor = asDescriptor( metadataFile, mojo );
76  
77                      descriptors.add( descriptor );
78                  }
79              }
80          }
81          catch ( IOException | XmlPullParserException e )
82          {
83              throw new PluginMetadataParseException( metadataFile, "Cannot parse plugin metadata from file.", e );
84          }
85  
86          return descriptors;
87      }
88  
89      /**
90       * @param metadataFile not null
91       * @param mojo         not null
92       * @return a mojo descriptor instance
93       * @throws PluginMetadataParseException if any
94       */
95      private MojoDescriptor asDescriptor( File metadataFile, Mojo mojo )
96          throws PluginMetadataParseException
97      {
98          MojoDescriptor descriptor = new MojoDescriptor();
99  
100         if ( mojo.getCall() != null )
101         {
102             descriptor.setImplementation( IMPL_BASE_PLACEHOLDER + ":" + mojo.getCall() );
103         }
104         else
105         {
106             descriptor.setImplementation( IMPL_BASE_PLACEHOLDER );
107         }
108 
109         descriptor.setGoal( mojo.getGoal() );
110         descriptor.setPhase( mojo.getPhase() );
111         descriptor.setDependencyResolutionRequired( mojo.getRequiresDependencyResolution() );
112         descriptor.setAggregator( mojo.isAggregator() );
113         descriptor.setInheritedByDefault( mojo.isInheritByDefault() );
114         descriptor.setDirectInvocationOnly( mojo.isRequiresDirectInvocation() );
115         descriptor.setOnlineRequired( mojo.isRequiresOnline() );
116         descriptor.setProjectRequired( mojo.isRequiresProject() );
117         descriptor.setRequiresReports( mojo.isRequiresReports() );
118         descriptor.setDescription( mojo.getDescription() );
119         descriptor.setDeprecated( mojo.getDeprecation() );
120         descriptor.setSince( mojo.getSince() );
121 
122         LifecycleExecution le = mojo.getExecution();
123         if ( le != null )
124         {
125             descriptor.setExecuteLifecycle( le.getLifecycle() );
126             descriptor.setExecutePhase( le.getPhase() );
127             descriptor.setExecuteGoal( le.getGoal() );
128         }
129 
130         List<org.apache.maven.tools.plugin.extractor.model.Parameter> parameters = mojo.getParameters();
131 
132         if ( parameters != null && !parameters.isEmpty() )
133         {
134             for ( org.apache.maven.tools.plugin.extractor.model.Parameter param : parameters )
135             {
136                 Parameter dParam = new Parameter();
137                 dParam.setAlias( param.getAlias() );
138                 dParam.setDeprecated( param.getDeprecation() );
139                 dParam.setDescription( param.getDescription() );
140                 dParam.setEditable( !param.isReadonly() );
141                 dParam.setExpression( param.getExpression() );
142                 dParam.setDefaultValue( param.getDefaultValue() );
143                 dParam.setSince( param.getSince() );
144 
145                 String property = param.getProperty();
146                 if ( StringUtils.isNotEmpty( property ) )
147                 {
148                     dParam.setName( property );
149                 }
150                 else
151                 {
152                     dParam.setName( param.getName() );
153                 }
154 
155                 if ( StringUtils.isEmpty( dParam.getName() ) )
156                 {
157                     throw new PluginMetadataParseException( metadataFile, "Mojo: \'" + mojo.getGoal()
158                         + "\' has a parameter without either property or name attributes. Please specify one." );
159                 }
160 
161                 dParam.setRequired( param.isRequired() );
162                 dParam.setType( param.getType() );
163 
164                 try
165                 {
166                     descriptor.addParameter( dParam );
167                 }
168                 catch ( DuplicateParameterException e )
169                 {
170                     throw new PluginMetadataParseException( metadataFile,
171                                                             "Duplicate parameters detected for mojo: " + mojo.getGoal(),
172                                                             e );
173                 }
174             }
175         }
176 
177         List<Component> components = mojo.getComponents();
178 
179         if ( components != null && !components.isEmpty() )
180         {
181             for ( Component component : components )
182             {
183                 ComponentRequirement cr = new ComponentRequirement();
184                 cr.setRole( component.getRole() );
185                 cr.setRoleHint( component.getHint() );
186 
187                 descriptor.addRequirement( cr );
188             }
189         }
190 
191         return descriptor;
192     }
193 }