Coverage Report - org.apache.maven.plugin.changes.AbstractChangesReport
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractChangesReport
64%
31/48
50%
3/6
3.667
 
 1  
 package org.apache.maven.plugin.changes;
 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.commons.io.IOUtils;
 23  
 import org.apache.maven.artifact.Artifact;
 24  
 import org.apache.maven.artifact.factory.ArtifactFactory;
 25  
 import org.apache.maven.artifact.repository.ArtifactRepository;
 26  
 import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
 27  
 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
 28  
 import org.apache.maven.artifact.resolver.ArtifactResolver;
 29  
 import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
 30  
 import org.apache.maven.artifact.versioning.VersionRange;
 31  
 import org.apache.maven.doxia.sink.render.RenderingContext;
 32  
 import org.apache.maven.doxia.site.decoration.Body;
 33  
 import org.apache.maven.doxia.site.decoration.DecorationModel;
 34  
 import org.apache.maven.doxia.site.decoration.Skin;
 35  
 import org.apache.maven.doxia.siterenderer.Renderer;
 36  
 import org.apache.maven.doxia.siterenderer.RendererException;
 37  
 import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
 38  
 import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink;
 39  
 import org.apache.maven.plugin.MojoExecutionException;
 40  
 import org.apache.maven.project.MavenProject;
 41  
 import org.apache.maven.reporting.AbstractMavenReport;
 42  
 import org.apache.maven.reporting.MavenReportException;
 43  
 import org.codehaus.plexus.i18n.I18N;
 44  
 import org.codehaus.plexus.util.ReaderFactory;
 45  
 
 46  
 import java.io.File;
 47  
 import java.io.FileOutputStream;
 48  
 import java.io.OutputStreamWriter;
 49  
 import java.io.IOException;
 50  
 import java.io.Writer;
 51  
 import java.util.HashMap;
 52  
 import java.util.Locale;
 53  
 import java.util.Map;
 54  
 
 55  
 /**
 56  
  * Base class with the things that should be in AbstractMavenReport anyway.
 57  
  *
 58  
  * Note: This file was copied from r415312 of AbstractProjectInfoReport in
 59  
  * maven-project-info-reports, as a work-around to MCHANGES-88.
 60  
  *
 61  
  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
 62  
  *
 63  
  */
 64  1
 public abstract class AbstractChangesReport
 65  
     extends AbstractMavenReport
 66  
 {
 67  
     /**
 68  
      * Report output directory. Note that this parameter is only relevant if the goal is run from the command line or
 69  
      * from the default build lifecycle. If the goal is run indirectly as part of a site generation, the output
 70  
      * directory configured in the Maven Site Plugin is used instead.
 71  
      * 
 72  
      * @parameter default-value="${project.reporting.outputDirectory}"
 73  
      */
 74  
     private File outputDirectory;
 75  
 
 76  
     /**
 77  
      * Report output encoding. Note that this parameter is only relevant if the goal is run from the command line or
 78  
      * from the default build lifecycle. If the goal is run indirectly as part of a site generation, the output
 79  
      * encoding configured in the Maven Site Plugin is used instead.
 80  
      *
 81  
      * @parameter expression="${outputEncoding}" default-value="${project.reporting.outputEncoding}"
 82  
      * @since 2.4
 83  
      */
 84  
     private String outputEncoding;
 85  
 
 86  
     /**
 87  
      * Doxia Site Renderer.
 88  
      *
 89  
      * @component
 90  
      */
 91  
     protected Renderer siteRenderer;
 92  
 
 93  
     /**
 94  
      * The Maven Project.
 95  
      *
 96  
      * @parameter expression="${project}"
 97  
      * @required
 98  
      * @readonly
 99  
      */
 100  
     protected MavenProject project;
 101  
 
 102  
     /**
 103  
      * Local Repository.
 104  
      *
 105  
      * @parameter expression="${localRepository}"
 106  
      * @required
 107  
      * @readonly
 108  
      */
 109  
     protected ArtifactRepository localRepository;
 110  
 
 111  
     /**
 112  
      * @component
 113  
      */
 114  
     protected ArtifactResolver resolver;
 115  
 
 116  
     /**
 117  
      * @component
 118  
      */
 119  
     protected ArtifactFactory factory;
 120  
 
 121  
     /**
 122  
      * Internationalization.
 123  
      *
 124  
      * @component
 125  
      */
 126  
     protected I18N i18n;
 127  
 
 128  
     private File getSkinArtifactFile()
 129  
         throws MojoExecutionException
 130  
     {
 131  1
         Skin skin = Skin.getDefaultSkin();
 132  
 
 133  1
         String version = skin.getVersion();
 134  
         Artifact artifact;
 135  
         try
 136  
         {
 137  1
             if ( version == null )
 138  
             {
 139  0
                 version = Artifact.RELEASE_VERSION;
 140  
             }
 141  1
             VersionRange versionSpec = VersionRange.createFromVersionSpec( version );
 142  1
             artifact = factory.createDependencyArtifact( skin.getGroupId(), skin.getArtifactId(), versionSpec, "jar",
 143  
                                                          null, null );
 144  
 
 145  1
             resolver.resolve( artifact, project.getRemoteArtifactRepositories(), localRepository );
 146  
         }
 147  0
         catch ( InvalidVersionSpecificationException e )
 148  
         {
 149  0
             throw new MojoExecutionException( "The skin version '" + version + "' is not valid: " + e.getMessage() );
 150  
         }
 151  0
         catch ( ArtifactResolutionException e )
 152  
         {
 153  0
             throw new MojoExecutionException( "Unable to find skin", e );
 154  
         }
 155  0
         catch ( ArtifactNotFoundException e )
 156  
         {
 157  0
             throw new MojoExecutionException( "The skin does not exist: " + e.getMessage() );
 158  1
         }
 159  
 
 160  1
         return artifact.getFile();
 161  
     }
 162  
 
 163  
     public void execute()
 164  
         throws MojoExecutionException
 165  
     {
 166  1
         if ( !canGenerateReport() )
 167  
         {
 168  0
             return;
 169  
         }
 170  
 
 171  
         // TODO: push to a helper? Could still be improved by taking more of the site information from the site plugin
 172  1
         FileOutputStream fileOutputStream = null;
 173  
         try
 174  
         {
 175  1
             DecorationModel model = new DecorationModel();
 176  1
             model.setBody( new Body() );
 177  1
             Map<String, String> attributes = new HashMap<String, String>();
 178  1
             attributes.put( "outputEncoding", getOutputEncoding() );
 179  1
             Locale locale = Locale.getDefault();
 180  1
             SiteRenderingContext siteContext = siteRenderer.createContextForSkin( getSkinArtifactFile(), attributes,
 181  
                                                                                   model, getName( locale ), locale );
 182  1
             siteContext.setOutputEncoding( getOutputEncoding() );
 183  
 
 184  1
             RenderingContext context = new RenderingContext( outputDirectory, getOutputName() + ".html" );
 185  
 
 186  1
             SiteRendererSink sink = new SiteRendererSink( context );
 187  1
             generate( sink, locale );
 188  
 
 189  1
             outputDirectory.mkdirs();
 190  
 
 191  1
             File file = new File( outputDirectory, getOutputName() + ".html" );
 192  1
             fileOutputStream = new FileOutputStream( file ) ;
 193  1
             Writer writer = new OutputStreamWriter( fileOutputStream, getOutputEncoding() );
 194  
 
 195  1
             siteRenderer.generateDocument( writer, sink, siteContext );
 196  
 
 197  1
             siteRenderer.copyResources( siteContext, new File( project.getBasedir(), "src/site/resources" ),
 198  
                                         outputDirectory );
 199  
         }
 200  0
         catch ( RendererException e )
 201  
         {
 202  0
             throw new MojoExecutionException(
 203  
                 "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e );
 204  
         }
 205  0
         catch ( IOException e )
 206  
         {
 207  0
             throw new MojoExecutionException(
 208  
                 "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e );
 209  
         }
 210  0
         catch ( MavenReportException e )
 211  
         {
 212  0
             throw new MojoExecutionException(
 213  
                 "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e );
 214  
         }
 215  
         finally {
 216  1
             IOUtils.closeQuietly( fileOutputStream );
 217  1
         }
 218  1
     }
 219  
 
 220  
     /**
 221  
      * @see org.apache.maven.reporting.AbstractMavenReport#getOutputDirectory()
 222  
      */
 223  
     protected String getOutputDirectory()
 224  
     {
 225  0
         return outputDirectory.getAbsolutePath();
 226  
     }
 227  
 
 228  
     /**
 229  
      * Get the effective reporting output file encoding.
 230  
      *
 231  
      * @return The effective reporting output file encoding, never <code>null</code>.
 232  
      * @since 2.4
 233  
      */
 234  
     protected String getOutputEncoding()
 235  
     {
 236  3
         return ( outputEncoding != null ) ? outputEncoding : ReaderFactory.UTF_8;
 237  
     }
 238  
 
 239  
     /**
 240  
      * @see org.apache.maven.reporting.AbstractMavenReport#getProject()
 241  
      */
 242  
     protected MavenProject getProject()
 243  
     {
 244  0
         return project;
 245  
     }
 246  
 
 247  
     /**
 248  
      * @see org.apache.maven.reporting.AbstractMavenReport#getSiteRenderer()
 249  
      */
 250  
     protected Renderer getSiteRenderer()
 251  
     {
 252  0
         return siteRenderer;
 253  
     }
 254  
 }