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