View Javadoc

1   package org.apache.maven.report.projectinfo;
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.ArtifactResolver;
26  import org.apache.maven.doxia.module.xhtml.decoration.render.RenderingContext;
27  import org.apache.maven.doxia.site.decoration.Body;
28  import org.apache.maven.doxia.site.decoration.DecorationModel;
29  import org.apache.maven.doxia.siterenderer.Renderer;
30  import org.apache.maven.doxia.siterenderer.RendererException;
31  import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
32  import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink;
33  import org.apache.maven.doxia.tools.SiteTool;
34  import org.apache.maven.doxia.tools.SiteToolException;
35  import org.apache.maven.plugin.MojoExecutionException;
36  import org.apache.maven.project.MavenProject;
37  import org.apache.maven.reporting.AbstractMavenReport;
38  import org.apache.maven.reporting.MavenReportException;
39  import org.codehaus.plexus.i18n.I18N;
40  
41  import java.io.File;
42  import java.io.FileOutputStream;
43  import java.io.IOException;
44  import java.io.OutputStreamWriter;
45  import java.io.Writer;
46  import java.util.HashMap;
47  import java.util.Locale;
48  import java.util.Map;
49  
50  /**
51   * Base class with the things that should be in AbstractMavenReport anyway.
52   *
53   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
54   * @version $Id: AbstractProjectInfoReport.java 728546 2008-12-21 22:56:51Z bentmann $
55   * @since 2.0
56   */
57  public abstract class AbstractProjectInfoReport
58      extends AbstractMavenReport
59  {
60      // ----------------------------------------------------------------------
61      // Mojo components
62      // ----------------------------------------------------------------------
63  
64      /**
65       * SiteTool component.
66       *
67       * @since 2.1
68       * @component
69       */
70      protected SiteTool siteTool;
71  
72      /**
73       * Doxia Site Renderer component.
74       *
75       * @component
76       */
77      protected Renderer siteRenderer;
78  
79      /**
80       * Artifact Resolver component.
81       *
82       * @component
83       */
84      protected ArtifactResolver resolver;
85  
86      /**
87       * Artifact Factory component.
88       *
89       * @component
90       */
91      protected ArtifactFactory factory;
92  
93      /**
94       * Internationalization component.
95       *
96       * @component
97       */
98      protected I18N i18n;
99  
100     // ----------------------------------------------------------------------
101     // Mojo parameters
102     // ----------------------------------------------------------------------
103 
104     /**
105      * The output directory for the report. Note that this parameter is only evaluated if the goal is run directly from
106      * the command line. If the goal is run indirectly as part of a site generation, the output directory configured in
107      * the Maven Site Plugin is used instead.
108      *
109      * @parameter expression="${project.reporting.outputDirectory}"
110      * @required
111      */
112     protected File outputDirectory;
113 
114     /**
115      * The Maven Project.
116      *
117      * @parameter expression="${project}"
118      * @required
119      * @readonly
120      */
121     protected MavenProject project;
122 
123     /**
124      * Local Repository.
125      *
126      * @parameter expression="${localRepository}"
127      * @required
128      * @readonly
129      */
130     protected ArtifactRepository localRepository;
131 
132     // ----------------------------------------------------------------------
133     // Public methods
134     // ----------------------------------------------------------------------
135 
136     /** {@inheritDoc} */
137     public void execute()
138         throws MojoExecutionException
139     {
140         if ( !canGenerateReport() )
141         {
142             return;
143         }
144 
145         // TODO: push to a helper? Could still be improved by taking more of the site information from the site plugin
146         try
147         {
148             DecorationModel model = new DecorationModel();
149             model.setBody( new Body() );
150             Map attributes = new HashMap();
151             attributes.put( "outputEncoding", "UTF-8" );
152             attributes.put( "project", project );
153             Locale locale = Locale.getDefault();
154             Artifact defaultSkin =
155                 siteTool.getDefaultSkinArtifact( localRepository, project.getRemoteArtifactRepositories() );
156             SiteRenderingContext siteContext = siteRenderer.createContextForSkin( defaultSkin.getFile(), attributes,
157                                                                                   model, getName( locale ), locale );
158 
159             RenderingContext context = new RenderingContext( outputDirectory, getOutputName() + ".html" );
160 
161             SiteRendererSink sink = new SiteRendererSink( context );
162             generate( sink, locale );
163 
164             outputDirectory.mkdirs();
165 
166             Writer writer =
167                 new OutputStreamWriter( new FileOutputStream( new File( outputDirectory, getOutputName() + ".html" ) ),
168                                         "UTF-8" );
169 
170             siteRenderer.generateDocument( writer, sink, siteContext );
171 
172             siteRenderer.copyResources( siteContext, new File( project.getBasedir(), "src/site/resources" ),
173                                         outputDirectory );
174         }
175         catch ( RendererException e )
176         {
177             throw new MojoExecutionException(
178                 "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e );
179         }
180         catch ( IOException e )
181         {
182             throw new MojoExecutionException(
183                 "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e );
184         }
185         catch ( SiteToolException e )
186         {
187             throw new MojoExecutionException(
188                 "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e );
189         }
190         catch ( MavenReportException e )
191         {
192             throw new MojoExecutionException(
193                 "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e );
194         }
195     }
196 
197     /** {@inheritDoc} */
198     public String getCategoryName()
199     {
200         return CATEGORY_PROJECT_INFORMATION;
201     }
202 
203     // ----------------------------------------------------------------------
204     // Protected methods
205     // ----------------------------------------------------------------------
206 
207     /** {@inheritDoc} */
208     protected String getOutputDirectory()
209     {
210         return outputDirectory.getAbsolutePath();
211     }
212 
213     /** {@inheritDoc} */
214     public File getReportOutputDirectory()
215     {
216         return outputDirectory;
217     }
218 
219     /** {@inheritDoc} */
220     public void setReportOutputDirectory( File reportOutputDirectory )
221     {
222         this.outputDirectory = reportOutputDirectory;
223     }
224 
225     /** {@inheritDoc} */
226     protected MavenProject getProject()
227     {
228         return project;
229     }
230 
231     /** {@inheritDoc} */
232     protected Renderer getSiteRenderer()
233     {
234         return siteRenderer;
235     }
236 }