1 package org.apache.maven.tools.plugin.extractor.model;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.io.Reader;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Set;
28
29 import org.apache.maven.plugin.descriptor.DuplicateParameterException;
30 import org.apache.maven.plugin.descriptor.MojoDescriptor;
31 import org.apache.maven.plugin.descriptor.Parameter;
32 import org.apache.maven.tools.plugin.extractor.model.io.xpp3.PluginMetadataXpp3Reader;
33 import org.codehaus.plexus.component.repository.ComponentRequirement;
34 import org.codehaus.plexus.util.IOUtil;
35 import org.codehaus.plexus.util.ReaderFactory;
36 import org.codehaus.plexus.util.StringUtils;
37 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
38
39
40
41
42
43
44 public class PluginMetadataParser
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 )
56 throws PluginMetadataParseException
57 {
58 Set<MojoDescriptor> descriptors = new HashSet<MojoDescriptor>();
59
60 Reader reader = null;
61
62 try
63 {
64 reader = ReaderFactory.newXmlReader( metadataFile );
65
66 PluginMetadataXpp3Reader metadataReader = new PluginMetadataXpp3Reader();
67
68 PluginMetadata pluginMetadata = metadataReader.read( reader );
69
70 List<Mojo> mojos = pluginMetadata.getMojos();
71
72 if ( mojos != null && !mojos.isEmpty() )
73 {
74 for ( Mojo mojo :mojos )
75 {
76 MojoDescriptor descriptor = asDescriptor( metadataFile, mojo );
77
78 descriptors.add( descriptor );
79 }
80 }
81 }
82 catch ( IOException e )
83 {
84 throw new PluginMetadataParseException( metadataFile, "Cannot parse plugin metadata from file.", e );
85 }
86 catch ( XmlPullParserException e )
87 {
88 throw new PluginMetadataParseException( metadataFile, "Cannot parse plugin metadata from file.", e );
89 }
90 finally
91 {
92 IOUtil.close( reader );
93 }
94
95 return descriptors;
96 }
97
98
99
100
101
102
103
104 private MojoDescriptor asDescriptor( File metadataFile, Mojo mojo )
105 throws PluginMetadataParseException
106 {
107 MojoDescriptor descriptor = new MojoDescriptor();
108
109 if ( mojo.getCall() != null )
110 {
111 descriptor.setImplementation( IMPL_BASE_PLACEHOLDER + ":" + mojo.getCall() );
112 }
113 else
114 {
115 descriptor.setImplementation( IMPL_BASE_PLACEHOLDER );
116 }
117
118 descriptor.setGoal( mojo.getGoal() );
119 descriptor.setPhase( mojo.getPhase() );
120 descriptor.setDependencyResolutionRequired( mojo.getRequiresDependencyResolution() );
121 descriptor.setAggregator( mojo.isAggregator() );
122 descriptor.setInheritedByDefault( mojo.isInheritByDefault() );
123 descriptor.setDirectInvocationOnly( mojo.isRequiresDirectInvocation() );
124 descriptor.setOnlineRequired( mojo.isRequiresOnline() );
125 descriptor.setProjectRequired( mojo.isRequiresProject() );
126 descriptor.setRequiresReports( mojo.isRequiresReports() );
127 descriptor.setDescription( mojo.getDescription() );
128 descriptor.setDeprecated( mojo.getDeprecation() );
129 descriptor.setSince( mojo.getSince() );
130
131 LifecycleExecution le = mojo.getExecution();
132 if ( le != null )
133 {
134 descriptor.setExecuteLifecycle( le.getLifecycle() );
135 descriptor.setExecutePhase( le.getPhase() );
136 descriptor.setExecuteGoal( le.getGoal() );
137 }
138
139 List<org.apache.maven.tools.plugin.extractor.model.Parameter> parameters = mojo.getParameters();
140
141 if ( parameters != null && !parameters.isEmpty() )
142 {
143 for ( org.apache.maven.tools.plugin.extractor.model.Parameter param : parameters )
144 {
145 Parameter dParam = new Parameter();
146 dParam.setAlias( param.getAlias() );
147 dParam.setDeprecated( param.getDeprecation() );
148 dParam.setDescription( param.getDescription() );
149 dParam.setEditable( !param.isReadonly() );
150 dParam.setExpression( param.getExpression() );
151 dParam.setDefaultValue( param.getDefaultValue() );
152 dParam.setSince( param.getSince() );
153
154 String property = param.getProperty();
155 if ( StringUtils.isNotEmpty( property ) )
156 {
157 dParam.setName( property );
158 }
159 else
160 {
161 dParam.setName( param.getName() );
162 }
163
164 if ( StringUtils.isEmpty( dParam.getName() ) )
165 {
166 throw new PluginMetadataParseException( metadataFile, "Mojo: \'" + mojo.getGoal()
167 + "\' has a parameter without either property or name attributes. Please specify one." );
168 }
169
170 dParam.setRequired( param.isRequired() );
171 dParam.setType( param.getType() );
172
173 try
174 {
175 descriptor.addParameter( dParam );
176 }
177 catch ( DuplicateParameterException e )
178 {
179 throw new PluginMetadataParseException( metadataFile,
180 "Duplicate parameters detected for mojo: "
181 + mojo.getGoal(), e );
182 }
183 }
184 }
185
186 List<Component> components = mojo.getComponents();
187
188 if ( components != null && !components.isEmpty() )
189 {
190 for ( Component component : components )
191 {
192 ComponentRequirement cr = new ComponentRequirement();
193 cr.setRole( component.getRole() );
194 cr.setRoleHint( component.getHint() );
195
196 descriptor.addRequirement( cr );
197 }
198 }
199
200 return descriptor;
201 }
202 }