Coverage Report - org.apache.maven.plugin.tools.model.PluginMetadataParser
 
Classes in this File Line Coverage Branch Coverage Complexity
PluginMetadataParser
77 %
54/70
57 %
16/28
11
 
 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 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.plugin.tools.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  
  * Parser for plugin metadata.
 41  
  *
 42  
  * @version $Id: PluginMetadataParser.java 1133707 2011-06-09 08:28:59Z stephenc $
 43  
  */
 44  2
 public class PluginMetadataParser
 45  
 {
 46  
     /** Default implementation path which will be replaced in
 47  
      * AbstractScriptedMojoDescriptorExtractor#extractMojoDescriptorsFromMetadata(Map, PluginDescriptor) */
 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 )
 56  
         throws PluginMetadataParseException
 57  
     {
 58  2
         Set<MojoDescriptor> descriptors = new HashSet<MojoDescriptor>();
 59  
 
 60  2
         Reader reader = null;
 61  
 
 62  
         try
 63  
         {
 64  2
             reader = ReaderFactory.newXmlReader( metadataFile );
 65  
 
 66  2
             PluginMetadataXpp3Reader metadataReader = new PluginMetadataXpp3Reader();
 67  
 
 68  2
             PluginMetadata pluginMetadata = metadataReader.read( reader );
 69  
 
 70  2
             List<Mojo> mojos = pluginMetadata.getMojos();
 71  
 
 72  2
             if ( mojos != null && !mojos.isEmpty() )
 73  
             {
 74  2
                 for ( Mojo mojo :mojos )
 75  
                 {
 76  2
                     MojoDescriptor descriptor = asDescriptor( metadataFile, mojo );
 77  
 
 78  2
                     descriptors.add( descriptor );
 79  2
                 }
 80  
             }
 81  
         }
 82  0
         catch ( IOException e )
 83  
         {
 84  0
             throw new PluginMetadataParseException( metadataFile, "Cannot parse plugin metadata from file.", e );
 85  
         }
 86  0
         catch ( XmlPullParserException e )
 87  
         {
 88  0
             throw new PluginMetadataParseException( metadataFile, "Cannot parse plugin metadata from file.", e );
 89  
         }
 90  
         finally
 91  
         {
 92  2
             IOUtil.close( reader );
 93  2
         }
 94  
 
 95  2
         return descriptors;
 96  
     }
 97  
 
 98  
     /**
 99  
      * @param metadataFile not null
 100  
      * @param mojo not null
 101  
      * @return a mojo descriptor instance
 102  
      * @throws PluginMetadataParseException if any
 103  
      */
 104  
     private MojoDescriptor asDescriptor( File metadataFile, Mojo mojo )
 105  
         throws PluginMetadataParseException
 106  
     {
 107  2
         MojoDescriptor descriptor = new MojoDescriptor();
 108  
 
 109  2
         if ( mojo.getCall() != null )
 110  
         {
 111  1
             descriptor.setImplementation( IMPL_BASE_PLACEHOLDER + ":" + mojo.getCall() );
 112  
         }
 113  
         else
 114  
         {
 115  1
             descriptor.setImplementation( IMPL_BASE_PLACEHOLDER );
 116  
         }
 117  
 
 118  2
         descriptor.setGoal( mojo.getGoal() );
 119  2
         descriptor.setPhase( mojo.getPhase() );
 120  2
         descriptor.setDependencyResolutionRequired( mojo.getRequiresDependencyResolution() );
 121  2
         descriptor.setAggregator( mojo.isAggregator() );
 122  2
         descriptor.setInheritedByDefault( mojo.isInheritByDefault() );
 123  2
         descriptor.setDirectInvocationOnly( mojo.isRequiresDirectInvocation() );
 124  2
         descriptor.setOnlineRequired( mojo.isRequiresOnline() );
 125  2
         descriptor.setProjectRequired( mojo.isRequiresProject() );
 126  2
         descriptor.setRequiresReports( mojo.isRequiresReports() );
 127  2
         descriptor.setDescription( mojo.getDescription() );
 128  2
         descriptor.setDeprecated( mojo.getDeprecation() );
 129  
 
 130  2
         LifecycleExecution le = mojo.getExecution();
 131  2
         if ( le != null )
 132  
         {
 133  0
             descriptor.setExecuteLifecycle( le.getLifecycle() );
 134  0
             descriptor.setExecutePhase( le.getPhase() );
 135  
         }
 136  
 
 137  2
         List<org.apache.maven.plugin.tools.model.Parameter> parameters = mojo.getParameters();
 138  
 
 139  2
         if ( parameters != null && !parameters.isEmpty() )
 140  
         {
 141  2
             for ( org.apache.maven.plugin.tools.model.Parameter param : parameters )
 142  
             {
 143  2
                 Parameter dParam = new Parameter();
 144  2
                 dParam.setAlias( param.getAlias() );
 145  2
                 dParam.setDeprecated( param.getDeprecation() );
 146  2
                 dParam.setDescription( param.getDescription() );
 147  2
                 dParam.setEditable( !param.isReadonly() );
 148  2
                 dParam.setExpression( param.getExpression() );
 149  2
                 dParam.setDefaultValue( param.getDefaultValue() );
 150  
 
 151  2
                 String property = param.getProperty();
 152  2
                 if ( StringUtils.isNotEmpty( property ) )
 153  
                 {
 154  2
                     dParam.setName( property );
 155  
                 }
 156  
                 else
 157  
                 {
 158  0
                     dParam.setName( param.getName() );
 159  
                 }
 160  
 
 161  2
                 if ( StringUtils.isEmpty( dParam.getName() ) )
 162  
                 {
 163  0
                     throw new PluginMetadataParseException( metadataFile, "Mojo: \'" + mojo.getGoal()
 164  
                         + "\' has a parameter without either property or name attributes. Please specify one." );
 165  
                 }
 166  
 
 167  2
                 dParam.setRequired( param.isRequired() );
 168  2
                 dParam.setType( param.getType() );
 169  
 
 170  
                 try
 171  
                 {
 172  2
                     descriptor.addParameter( dParam );
 173  
                 }
 174  0
                 catch ( DuplicateParameterException e )
 175  
                 {
 176  0
                     throw new PluginMetadataParseException( metadataFile,
 177  
                                                             "Duplicate parameters detected for mojo: "
 178  
                                                                 + mojo.getGoal(), e );
 179  2
                 }
 180  2
             }
 181  
         }
 182  
 
 183  2
         List<Component> components = mojo.getComponents();
 184  
 
 185  2
         if ( components != null && !components.isEmpty() )
 186  
         {
 187  0
             for ( Component component : components )
 188  
             {
 189  0
                 ComponentRequirement cr = new ComponentRequirement();
 190  0
                 cr.setRole( component.getRole() );
 191  0
                 cr.setRoleHint( component.getHint() );
 192  
 
 193  0
                 descriptor.addRequirement( cr );
 194  0
             }
 195  
         }
 196  
 
 197  2
         return descriptor;
 198  
     }
 199  
 }