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