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