View Javadoc

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  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         Skin skin = Skin.getDefaultSkin();
125 
126         String version = skin.getVersion();
127         Artifact artifact;
128         try
129         {
130             if ( version == null )
131             {
132                 version = Artifact.RELEASE_VERSION;
133             }
134             VersionRange versionSpec = VersionRange.createFromVersionSpec( version );
135             artifact = factory.createDependencyArtifact( skin.getGroupId(), skin.getArtifactId(), versionSpec, "jar",
136                                                          null, null );
137 
138             resolver.resolve( artifact, project.getRemoteArtifactRepositories(), localRepository );
139         }
140         catch ( InvalidVersionSpecificationException e )
141         {
142             throw new MojoExecutionException( "The skin version '" + version + "' is not valid: " + e.getMessage() );
143         }
144         catch ( ArtifactResolutionException e )
145         {
146             throw new MojoExecutionException( "Unable to find skin", e );
147         }
148         catch ( ArtifactNotFoundException e )
149         {
150             throw new MojoExecutionException( "The skin does not exist: " + e.getMessage() );
151         }
152 
153         return artifact.getFile();
154     }
155 
156     public void execute()
157         throws MojoExecutionException
158     {
159         if ( !canGenerateReport() )
160         {
161             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         FileOutputStream fileOutputStream = null;
166         try
167         {
168             DecorationModel model = new DecorationModel();
169             model.setBody( new Body() );
170             Map<String, String> attributes = new HashMap<String, String>();
171             attributes.put( "outputEncoding", getOutputEncoding() );
172             Locale locale = Locale.getDefault();
173             SiteRenderingContext siteContext = siteRenderer.createContextForSkin( getSkinArtifactFile(), attributes,
174                                                                                   model, getName( locale ), locale );
175             siteContext.setOutputEncoding( getOutputEncoding() );
176 
177             RenderingContext context = new RenderingContext( outputDirectory, getOutputName() + ".html" );
178 
179             SiteRendererSink sink = new SiteRendererSink( context );
180             generate( sink, locale );
181 
182             outputDirectory.mkdirs();
183 
184             File file = new File( outputDirectory, getOutputName() + ".html" );
185             fileOutputStream = new FileOutputStream( file ) ;
186             Writer writer = new OutputStreamWriter( fileOutputStream, getOutputEncoding() );
187 
188             siteRenderer.generateDocument( writer, sink, siteContext );
189 
190             siteRenderer.copyResources( siteContext, new File( project.getBasedir(), "src/site/resources" ),
191                                         outputDirectory );
192         }
193         catch ( RendererException e )
194         {
195             throw new MojoExecutionException(
196                 "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e );
197         }
198         catch ( IOException e )
199         {
200             throw new MojoExecutionException(
201                 "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e );
202         }
203         catch ( MavenReportException e )
204         {
205             throw new MojoExecutionException(
206                 "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e );
207         }
208         finally {
209             IOUtils.closeQuietly( fileOutputStream );
210         }
211     }
212 
213     /**
214      * @see org.apache.maven.reporting.AbstractMavenReport#getOutputDirectory()
215      */
216     protected String getOutputDirectory()
217     {
218         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         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         return project;
238     }
239 
240     /**
241      * @see org.apache.maven.reporting.AbstractMavenReport#getSiteRenderer()
242      */
243     protected Renderer getSiteRenderer()
244     {
245         return siteRenderer;
246     }
247 }