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