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