1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
39
40
41
42 @Deprecated
43 public class PluginMetadataParser {
44
45
46
47
48 public static final String IMPL_BASE_PLACEHOLDER = "<REPLACE-WITH-MOJO-PATH>";
49
50
51
52
53
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
82
83
84
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 }