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