Coverage Report - org.apache.maven.plugin.plugin.AbstractGeneratorMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractGeneratorMojo
0%
0/40
0%
0/18
7.333
 
 1  
 package org.apache.maven.plugin.plugin;
 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.AbstractMojo;
 23  
 import org.apache.maven.plugin.MojoExecutionException;
 24  
 import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException;
 25  
 import org.apache.maven.plugin.descriptor.PluginDescriptor;
 26  
 import org.apache.maven.project.MavenProject;
 27  
 import org.apache.maven.tools.plugin.DefaultPluginToolsRequest;
 28  
 import org.apache.maven.tools.plugin.PluginToolsRequest;
 29  
 import org.apache.maven.tools.plugin.extractor.ExtractionException;
 30  
 import org.apache.maven.tools.plugin.generator.Generator;
 31  
 import org.apache.maven.tools.plugin.scanner.MojoScanner;
 32  
 import org.apache.maven.tools.plugin.util.PluginUtils;
 33  
 import org.codehaus.plexus.util.ReaderFactory;
 34  
 
 35  
 import java.io.File;
 36  
 import java.io.IOException;
 37  
 import java.util.Set;
 38  
 
 39  
 /**
 40  
  * Abstract class for this Plugin.
 41  
  *
 42  
  * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
 43  
  * @version $Id: AbstractGeneratorMojo.java 1133737 2011-06-09 09:20:50Z stephenc $
 44  
  * 
 45  
  * @threadSafe
 46  
  */
 47  0
 public abstract class AbstractGeneratorMojo
 48  
     extends AbstractMojo
 49  
 {
 50  
     /**
 51  
      * The project currently being built.
 52  
      *
 53  
      * @parameter default-value="${project}"
 54  
      * @required
 55  
      * @readonly
 56  
      */
 57  
     protected MavenProject project;
 58  
 
 59  
     /**
 60  
      * The component used for scanning the source tree for mojos.
 61  
      *
 62  
      * @component
 63  
      * @required
 64  
      */
 65  
     protected MojoScanner mojoScanner;
 66  
 
 67  
     /**
 68  
      * The file encoding of the source files.
 69  
      * 
 70  
      * @parameter expression="${encoding}" default-value="${project.build.sourceEncoding}"
 71  
      * @since 2.5
 72  
      */
 73  
     protected String encoding;
 74  
 
 75  
     /**
 76  
      * The goal prefix that will appear before the ":".
 77  
      *
 78  
      * @parameter
 79  
      */
 80  
     protected String goalPrefix;
 81  
 
 82  
     /**
 83  
      * The role names of mojo extractors to use.
 84  
      * <p/>
 85  
      * If not set, all mojo extractors will be used. If set to an empty extractor name, no mojo extractors
 86  
      * will be used.
 87  
      * <p/>
 88  
      * Example:
 89  
      * <p/>
 90  
      * <pre>
 91  
      *  &lt;!-- Use all mojo extractors --&gt;
 92  
      *  &lt;extractors/&gt;
 93  
      *  
 94  
      *  &lt;!-- Use no mojo extractors --&gt;
 95  
      *  &lt;extractors&gt;
 96  
      *      &lt;extractor/&gt;
 97  
      *  &lt;/extractors&gt;
 98  
      *  
 99  
      *  &lt;!-- Use only bsh mojo extractor --&gt;
 100  
      *  &lt;extractors&gt;
 101  
      *      &lt;extractor&gt;bsh&lt;/extractor&gt;
 102  
      *  &lt;/extractors&gt;
 103  
      * </pre>
 104  
      *
 105  
      * @parameter
 106  
      */
 107  
     protected Set<String> extractors;
 108  
 
 109  
     /**
 110  
      * Set this to "true" to skip invoking any goals or reports of the plugin.
 111  
      *
 112  
      * @parameter default-value="false" expression="${maven.plugin.skip}"
 113  
      * @since 2.8
 114  
      */
 115  
     protected boolean skip;
 116  
 
 117  
     /**
 118  
      * @return the output directory where files will be generated.
 119  
      */
 120  
     protected abstract File getOutputDirectory();
 121  
 
 122  
     /**
 123  
      * @return the wanted <code>Generator</code> implementation.
 124  
      */
 125  
     protected abstract Generator createGenerator();
 126  
 
 127  
     /** {@inheritDoc} */
 128  
     public void execute()
 129  
         throws MojoExecutionException
 130  
     {
 131  0
         if ( !"maven-plugin".equals( project.getPackaging() ) )
 132  
         {
 133  0
             return;
 134  
         }
 135  0
         if ( skip )
 136  
         {
 137  0
             getLog().warn( "Execution skipped" );
 138  0
             return;
 139  
         }
 140  
 
 141  0
         if ( project.getArtifactId().toLowerCase().startsWith( "maven-" ) 
 142  
             && project.getArtifactId().toLowerCase().endsWith( "-plugin" )
 143  
             && !"org.apache.maven.plugins".equals( project.getGroupId() ) )
 144  
         {
 145  0
             getLog().error( "\n\nArtifact Ids of the format maven-___-plugin are reserved for \n" 
 146  
                                 + "plugins in the Group Id org.apache.maven.plugins\n"
 147  
                                 + "Please change your artifactId to the format ___-maven-plugin\n"
 148  
                                 + "In the future this error will break the build.\n\n" );
 149  
         }
 150  
 
 151  0
         String defaultGoalPrefix = PluginDescriptor.getGoalPrefixFromArtifactId( project.getArtifactId() );
 152  0
         if ( goalPrefix == null )
 153  
         {
 154  0
             goalPrefix = defaultGoalPrefix;
 155  
         }
 156  0
         else if ( !goalPrefix.equals( defaultGoalPrefix ) )
 157  
         {
 158  0
             getLog().warn(
 159  
                            "\n\nGoal prefix is specified as: '" + goalPrefix + "'. "
 160  
                                + "Maven currently expects it to be '" + defaultGoalPrefix + "'.\n" );
 161  
         }
 162  
 
 163  0
         mojoScanner.setActiveExtractors( extractors );
 164  
 
 165  
         // TODO: could use this more, eg in the writing of the plugin descriptor!
 166  0
         PluginDescriptor pluginDescriptor = new PluginDescriptor();
 167  
 
 168  0
         pluginDescriptor.setGroupId( project.getGroupId() );
 169  
 
 170  0
         pluginDescriptor.setArtifactId( project.getArtifactId() );
 171  
 
 172  0
         pluginDescriptor.setVersion( project.getVersion() );
 173  
 
 174  0
         pluginDescriptor.setGoalPrefix( goalPrefix );
 175  
 
 176  0
         pluginDescriptor.setName( project.getName() );
 177  
 
 178  0
         pluginDescriptor.setDescription( project.getDescription() );
 179  
 
 180  0
         if ( encoding == null || encoding.length() < 1 )
 181  
         {
 182  0
             getLog().warn( "Using platform encoding (" + ReaderFactory.FILE_ENCODING
 183  
                                   + " actually) to read mojo metadata, i.e. build is platform dependent!" );
 184  
         }
 185  
         else
 186  
         {
 187  0
             getLog().info( "Using '" + encoding + "' encoding to read mojo metadata." );
 188  
         }
 189  
         
 190  
         try
 191  
         {
 192  0
             pluginDescriptor.setDependencies( PluginUtils.toComponentDependencies( project.getRuntimeDependencies() ) );
 193  
             
 194  0
             PluginToolsRequest request = new DefaultPluginToolsRequest( project, pluginDescriptor );
 195  0
             request.setEncoding( encoding );
 196  
 
 197  0
             mojoScanner.populatePluginDescriptor( request );
 198  
 
 199  0
             getOutputDirectory().mkdirs();
 200  
 
 201  0
             createGenerator().execute( getOutputDirectory(), request );
 202  
         }
 203  0
         catch ( IOException e )
 204  
         {
 205  0
             throw new MojoExecutionException( "Error writing plugin descriptor", e );
 206  
         }
 207  0
         catch ( InvalidPluginDescriptorException e )
 208  
         {
 209  0
             throw new MojoExecutionException( "Error extracting plugin descriptor: \'" + e.getLocalizedMessage() + "\'",
 210  
                                               e );
 211  
         }
 212  0
         catch ( ExtractionException e )
 213  
         {
 214  0
             throw new MojoExecutionException( "Error extracting plugin descriptor: \'" + e.getLocalizedMessage() + "\'",
 215  
                                               e );
 216  
         }
 217  0
         catch ( LinkageError e )
 218  
         {
 219  0
             throw new MojoExecutionException( "The API of the mojo scanner is not compatible with this plugin version."
 220  
                 + " Please check the plugin dependencies configured in the POM and ensure the versions match.", e );
 221  0
         }
 222  0
     }
 223  
 
 224  
 }