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