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