Coverage Report - org.apache.maven.plugins.help.EvaluateMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
EvaluateMojo
0 %
1/213
0 %
0/114
5,824
EvaluateMojo$1
0 %
0/11
0 %
0/6
5,824
 
 1  
 package org.apache.maven.plugins.help;
 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.FileInputStream;
 24  
 import java.io.IOException;
 25  
 import java.io.InputStream;
 26  
 import java.io.StringWriter;
 27  
 import java.util.Iterator;
 28  
 import java.util.List;
 29  
 import java.util.Locale;
 30  
 import java.util.Map;
 31  
 import java.util.Properties;
 32  
 import java.util.TreeMap;
 33  
 import java.util.jar.JarEntry;
 34  
 import java.util.jar.JarInputStream;
 35  
 
 36  
 import org.apache.commons.lang.ClassUtils;
 37  
 import org.apache.maven.artifact.Artifact;
 38  
 import org.apache.maven.artifact.ArtifactUtils;
 39  
 import org.apache.maven.artifact.factory.ArtifactFactory;
 40  
 import org.apache.maven.artifact.repository.ArtifactRepository;
 41  
 import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
 42  
 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
 43  
 import org.apache.maven.artifact.resolver.ArtifactResolver;
 44  
 import org.apache.maven.execution.MavenSession;
 45  
 import org.apache.maven.model.Dependency;
 46  
 import org.apache.maven.model.Model;
 47  
 import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
 48  
 import org.apache.maven.plugin.AbstractMojo;
 49  
 import org.apache.maven.plugin.MojoExecution;
 50  
 import org.apache.maven.plugin.MojoExecutionException;
 51  
 import org.apache.maven.plugin.MojoFailureException;
 52  
 import org.apache.maven.plugin.PluginParameterExpressionEvaluator;
 53  
 import org.apache.maven.plugin.descriptor.MojoDescriptor;
 54  
 import org.apache.maven.project.MavenProject;
 55  
 import org.apache.maven.project.MavenProjectBuilder;
 56  
 import org.apache.maven.project.ProjectBuildingException;
 57  
 import org.apache.maven.project.path.PathTranslator;
 58  
 import org.apache.maven.settings.Settings;
 59  
 import org.apache.maven.settings.io.xpp3.SettingsXpp3Writer;
 60  
 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
 61  
 import org.codehaus.plexus.components.interactivity.InputHandler;
 62  
 import org.codehaus.plexus.util.IOUtil;
 63  
 import org.codehaus.plexus.util.StringUtils;
 64  
 
 65  
 import com.thoughtworks.xstream.XStream;
 66  
 import com.thoughtworks.xstream.converters.MarshallingContext;
 67  
 import com.thoughtworks.xstream.converters.collections.PropertiesConverter;
 68  
 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
 69  
 
 70  
 /**
 71  
  * Evaluates Maven expressions given by the user in an interactive mode.
 72  
  *
 73  
  * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
 74  
  * @version $Id: EvaluateMojo.java 689794 2008-08-28 12:08:43Z vsiveton $
 75  
  * @since 2.1
 76  
  * @goal evaluate
 77  
  * @requiresProject false
 78  
  */
 79  1
 public class EvaluateMojo
 80  
     extends AbstractMojo
 81  
 {
 82  
     // ----------------------------------------------------------------------
 83  
     // Mojo components
 84  
     // ----------------------------------------------------------------------
 85  
 
 86  
     /**
 87  
      * Maven Artifact Factory component.
 88  
      *
 89  
      * @component
 90  
      */
 91  
     private ArtifactFactory artifactFactory;
 92  
 
 93  
     /**
 94  
      * Input handler, needed for command line handling.
 95  
      *
 96  
      * @component
 97  
      */
 98  
     private InputHandler inputHandler;
 99  
 
 100  
     /**
 101  
      * Maven Project Builder component.
 102  
      *
 103  
      * @component
 104  
      */
 105  
     private MavenProjectBuilder mavenProjectBuilder;
 106  
 
 107  
     /**
 108  
      * @component
 109  
      */
 110  
     private PathTranslator pathTranslator;
 111  
 
 112  
     /**
 113  
      * Artifact Resolver component.
 114  
      *
 115  
      * @component
 116  
      */
 117  
     private ArtifactResolver resolver;
 118  
 
 119  
     // ----------------------------------------------------------------------
 120  
     // Mojo parameters
 121  
     // ----------------------------------------------------------------------
 122  
 
 123  
     /**
 124  
      * An artifact for evaluating Maven expressions.
 125  
      * <br/>
 126  
      * <b>Note</b>: Should respect the Maven format, i.e. <code>groupId:artifactId[:version][:classifier]</code>.
 127  
      *
 128  
      * @parameter expression="${artifact}"
 129  
      */
 130  
     private String artifact;
 131  
 
 132  
     /**
 133  
      * Local Repository.
 134  
      *
 135  
      * @parameter expression="${localRepository}"
 136  
      * @required
 137  
      * @readonly
 138  
      */
 139  
     protected ArtifactRepository localRepository;
 140  
 
 141  
     /**
 142  
      * The current Maven project or the super pom.
 143  
      *
 144  
      * @parameter expression="${project}"
 145  
      * @readonly
 146  
      * @required
 147  
      */
 148  
     protected MavenProject project;
 149  
 
 150  
     /**
 151  
      * Remote repositories used for the project.
 152  
      *
 153  
      * @parameter expression="${project.remoteArtifactRepositories}"
 154  
      * @readonly
 155  
      * @required
 156  
      */
 157  
     private List remoteRepositories;
 158  
 
 159  
     /**
 160  
      * The system settings for Maven.
 161  
      *
 162  
      * @parameter expression="${settings}"
 163  
      * @readonly
 164  
      * @required
 165  
      */
 166  
     protected Settings settings;
 167  
 
 168  
     /**
 169  
      * The current Maven session.
 170  
      *
 171  
      * @parameter expression="${session}"
 172  
      * @required
 173  
      * @readonly
 174  
      */
 175  
     private MavenSession session;
 176  
 
 177  
     // ----------------------------------------------------------------------
 178  
     // Instance variables
 179  
     // ----------------------------------------------------------------------
 180  
 
 181  
     /** lazy loading evaluator variable */
 182  
     private PluginParameterExpressionEvaluator evaluator;
 183  
 
 184  
     /** lazy loading xstream variable */
 185  
     private XStream xstream;
 186  
 
 187  
     // ----------------------------------------------------------------------
 188  
     // Public methods
 189  
     // ----------------------------------------------------------------------
 190  
 
 191  
     /** {@inheritDoc} */
 192  
     public void execute()
 193  
         throws MojoExecutionException, MojoFailureException
 194  
     {
 195  0
         if ( !settings.isInteractiveMode() )
 196  
         {
 197  0
             StringBuffer msg = new StringBuffer();
 198  0
             msg.append( "Maven is configured to NOT interact with the user for input. " );
 199  0
             msg.append( "This Mojo requires that 'interactiveMode' in your settings file is flag to 'true'." );
 200  
 
 201  0
             getLog().error( msg.toString() );
 202  0
             return;
 203  
         }
 204  
 
 205  0
         validateParameters();
 206  
 
 207  0
         if ( StringUtils.isNotEmpty( artifact ) )
 208  
         {
 209  0
             Artifact artifactObj = getArtifact( artifact );
 210  
 
 211  
             try
 212  
             {
 213  0
                 project = getMavenProject( artifactObj );
 214  
             }
 215  0
             catch ( ProjectBuildingException e )
 216  
             {
 217  0
                 throw new MojoExecutionException( "Unable to get the POM for the artifact '" + artifact
 218  
                     + "'. Verify the artifact parameter." );
 219  0
             }
 220  
         }
 221  
 
 222  
         while ( true )
 223  
         {
 224  0
             getLog().info( "Enter the Maven expression i.e. ${project.groupId} or 0 to exit?:" );
 225  
 
 226  
             try
 227  
             {
 228  0
                 String userExpression = inputHandler.readLine();
 229  0
                 if ( userExpression == null || userExpression.toLowerCase( Locale.ENGLISH ).equals( "0" ) )
 230  
                 {
 231  0
                     break;
 232  
                 }
 233  
 
 234  0
                 handleResponse( userExpression );
 235  
             }
 236  0
             catch ( IOException e )
 237  
             {
 238  0
                 throw new MojoExecutionException( "Unable to read from standard input.", e );
 239  0
             }
 240  
         }
 241  0
     }
 242  
 
 243  
     // ----------------------------------------------------------------------
 244  
     // Private methods
 245  
     // ----------------------------------------------------------------------
 246  
 
 247  
     /**
 248  
      * Validate Mojo parameters.
 249  
      */
 250  
     private void validateParameters()
 251  
     {
 252  0
         if ( artifact == null )
 253  
         {
 254  
             // using project if found or super-pom
 255  0
             getLog().info( "No artifact parameter specified, using '" + project.getId() + "' as project." );
 256  
         }
 257  0
     }
 258  
 
 259  
     /**
 260  
      * @param artifactString should respect the format <code>groupId:artifactId[:version][:classifier]</code>
 261  
      * @return the <code>Artifact</code> object for the <code>artifactString</code> parameter.
 262  
      * @throws MojoExecutionException if the <code>artifactString</code> doesn't respect the format.
 263  
      */
 264  
     private Artifact getArtifact( String artifactString )
 265  
         throws MojoExecutionException
 266  
     {
 267  0
         if ( StringUtils.isEmpty( artifactString ) )
 268  
         {
 269  0
             throw new IllegalArgumentException( "artifact parameter could not be empty" );
 270  
         }
 271  
 
 272  0
         String groupId = null; // required
 273  0
         String artifactId = null; // required
 274  0
         String version = null; // optional
 275  0
         String classifier = null; // optional
 276  
 
 277  0
         String[] artifactParts = artifactString.split( ":" );
 278  
 
 279  0
         switch ( artifactParts.length )
 280  
         {
 281  
             case ( 2 ):
 282  0
                 groupId = artifactParts[0];
 283  0
                 artifactId = artifactParts[1];
 284  0
                 version = Artifact.LATEST_VERSION;
 285  0
                 break;
 286  
             case ( 3 ):
 287  0
                 groupId = artifactParts[0];
 288  0
                 artifactId = artifactParts[1];
 289  0
                 version = artifactParts[2];
 290  0
                 break;
 291  
             case ( 4 ):
 292  0
                 groupId = artifactParts[0];
 293  0
                 artifactId = artifactParts[1];
 294  0
                 version = artifactParts[2];
 295  0
                 classifier = artifactParts[3];
 296  0
                 break;
 297  
             default:
 298  0
                 throw new MojoExecutionException( "The artifact parameter '" + artifactString
 299  
                     + "' should be conform to: " + "'groupId:artifactId[:version][:classifier]'." );
 300  
         }
 301  
 
 302  0
         if ( StringUtils.isNotEmpty( classifier ) )
 303  
         {
 304  0
             return artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, "jar", classifier );
 305  
         }
 306  
 
 307  0
         return artifactFactory.createArtifact( groupId, artifactId, version, Artifact.SCOPE_COMPILE, "jar" );
 308  
     }
 309  
 
 310  
     /**
 311  
      * @param artifactObj not null
 312  
      * @return the POM for the given artifact.
 313  
      * @throws MojoExecutionException if the artifact has a system scope.
 314  
      * @throws ProjectBuildingException when building pom.
 315  
      */
 316  
     private MavenProject getMavenProject( Artifact artifactObj )
 317  
         throws MojoExecutionException, ProjectBuildingException
 318  
     {
 319  0
         if ( Artifact.SCOPE_SYSTEM.equals( artifactObj.getScope() ) )
 320  
         {
 321  0
             throw new MojoExecutionException( "System artifact is not be handled." );
 322  
         }
 323  
 
 324  0
         Artifact copyArtifact = ArtifactUtils.copyArtifact( artifactObj );
 325  0
         if ( !"pom".equals( copyArtifact.getType() ) )
 326  
         {
 327  0
             copyArtifact =
 328  
                 artifactFactory.createProjectArtifact( copyArtifact.getGroupId(), copyArtifact.getArtifactId(),
 329  
                                                        copyArtifact.getVersion(), copyArtifact.getScope() );
 330  
         }
 331  
 
 332  0
         return mavenProjectBuilder.buildFromRepository( copyArtifact, remoteRepositories, localRepository );
 333  
     }
 334  
 
 335  
     /**
 336  
      * @return a lazy loading evaluator object.
 337  
      * @throws MojoExecutionException if any
 338  
      * @throws MojoFailureException if any reflection exceptions occur or missing components.
 339  
      * @see #getMojoDescriptor(String, MavenSession, MavenProject, String, boolean, boolean)
 340  
      */
 341  
     private PluginParameterExpressionEvaluator getEvaluator()
 342  
         throws MojoExecutionException, MojoFailureException
 343  
     {
 344  0
         if ( evaluator == null )
 345  
         {
 346  0
             MojoDescriptor mojoDescriptor =
 347  
                 HelpUtil.getMojoDescriptor( "help:evaluate", session, project, "help:evaluate", true, false );
 348  0
             MojoExecution mojoExecution = new MojoExecution( mojoDescriptor );
 349  0
             evaluator =
 350  
                 new PluginParameterExpressionEvaluator( session, mojoExecution, pathTranslator,
 351  
                                                         session.getContainer().getLogger(), project,
 352  
                                                         session.getExecutionProperties() );
 353  
         }
 354  
 
 355  0
         return evaluator;
 356  
     }
 357  
 
 358  
     /**
 359  
      * @param expression the user expression asked.
 360  
      * @throws MojoExecutionException if any
 361  
      * @throws MojoFailureException if any reflection exceptions occur or missing components.
 362  
      */
 363  
     private void handleResponse( String expression )
 364  
         throws MojoExecutionException, MojoFailureException
 365  
     {
 366  0
         StringBuffer response = new StringBuffer();
 367  
 
 368  
         Object obj;
 369  
         try
 370  
         {
 371  0
             obj = getEvaluator().evaluate( expression );
 372  
         }
 373  0
         catch ( ExpressionEvaluationException e )
 374  
         {
 375  0
             throw new MojoExecutionException( "Error when evaluating the Maven expression", e );
 376  0
         }
 377  
 
 378  0
         if ( obj != null && expression.equals( obj.toString() ) )
 379  
         {
 380  0
             getLog().warn( "The Maven expression was invalid. Please use a valid expression." );
 381  0
             return;
 382  
         }
 383  
 
 384  
         // handle null
 385  0
         if ( obj == null )
 386  
         {
 387  0
             response.append( "null object or invalid expression" );
 388  
         }
 389  
         // handle primitives objects
 390  0
         else if ( obj instanceof String )
 391  
         {
 392  0
             response.append( obj.toString() );
 393  
         }
 394  0
         else if ( obj instanceof Boolean )
 395  
         {
 396  0
             response.append( obj.toString() );
 397  
         }
 398  0
         else if ( obj instanceof Byte )
 399  
         {
 400  0
             response.append( obj.toString() );
 401  
         }
 402  0
         else if ( obj instanceof Character )
 403  
         {
 404  0
             response.append( obj.toString() );
 405  
         }
 406  0
         else if ( obj instanceof Double )
 407  
         {
 408  0
             response.append( obj.toString() );
 409  
         }
 410  0
         else if ( obj instanceof Float )
 411  
         {
 412  0
             response.append( obj.toString() );
 413  
         }
 414  0
         else if ( obj instanceof Integer )
 415  
         {
 416  0
             response.append( obj.toString() );
 417  
         }
 418  0
         else if ( obj instanceof Long )
 419  
         {
 420  0
             response.append( obj.toString() );
 421  
         }
 422  0
         else if ( obj instanceof Short )
 423  
         {
 424  0
             response.append( obj.toString() );
 425  
         }
 426  
         // handle specific objects
 427  0
         else if ( obj instanceof File )
 428  
         {
 429  0
             File f = (File) obj;
 430  0
             response.append( f.getAbsolutePath() );
 431  0
         }
 432  
         // handle Maven pom object
 433  0
         else if ( obj instanceof MavenProject )
 434  
         {
 435  0
             MavenProject projectAsked = (MavenProject) obj;
 436  0
             StringWriter sWriter = new StringWriter();
 437  0
             MavenXpp3Writer pomWriter = new MavenXpp3Writer();
 438  
             try
 439  
             {
 440  0
                 pomWriter.write( sWriter, projectAsked.getModel() );
 441  
             }
 442  0
             catch ( IOException e )
 443  
             {
 444  0
                 throw new MojoExecutionException( "Error when writing pom", e );
 445  0
             }
 446  
 
 447  0
             response.append( sWriter.toString() );
 448  0
         }
 449  
         // handle Maven Settings object
 450  0
         else if ( obj instanceof Settings )
 451  
         {
 452  0
             Settings settingsAsked = (Settings) obj;
 453  0
             StringWriter sWriter = new StringWriter();
 454  0
             SettingsXpp3Writer settingsWriter = new SettingsXpp3Writer();
 455  
             try
 456  
             {
 457  0
                 settingsWriter.write( sWriter, settingsAsked );
 458  
             }
 459  0
             catch ( IOException e )
 460  
             {
 461  0
                 throw new MojoExecutionException( "Error when writing settings", e );
 462  0
             }
 463  
 
 464  0
             response.append( sWriter.toString() );
 465  0
         }
 466  
         else
 467  
         {
 468  
             // others Maven objects
 469  0
             response.append( toXML( expression, obj ) );
 470  
         }
 471  
 
 472  0
         getLog().info( "\n" + response.toString() );
 473  0
     }
 474  
 
 475  
     /**
 476  
      * @param expression the user expression.
 477  
      * @param obj a not null.
 478  
      * @return the XML for the given object.
 479  
      */
 480  
     private String toXML( String expression, Object obj )
 481  
     {
 482  0
         XStream currentXStream = getXStream();
 483  
 
 484  
         // beautify list
 485  0
         if ( obj instanceof List )
 486  
         {
 487  0
             List list = (List) obj;
 488  0
             if ( list.size() > 0 )
 489  
             {
 490  0
                 Object elt = list.iterator().next();
 491  
 
 492  0
                 String name = StringUtils.lowercaseFirstLetter( ClassUtils.getShortClassName( elt.getClass() ) );
 493  0
                 currentXStream.alias( pluralize( name ), List.class );
 494  0
             }
 495  
             else
 496  
             {
 497  
                 // try to detect the alias from question
 498  0
                 if ( expression.indexOf( "." ) != -1 )
 499  
                 {
 500  0
                     String name = expression.substring( expression.indexOf( "." ) + 1, expression.indexOf( "}" ) );
 501  0
                     currentXStream.alias( name, List.class );
 502  
                 }
 503  
             }
 504  
         }
 505  
 
 506  0
         return currentXStream.toXML( obj );
 507  
     }
 508  
 
 509  
     /**
 510  
      * @return lazy loading xstream object.
 511  
      */
 512  
     private XStream getXStream()
 513  
     {
 514  0
         if ( xstream == null )
 515  
         {
 516  0
             xstream = new XStream();
 517  0
             addAlias( xstream );
 518  
 
 519  
             // handle Properties a la Maven
 520  0
             xstream.registerConverter( new PropertiesConverter()
 521  
             {
 522  
                 /** {@inheritDoc} */
 523  
                 public boolean canConvert( Class type )
 524  
                 {
 525  0
                     return Properties.class == type;
 526  
                 }
 527  
 
 528  
                 /** {@inheritDoc} */
 529  0
                 public void marshal( Object source, HierarchicalStreamWriter writer, MarshallingContext context )
 530  
                 {
 531  0
                     Properties properties = (Properties) source;
 532  0
                     Map map = new TreeMap( properties ); // sort
 533  0
                     for ( Iterator iterator = map.entrySet().iterator(); iterator.hasNext(); )
 534  
                     {
 535  0
                         Map.Entry entry = (Map.Entry) iterator.next();
 536  
 
 537  0
                         writer.startNode( entry.getKey().toString() );
 538  0
                         writer.setValue( entry.getValue().toString() );
 539  0
                         writer.endNode();
 540  0
                     }
 541  0
                 }
 542  
             } );
 543  
         }
 544  
 
 545  0
         return xstream;
 546  
     }
 547  
 
 548  
     /**
 549  
      * @param xstreamObject not null
 550  
      */
 551  
     private void addAlias( XStream xstreamObject )
 552  
     {
 553  
         try
 554  
         {
 555  0
             addAlias( xstreamObject, getMavenModelJarFile(), "org.apache.maven.model" );
 556  0
             addAlias( xstreamObject, getMavenSettingsJarFile(), "org.apache.maven.settings" );
 557  
         }
 558  0
         catch ( MojoExecutionException e )
 559  
         {
 560  0
             if ( getLog().isDebugEnabled() )
 561  
             {
 562  0
                 getLog().debug( "MojoExecutionException: " + e.getMessage(), e );
 563  
             }
 564  
         }
 565  0
         catch ( ArtifactResolutionException e )
 566  
         {
 567  0
             if ( getLog().isDebugEnabled() )
 568  
             {
 569  0
                 getLog().debug( "ArtifactResolutionException: " + e.getMessage(), e );
 570  
             }
 571  
         }
 572  0
         catch ( ArtifactNotFoundException e )
 573  
         {
 574  0
             if ( getLog().isDebugEnabled() )
 575  
             {
 576  0
                 getLog().debug( "ArtifactNotFoundException: " + e.getMessage(), e );
 577  
             }
 578  
         }
 579  0
         catch ( ProjectBuildingException e )
 580  
         {
 581  0
             if ( getLog().isDebugEnabled() )
 582  
             {
 583  0
                 getLog().debug( "ProjectBuildingException: " + e.getMessage(), e );
 584  
             }
 585  0
         }
 586  
 
 587  
         // TODO need to handle specific Maven objects like DefaultArtifact?
 588  0
     }
 589  
 
 590  
     /**
 591  
      * @param xstreamObject not null
 592  
      * @param jarFile not null
 593  
      * @param packageFilter a package name to filter.
 594  
      */
 595  
     private void addAlias( XStream xstreamObject, File jarFile, String packageFilter )
 596  
     {
 597  0
         JarInputStream jarStream = null;
 598  
         try
 599  
         {
 600  0
             jarStream = new JarInputStream( new FileInputStream( jarFile ) );
 601  0
             JarEntry jarEntry = jarStream.getNextJarEntry();
 602  0
             while ( jarEntry != null )
 603  
             {
 604  0
                 if ( jarEntry == null )
 605  
                 {
 606  0
                     break;
 607  
                 }
 608  
 
 609  0
                 if ( jarEntry.getName().toLowerCase( Locale.ENGLISH ).endsWith( ".class" ) )
 610  
                 {
 611  0
                     String name = jarEntry.getName().substring( 0, jarEntry.getName().indexOf( "." ) );
 612  0
                     name = name.replaceAll( "/", "\\." );
 613  
 
 614  0
                     if ( name.indexOf( packageFilter ) != -1 )
 615  
                     {
 616  
                         try
 617  
                         {
 618  0
                             Class clazz = ClassUtils.getClass( name );
 619  0
                             String alias = StringUtils.lowercaseFirstLetter( ClassUtils.getShortClassName( clazz ) );
 620  0
                             xstreamObject.alias( alias, clazz );
 621  0
                             if ( !clazz.equals( Model.class ) )
 622  
                             {
 623  0
                                 xstreamObject.omitField( clazz, "modelEncoding" ); // unnecessary field
 624  
                             }
 625  
                         }
 626  0
                         catch ( ClassNotFoundException e )
 627  
                         {
 628  0
                             e.printStackTrace();
 629  0
                         }
 630  
                     }
 631  
                 }
 632  
 
 633  0
                 jarStream.closeEntry();
 634  0
                 jarEntry = jarStream.getNextJarEntry();
 635  
             }
 636  
         }
 637  0
         catch ( IOException e )
 638  
         {
 639  0
             if ( getLog().isDebugEnabled() )
 640  
             {
 641  0
                 getLog().debug( "IOException: " + e.getMessage(), e );
 642  
             }
 643  
         }
 644  
         finally
 645  
         {
 646  0
             IOUtil.close( jarStream );
 647  0
         }
 648  0
     }
 649  
 
 650  
     /**
 651  
      * @return the <code>org.apache.maven:maven-model</code> artifact jar file in the local repository.
 652  
      * @throws MojoExecutionException if any
 653  
      * @throws ProjectBuildingException if any
 654  
      * @throws ArtifactResolutionException if any
 655  
      * @throws ArtifactNotFoundException if any
 656  
      */
 657  
     private File getMavenModelJarFile()
 658  
         throws MojoExecutionException, ProjectBuildingException, ArtifactResolutionException,
 659  
         ArtifactNotFoundException
 660  
     {
 661  0
         return getArtifactFile( true );
 662  
     }
 663  
 
 664  
     /**
 665  
      * @return the <code>org.apache.maven:maven-settings</code> artifact jar file in the local repository.
 666  
      * @throws MojoExecutionException if any
 667  
      * @throws ProjectBuildingException if any
 668  
      * @throws ArtifactResolutionException if any
 669  
      * @throws ArtifactNotFoundException if any
 670  
      */
 671  
     private File getMavenSettingsJarFile()
 672  
         throws MojoExecutionException, ProjectBuildingException, ArtifactResolutionException,
 673  
         ArtifactNotFoundException
 674  
     {
 675  0
         return getArtifactFile( false );
 676  
     }
 677  
 
 678  
     /**
 679  
      *
 680  
      * @param isPom <code>true</code> to lookup the <code>maven-model</code> artifact jar, <code>false</code> to
 681  
      * lookup the <code>maven-settings</code> artifact jar.
 682  
      * @return the <code>org.apache.maven:maven-model|maven-settings</code> artifact jar file for this current
 683  
      * HelpPlugin pom.
 684  
      * @throws MojoExecutionException if any
 685  
      * @throws ProjectBuildingException if any
 686  
      * @throws ArtifactResolutionException if any
 687  
      * @throws ArtifactNotFoundException if any
 688  
      */
 689  
     private File getArtifactFile( boolean isPom )
 690  
         throws MojoExecutionException, ProjectBuildingException, ArtifactResolutionException,
 691  
         ArtifactNotFoundException
 692  
     {
 693  0
         for ( Iterator it = getHelpPluginPom().getDependencies().iterator(); it.hasNext(); )
 694  
         {
 695  0
             Dependency depependency = (Dependency) it.next();
 696  
 
 697  0
             if ( !( depependency.getGroupId().equals( "org.apache.maven" ) ) )
 698  
             {
 699  0
                 continue;
 700  
             }
 701  
 
 702  0
             if ( isPom )
 703  
             {
 704  0
                 if ( !( depependency.getArtifactId().equals( "maven-model" ) ) )
 705  
                 {
 706  0
                     continue;
 707  
                 }
 708  
             }
 709  
             else
 710  
             {
 711  0
                 if ( !( depependency.getArtifactId().equals( "maven-settings" ) ) )
 712  
                 {
 713  0
                     continue;
 714  
                 }
 715  
             }
 716  
 
 717  0
             Artifact mavenArtifact =
 718  
                 getArtifact( depependency.getGroupId() + ":" + depependency.getArtifactId() + ":"
 719  
                     + depependency.getVersion() );
 720  0
             resolver.resolveAlways( mavenArtifact, remoteRepositories, localRepository );
 721  
 
 722  0
             return mavenArtifact.getFile();
 723  
         }
 724  
 
 725  0
         throw new MojoExecutionException( "Unable to find the 'org.apache.maven:"
 726  
             + ( isPom ? "maven-model" : "maven-settings" ) + "' artifact" );
 727  
     }
 728  
 
 729  
     /**
 730  
      * @return the Maven POM for the current help plugin
 731  
      * @throws MojoExecutionException if any
 732  
      * @throws ProjectBuildingException if any
 733  
      */
 734  
     private MavenProject getHelpPluginPom()
 735  
         throws MojoExecutionException, ProjectBuildingException
 736  
     {
 737  0
         String resource = "META-INF/maven/org.apache.maven.plugins/maven-help-plugin/pom.properties";
 738  
 
 739  0
         InputStream resourceAsStream = EvaluateMojo.class.getClassLoader().getResourceAsStream( resource );
 740  0
         Artifact helpPluginArtifact = null;
 741  0
         if ( resourceAsStream != null )
 742  
         {
 743  0
             Properties properties = new Properties();
 744  
             try
 745  
             {
 746  0
                 properties.load( resourceAsStream );
 747  
             }
 748  0
             catch ( IOException e )
 749  
             {
 750  0
                 if ( getLog().isDebugEnabled() )
 751  
                 {
 752  0
                     getLog().debug( "IOException: " + e.getMessage(), e );
 753  
                 }
 754  0
             }
 755  
 
 756  0
             String artifactString =
 757  
                 properties.getProperty( "groupId", "unknown" ) + ":"
 758  
                     + properties.getProperty( "artifactId", "unknown" ) + ":"
 759  
                     + properties.getProperty( "version", "unknown" );
 760  
 
 761  0
             helpPluginArtifact = getArtifact( artifactString );
 762  
         }
 763  
 
 764  0
         if ( helpPluginArtifact == null )
 765  
         {
 766  0
             throw new MojoExecutionException( "The help plugin artifact was not found." );
 767  
         }
 768  
 
 769  0
         return getMavenProject( helpPluginArtifact );
 770  
     }
 771  
 
 772  
     /**
 773  
      * @param name not null
 774  
      * @return the plural of the name
 775  
      */
 776  
     private static String pluralize( String name )
 777  
     {
 778  0
         if ( StringUtils.isEmpty( name ) )
 779  
         {
 780  0
             throw new IllegalArgumentException( "name is required" );
 781  
         }
 782  
 
 783  0
         if ( name.endsWith( "y" ) )
 784  
         {
 785  0
             return name.substring( 0, name.length() - 1 ) + "ies";
 786  
         }
 787  0
         else if ( name.endsWith( "s" ) )
 788  
         {
 789  0
             return name;
 790  
         }
 791  
         else
 792  
         {
 793  0
             return name + "s";
 794  
         }
 795  
     }
 796  
 }