Coverage Report - org.apache.maven.tools.plugin.generator.PluginHelpGenerator
 
Classes in this File Line Coverage Branch Coverage Complexity
PluginHelpGenerator
78 %
47/60
40 %
9/22
3,286
 
 1  
 package org.apache.maven.tools.plugin.generator;
 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.MojoDescriptor;
 23  
 import org.apache.maven.plugin.descriptor.PluginDescriptor;
 24  
 import org.apache.maven.project.MavenProject;
 25  
 import org.apache.maven.tools.plugin.PluginToolsRequest;
 26  
 import org.apache.velocity.VelocityContext;
 27  
 import org.codehaus.plexus.logging.AbstractLogEnabled;
 28  
 import org.codehaus.plexus.logging.Logger;
 29  
 import org.codehaus.plexus.logging.console.ConsoleLogger;
 30  
 import org.codehaus.plexus.util.IOUtil;
 31  
 import org.codehaus.plexus.util.StringUtils;
 32  
 import org.codehaus.plexus.velocity.VelocityComponent;
 33  
 
 34  
 import java.io.File;
 35  
 import java.io.FileOutputStream;
 36  
 import java.io.IOException;
 37  
 import java.io.InputStream;
 38  
 import java.io.InputStreamReader;
 39  
 import java.io.OutputStreamWriter;
 40  
 import java.io.StringWriter;
 41  
 import java.io.Writer;
 42  
 import java.util.List;
 43  
 import java.util.Properties;
 44  
 
 45  
 /**
 46  
  * Generates an <code>HelpMojo</code> class.
 47  
  *
 48  
  * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
 49  
  * @version $Id: PluginHelpGenerator.java 1342957 2012-05-26 20:29:09Z hboutemy $
 50  
  * @since 2.4
 51  
  */
 52  
 public class PluginHelpGenerator
 53  
     extends AbstractLogEnabled
 54  
     implements Generator
 55  
 {
 56  
     /**
 57  
      * Default generated class name
 58  
      */
 59  
     private static final String HELP_MOJO_CLASS_NAME = "HelpMojo";
 60  
 
 61  
     /**
 62  
      * Default goal
 63  
      */
 64  
     private static final String HELP_GOAL = "help";
 65  
 
 66  
     private String helpPackageName;
 67  
 
 68  
     private VelocityComponent velocityComponent;
 69  
 
 70  
     /**
 71  
      * Default constructor
 72  
      */
 73  
     public PluginHelpGenerator()
 74  1
     {
 75  1
         this.enableLogging( new ConsoleLogger( Logger.LEVEL_INFO, "PluginHelpGenerator" ) );
 76  1
     }
 77  
 
 78  
     // ----------------------------------------------------------------------
 79  
     // Public methods
 80  
     // ----------------------------------------------------------------------
 81  
 
 82  
 
 83  
     /**
 84  
      * {@inheritDoc}
 85  
      */
 86  
     public void execute( File destinationDirectory, PluginToolsRequest request )
 87  
         throws GeneratorException
 88  
     {
 89  1
         PluginDescriptor pluginDescriptor = request.getPluginDescriptor();
 90  
 
 91  1
         String helpImplementation = getImplementation( pluginDescriptor );
 92  
 
 93  
         @SuppressWarnings( "unchecked" )
 94  1
         List<MojoDescriptor> mojoDescriptors = pluginDescriptor.getMojos();
 95  
 
 96  1
         if ( mojoDescriptors != null )
 97  
         {
 98  
             // Verify that no help goal already exists
 99  1
             for ( MojoDescriptor descriptor : mojoDescriptors )
 100  
             {
 101  1
                 if ( HELP_GOAL.equals( descriptor.getGoal() )
 102  
                     && !descriptor.getImplementation().equals( helpImplementation ) )
 103  
                 {
 104  0
                     if ( getLogger().isWarnEnabled() )
 105  
                     {
 106  0
                         getLogger().warn( "\n\nA help goal (" + descriptor.getImplementation()
 107  
                                               + ") already exists in this plugin. SKIPPED THE "
 108  
                                               + helpImplementation + " GENERATION.\n" );
 109  
                     }
 110  
 
 111  0
                     return;
 112  
                 }
 113  
             }
 114  
         }
 115  1
         Properties properties = new Properties();
 116  1
         properties.put( "helpPackageName", helpPackageName == null ? "" : helpPackageName );
 117  
 
 118  1
         MavenProject mavenProject = request.getProject();
 119  
 
 120  1
         String propertiesFilePath = "META-INF/maven/" + mavenProject.getGroupId() + "/" + mavenProject.getArtifactId();
 121  
 
 122  1
         File tmpPropertiesFile =
 123  
             new File( request.getProject().getBuild().getDirectory(), "maven-plugin-help.properties" );
 124  1
         if ( tmpPropertiesFile.exists() )
 125  
         {
 126  1
             tmpPropertiesFile.delete();
 127  
         }
 128  
         else
 129  
         {
 130  0
             if ( !tmpPropertiesFile.getParentFile().exists() )
 131  
             {
 132  0
                 tmpPropertiesFile.getParentFile().mkdirs();
 133  
             }
 134  
         }
 135  1
         FileOutputStream fos = null;
 136  
         try
 137  
         {
 138  1
             fos = new FileOutputStream( tmpPropertiesFile );
 139  1
             properties.store( fos, "maven plugin help generation informations" );
 140  
         }
 141  0
         catch ( IOException e )
 142  
         {
 143  0
             throw new GeneratorException( e.getMessage(), e );
 144  
         }
 145  
         finally
 146  
         {
 147  1
             IOUtil.close( fos );
 148  1
         }
 149  
 
 150  1
         String sourcePath = helpImplementation.replace( '.', File.separatorChar ) + ".java";
 151  1
         File helpClass = new File( destinationDirectory, sourcePath );
 152  1
         helpClass.getParentFile().mkdirs();
 153  
 
 154  1
         Writer writer = null;
 155  
         try
 156  
         {
 157  1
             writer = new OutputStreamWriter( new FileOutputStream( helpClass ), request.getEncoding() );
 158  1
             writer.write( getHelpClassSources( propertiesFilePath ) );
 159  1
             writer.flush();
 160  
         }
 161  0
         catch ( IOException e )
 162  
         {
 163  0
             throw new GeneratorException( e.getMessage(), e );
 164  
         }
 165  
         finally
 166  
         {
 167  1
             IOUtil.close( writer );
 168  1
         }
 169  1
     }
 170  
 
 171  
     public PluginHelpGenerator setHelpPackageName( String helpPackageName )
 172  
     {
 173  0
         this.helpPackageName = helpPackageName;
 174  0
         return this;
 175  
     }
 176  
 
 177  
     public VelocityComponent getVelocityComponent()
 178  
     {
 179  0
         return velocityComponent;
 180  
     }
 181  
 
 182  
     public PluginHelpGenerator setVelocityComponent( VelocityComponent velocityComponent )
 183  
     {
 184  1
         this.velocityComponent = velocityComponent;
 185  1
         return this;
 186  
     }
 187  
 
 188  
     // ----------------------------------------------------------------------
 189  
     // Private methods
 190  
     // ----------------------------------------------------------------------
 191  
 
 192  
     protected String getHelpClassSources( String propertiesFilePath )
 193  
     {
 194  1
         Properties properties = new Properties();
 195  1
         VelocityContext context = new VelocityContext( properties );
 196  1
         if ( this.helpPackageName != null )
 197  
         {
 198  0
             properties.put( "helpPackageName", this.helpPackageName );
 199  
         }
 200  
         else
 201  
         {
 202  1
             properties.put( "helpPackageName", "" );
 203  
         }
 204  1
         properties.put( "pluginHelpPath", propertiesFilePath + "/plugin-help.xml" );
 205  
         // FIXME encoding !
 206  
 
 207  1
         StringWriter stringWriter = new StringWriter();
 208  
 
 209  1
         InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream( "help-class-source.vm" );
 210  1
         InputStreamReader isReader = new InputStreamReader( is );
 211  1
         velocityComponent.getEngine().evaluate( context, stringWriter, "", isReader );
 212  
 
 213  1
         return stringWriter.toString();
 214  
     }
 215  
 
 216  
 
 217  
     /**
 218  
      * @param pluginDescriptor The descriptor of the plugin for which to generate a help goal, must not be
 219  
      *                         <code>null</code>.
 220  
      * @return The implementation.
 221  
      */
 222  
     private String getImplementation( PluginDescriptor pluginDescriptor )
 223  
     {
 224  1
         String packageName = helpPackageName;
 225  1
         if ( StringUtils.isEmpty( packageName ) )
 226  
         {
 227  1
             packageName = GeneratorUtils.discoverPackageName( pluginDescriptor );
 228  
         }
 229  
 
 230  1
         return StringUtils.isEmpty( packageName ) ? HELP_MOJO_CLASS_NAME : packageName + '.' + HELP_MOJO_CLASS_NAME;
 231  
     }
 232  
 }