View Javadoc

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