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         FileOutputStream fileOutputStream = 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             fileOutputStream = new FileOutputStream( file );
209             Writer writer = new OutputStreamWriter( fileOutputStream, getOutputEncoding() );
210 
211             siteRenderer.generateDocument( writer, sink, siteContext );
212 
213             siteRenderer.copyResources( siteContext, new File( project.getBasedir(), "src/site/resources" ),
214                                         outputDirectory );
215         }
216         catch ( RendererException e )
217         {
218             throw new MojoExecutionException( "An error has occurred in " + getName( Locale.ENGLISH )
219                 + " report generation.", e );
220         }
221         catch ( IOException e )
222         {
223             throw new MojoExecutionException( "An error has occurred in " + getName( Locale.ENGLISH )
224                 + " report generation.", e );
225         }
226         catch ( MavenReportException e )
227         {
228             throw new MojoExecutionException( "An error has occurred in " + getName( Locale.ENGLISH )
229                 + " report generation.", e );
230         }
231         finally
232         {
233             IOUtils.closeQuietly( fileOutputStream );
234         }
235     }
236 
237     /**
238      * @see org.apache.maven.reporting.AbstractMavenReport#getOutputDirectory()
239      */
240     protected String getOutputDirectory()
241     {
242         return outputDirectory.getAbsolutePath();
243     }
244 
245     /**
246      * Get the effective reporting output file encoding.
247      *
248      * @return The effective reporting output file encoding, never <code>null</code>.
249      * @since 2.4
250      */
251     protected String getOutputEncoding()
252     {
253         return ( outputEncoding != null ) ? outputEncoding : ReaderFactory.UTF_8;
254     }
255 
256     /**
257      * @see org.apache.maven.reporting.AbstractMavenReport#getProject()
258      */
259     protected MavenProject getProject()
260     {
261         return project;
262     }
263 
264     /**
265      * @see org.apache.maven.reporting.AbstractMavenReport#getSiteRenderer()
266      */
267     protected Renderer getSiteRenderer()
268     {
269         return siteRenderer;
270     }
271 
272     /**
273      * Returns <code>true</code> if the current project is located at the Execution Root Directory (where mvn was
274      * launched).
275      *
276      * @return <code>true</code> if the current project is at the Execution Root
277      */
278     protected boolean isThisTheExecutionRoot()
279     {
280         getLog().debug( "Root Folder:" + mavenSession.getExecutionRootDirectory() );
281         getLog().debug( "Current Folder:" + basedir );
282         boolean result = mavenSession.getExecutionRootDirectory().equalsIgnoreCase( basedir );
283         if ( result )
284         {
285             getLog().debug( "This is the execution root." );
286         }
287         else
288         {
289             getLog().debug( "This is NOT the execution root." );
290         }
291         return result;
292     }
293 }