Coverage Report - org.apache.maven.tools.plugin.extractor.ant.AntMojoDescriptorExtractor
 
Classes in this File Line Coverage Branch Coverage Complexity
AntMojoDescriptorExtractor
94 %
92/97
80 %
24/30
7
 
 1  
 package org.apache.maven.tools.plugin.extractor.ant;
 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.util.ArrayList;
 24  
 import java.util.HashMap;
 25  
 import java.util.List;
 26  
 import java.util.Map;
 27  
 import java.util.Set;
 28  
 
 29  
 import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException;
 30  
 import org.apache.maven.plugin.descriptor.MojoDescriptor;
 31  
 import org.apache.maven.plugin.descriptor.Parameter;
 32  
 import org.apache.maven.plugin.tools.model.PluginMetadataParseException;
 33  
 import org.apache.maven.plugin.tools.model.PluginMetadataParser;
 34  
 import org.apache.maven.project.MavenProject;
 35  
 import org.apache.maven.project.path.PathTranslator;
 36  
 import org.apache.maven.tools.plugin.PluginToolsRequest;
 37  
 import org.apache.maven.tools.plugin.extractor.AbstractScriptedMojoDescriptorExtractor;
 38  
 import org.apache.maven.tools.plugin.extractor.ExtractionException;
 39  
 import org.apache.maven.tools.plugin.extractor.MojoDescriptorExtractor;
 40  
 import org.codehaus.plexus.component.annotations.Component;
 41  
 import org.codehaus.plexus.component.repository.ComponentRequirement;
 42  
 import org.codehaus.plexus.util.StringUtils;
 43  
 
 44  
 /**
 45  
  * Extracts Mojo descriptors from <a href="http://ant.apache.org">Ant</a> sources.
 46  
  *
 47  
  * @version $Id: AntMojoDescriptorExtractor.java 1343107 2012-05-27 21:35:40Z hboutemy $
 48  
  */
 49  
 @Component( role = MojoDescriptorExtractor.class, hint = "ant" )
 50  2
 public class AntMojoDescriptorExtractor
 51  
     extends AbstractScriptedMojoDescriptorExtractor
 52  
     implements MojoDescriptorExtractor
 53  
 {
 54  
     /** Default metadata file extension */
 55  
     private static final String METADATA_FILE_EXTENSION = ".mojos.xml";
 56  
 
 57  
     /** Default Ant build file extension */
 58  
     private static final String SCRIPT_FILE_EXTENSION = ".build.xml";
 59  
     
 60  
     /** {@inheritDoc} */
 61  
     protected List<MojoDescriptor> extractMojoDescriptorsFromMetadata( Map<String, Set<File>> metadataFilesKeyedByBasedir,
 62  
                                                                        PluginToolsRequest request )
 63  
         throws ExtractionException, InvalidPluginDescriptorException
 64  
     {
 65  2
         List<MojoDescriptor> descriptors = new ArrayList<MojoDescriptor>();
 66  
 
 67  2
         PluginMetadataParser parser = new PluginMetadataParser();
 68  
 
 69  2
         for ( Map.Entry<String, Set<File>> entry : metadataFilesKeyedByBasedir.entrySet() )
 70  
         {
 71  2
             String basedir = entry.getKey();
 72  2
             Set<File> metadataFiles = entry.getValue();
 73  
 
 74  2
             for ( File metadataFile : metadataFiles )
 75  
             {
 76  2
                 String basename = metadataFile.getName();
 77  2
                 basename = basename.substring( 0, basename.length() - METADATA_FILE_EXTENSION.length() );
 78  
 
 79  2
                 File scriptFile = new File( metadataFile.getParentFile(), basename + SCRIPT_FILE_EXTENSION );
 80  
 
 81  2
                 if ( !scriptFile.exists() )
 82  
                 {
 83  0
                     throw new InvalidPluginDescriptorException(
 84  
                         "Found orphaned plugin metadata file: " + metadataFile );
 85  
                 }
 86  
 
 87  2
                 String relativePath = scriptFile.getPath().substring( basedir.length() ).replace( '\\', '/' );
 88  
                 
 89  2
                 if ( relativePath.startsWith( "/" ) )
 90  
                 {
 91  2
                     relativePath = relativePath.substring( 1 );
 92  
                 }
 93  
 
 94  
                 try
 95  
                 {
 96  2
                     Set<MojoDescriptor> mojoDescriptors = parser.parseMojoDescriptors( metadataFile );
 97  
 
 98  2
                     for ( MojoDescriptor descriptor : mojoDescriptors )
 99  
                     {
 100  
                         @SuppressWarnings( "unchecked" )
 101  4
                         Map<String, ?> paramMap = descriptor.getParameterMap();
 102  
 
 103  4
                         if ( !paramMap.containsKey( "basedir" ) )
 104  
                         {
 105  4
                             Parameter param = new Parameter();
 106  4
                             param.setName( "basedir" );
 107  4
                             param.setAlias( "ant.basedir" );
 108  4
                             param.setExpression( "${antBasedir}" );
 109  4
                             param.setDefaultValue( "${basedir}" );
 110  4
                             param.setType( "java.io.File" );
 111  4
                             param.setDescription( "The base directory from which to execute the Ant script." );
 112  4
                             param.setEditable( true );
 113  4
                             param.setRequired( true );
 114  
 
 115  4
                             descriptor.addParameter( param );
 116  
                         }
 117  
 
 118  4
                         if ( !paramMap.containsKey( "antMessageLevel" ) )
 119  
                         {
 120  4
                             Parameter param = new Parameter();
 121  4
                             param.setName( "messageLevel" );
 122  4
                             param.setAlias( "ant.messageLevel" );
 123  4
                             param.setExpression( "${antMessageLevel}" );
 124  4
                             param.setDefaultValue( "info" );
 125  4
                             param.setType( "java.lang.String" );
 126  4
                             param.setDescription( "The message-level used to tune the verbosity of Ant logging." );
 127  4
                             param.setEditable( true );
 128  4
                             param.setRequired( false );
 129  
 
 130  4
                             descriptor.addParameter( param );
 131  
                         }
 132  
                         
 133  4
                         if ( !paramMap.containsKey( "project" ) )
 134  
                         {
 135  2
                             Parameter param = new Parameter();
 136  2
                             param.setName( "project" );
 137  2
                             param.setDefaultValue( "${project}" );
 138  2
                             param.setType( MavenProject.class.getName() );
 139  2
                             param.setDescription( "The current MavenProject instance, which contains classpath elements." );
 140  2
                             param.setEditable( false );
 141  2
                             param.setRequired( true );
 142  
 
 143  2
                             descriptor.addParameter( param );
 144  
                         }
 145  
 
 146  4
                         if ( !paramMap.containsKey( "session" ) )
 147  
                         {
 148  2
                             Parameter param = new Parameter();
 149  2
                             param.setName( "session" );
 150  2
                             param.setDefaultValue( "${session}" );
 151  2
                             param.setType( "org.apache.maven.execution.MavenSession" );
 152  2
                             param.setDescription( "The current MavenSession instance, which is used for plugin-style expression resolution." );
 153  2
                             param.setEditable( false );
 154  2
                             param.setRequired( true );
 155  
 
 156  2
                             descriptor.addParameter( param );
 157  
                         }
 158  
 
 159  4
                         if ( !paramMap.containsKey( "mojoExecution" ) )
 160  
                         {
 161  2
                             Parameter param = new Parameter();
 162  2
                             param.setName( "mojoExecution" );
 163  2
                             param.setDefaultValue( "${mojoExecution}" );
 164  2
                             param.setType( "org.apache.maven.plugin.MojoExecution" );
 165  2
                             param.setDescription( "The current Maven MojoExecution instance, which contains information about the mojo currently executing." );
 166  2
                             param.setEditable( false );
 167  2
                             param.setRequired( true );
 168  
 
 169  2
                             descriptor.addParameter( param );
 170  
                         }
 171  
                         
 172  
                         @SuppressWarnings( "unchecked" )
 173  4
                         List<ComponentRequirement> requirements = descriptor.getRequirements();
 174  4
                         Map<String, ComponentRequirement> reqMap = new HashMap<String, ComponentRequirement>();
 175  
 
 176  4
                         if ( requirements != null )
 177  
                         {
 178  4
                             for ( ComponentRequirement req : requirements )
 179  
                             {
 180  2
                                 reqMap.put( req.getRole(), req );
 181  
                             }
 182  
                         }
 183  
                         
 184  4
                         if ( !reqMap.containsKey( PathTranslator.class.getName() ) )
 185  
                         {
 186  2
                             ComponentRequirement req = new ComponentRequirement();
 187  2
                             req.setRole( PathTranslator.class.getName() );
 188  
                             
 189  2
                             descriptor.addRequirement( req );
 190  
                         }
 191  
 
 192  4
                         String implementation = relativePath;
 193  
 
 194  4
                         String dImpl = descriptor.getImplementation();
 195  4
                         if ( StringUtils.isNotEmpty( dImpl ) )
 196  
                         {
 197  4
                             if ( PluginMetadataParser.IMPL_BASE_PLACEHOLDER.equals( dImpl ) )
 198  
                             {
 199  2
                                 implementation = relativePath;
 200  
                             }
 201  
                             else
 202  
                             {
 203  2
                                 implementation =
 204  
                                     relativePath + dImpl.substring( PluginMetadataParser.IMPL_BASE_PLACEHOLDER.length() );
 205  
                             }
 206  
                         }
 207  
 
 208  4
                         descriptor.setImplementation( implementation );
 209  
 
 210  4
                         descriptor.setLanguage( "ant-mojo" );
 211  4
                         descriptor.setComponentComposer( "map-oriented" );
 212  4
                         descriptor.setComponentConfigurator( "map-oriented" );
 213  
 
 214  4
                         descriptor.setPluginDescriptor( request.getPluginDescriptor() );
 215  
 
 216  4
                         descriptors.add( descriptor );
 217  4
                     }
 218  
                 }
 219  0
                 catch ( PluginMetadataParseException e )
 220  
                 {
 221  0
                     throw new ExtractionException( "Error extracting mojo descriptor from script: " + metadataFile, e );
 222  2
                 }
 223  2
             }
 224  2
         }
 225  
 
 226  2
         return descriptors;
 227  
     }
 228  
 
 229  
     /** {@inheritDoc} */
 230  
     protected String getScriptFileExtension( PluginToolsRequest request )
 231  
     {
 232  0
         return SCRIPT_FILE_EXTENSION;
 233  
     }
 234  
 
 235  
     /** {@inheritDoc} */
 236  
     protected String getMetadataFileExtension( PluginToolsRequest request )
 237  
     {
 238  0
         return METADATA_FILE_EXTENSION;
 239  
     }
 240  
 }