View Javadoc
1   package org.apache.maven.plugins.site.render;
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 java.io.File;
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.Collections;
26  import java.util.List;
27  import java.util.Locale;
28  import java.util.Map;
29  import java.util.TreeMap;
30  
31  import org.apache.maven.doxia.siterenderer.DocumentRenderer;
32  import org.apache.maven.doxia.siterenderer.DoxiaDocumentRenderer;
33  import org.apache.maven.doxia.siterenderer.RendererException;
34  import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
35  import org.apache.maven.execution.MavenSession;
36  import org.apache.maven.plugin.MojoExecutionException;
37  import org.apache.maven.plugin.MojoFailureException;
38  import org.apache.maven.plugins.annotations.Mojo;
39  import org.apache.maven.plugins.annotations.Parameter;
40  import org.apache.maven.plugins.annotations.ResolutionScope;
41  import org.apache.maven.project.MavenProject;
42  import org.apache.maven.reporting.MavenReport;
43  import org.apache.maven.reporting.MavenReportException;
44  import org.apache.maven.reporting.exec.MavenReportExecution;
45  import org.apache.maven.shared.utils.logging.MessageBuilder;
46  
47  import static org.apache.maven.shared.utils.logging.MessageUtils.buffer;
48  
49  /**
50   * Generates the site for a single project.
51   * <p>
52   * Note that links between module sites in a multi module build will <b>not</b> work, since local build directory
53   * structure doesn't match deployed site.
54   * </p>
55   *
56   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
57   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
58   *
59   */
60  @Mojo( name = "site", requiresDependencyResolution = ResolutionScope.TEST, requiresReports = true )
61  public class SiteMojo
62      extends AbstractSiteRenderingMojo
63  {
64      /**
65       * Directory where the project sites and report distributions will be generated (as html/css/...).
66       */
67      @Parameter( property = "siteOutputDirectory", defaultValue = "${project.reporting.outputDirectory}" )
68      protected File outputDirectory;
69  
70      /**
71       * Convenience parameter that allows you to disable report generation.
72       */
73      @Parameter( property = "generateReports", defaultValue = "true" )
74      private boolean generateReports;
75  
76      /**
77       * Generate a sitemap. The result will be a "sitemap.html" file at the site root.
78       *
79       * @since 2.1
80       */
81      @Parameter( property = "generateSitemap", defaultValue = "false" )
82      private boolean generateSitemap;
83  
84      /**
85       * Whether to validate xml input documents. If set to true, <strong>all</strong> input documents in xml format (in
86       * particular xdoc and fml) will be validated and any error will lead to a build failure.
87       *
88       * @since 2.1.1
89       */
90      @Parameter( property = "validate", defaultValue = "false" )
91      private boolean validate;
92  
93      /**
94       * {@inheritDoc}
95       */
96      public void execute()
97          throws MojoExecutionException, MojoFailureException
98      {
99          if ( skip )
100         {
101             getLog().info( "maven.site.skip = true: Skipping site generation" );
102             return;
103         }
104 
105         if ( getLog().isDebugEnabled() )
106         {
107             getLog().debug( "executing Site Mojo" );
108         }
109 
110         checkInputEncoding();
111 
112         List<MavenReportExecution> reports;
113         if ( generateReports )
114         {
115             reports = getReports();
116         }
117         else
118         {
119             reports = Collections.emptyList();
120         }
121 
122         try
123         {
124             List<Locale> localesList = getLocales();
125 
126             // Default is first in the list
127             Locale defaultLocale = localesList.get( 0 );
128             Locale.setDefault( defaultLocale );
129 
130             for ( Locale locale : localesList )
131             {
132                 if ( locale == defaultLocale )
133                 {
134                     getLog().info( buffer().strong( "Rendering site with default locale " + locale.getDisplayName()
135                         + " (" + locale + ")" ).toString() );
136                 }
137                 else
138                 {
139                     getLog().info( "" );
140                     getLog().info( buffer().strong( "Rendering localized site for " + locale.getDisplayName() + " ("
141                         + locale + ")" ).toString() );
142                 }
143                 renderLocale( locale, reports );
144             }
145         }
146         catch ( RendererException e )
147         {
148             if ( e.getCause() instanceof MavenReportException )
149             {
150                 // issue caused by report, not really by Doxia Site Renderer
151                 throw new MojoExecutionException( e.getMessage(), e.getCause() );
152             }
153             throw new MojoExecutionException( e.getMessage(), e );
154         }
155         catch ( IOException e )
156         {
157             throw new MojoExecutionException( "Error during site generation", e );
158         }
159     }
160 
161     private void renderLocale( Locale locale, List<MavenReportExecution> reports )
162         throws IOException, RendererException, MojoFailureException, MojoExecutionException
163     {
164         SiteRenderingContext context = createSiteRenderingContext( locale );
165         // MSITE-723 add generated site directory, in case some content has been put in pre-site phase
166         context.addSiteDirectory( generatedSiteDirectory );
167 
168         context.setInputEncoding( getInputEncoding() );
169         context.setOutputEncoding( getOutputEncoding() );
170         context.setValidate( validate );
171         if ( validate )
172         {
173             getLog().info( "Validation is switched on, xml input documents will be validated!" );
174         }
175 
176         File outputDir = getOutputDirectory( locale );
177 
178         Map<String, DocumentRenderer> documents = locateDocuments( context, reports, locale );
179 
180         // copy resources
181         siteRenderer.copyResources( context, outputDir );
182 
183         // 1. render Doxia documents first
184         List<DocumentRenderer> reportDocuments = renderDoxiaDocuments( documents, context, outputDir, false );
185 
186         // 2. then reports
187         // prepare external reports
188         for ( MavenReportExecution mavenReportExecution : reports )
189         {
190             MavenReport report = mavenReportExecution.getMavenReport();
191             report.setReportOutputDirectory( outputDir );
192         }
193 
194         siteRenderer.render( reportDocuments, context, outputDir );
195 
196         if ( generateSitemap )
197         {
198             getLog().info( "Generating Sitemap." );
199 
200             new SiteMap( getOutputEncoding(), i18n ).generate( context.getDecoration(), generatedSiteDirectory,
201                                                                locale );
202         }
203 
204         // 3. Generated docs must be (re-)done afterwards as they are often generated by reports
205         context.getSiteDirectories().clear();
206         context.addSiteDirectory( generatedSiteDirectory );
207 
208         Map<String, DocumentRenderer> generatedDocuments =
209             siteRenderer.locateDocumentFiles( context, false /* not editable */ );
210 
211         renderDoxiaDocuments( generatedDocuments, context, outputDir, true );
212 
213         // copy generated resources also
214         siteRenderer.copyResources( context, outputDir );
215     }
216 
217     /**
218      * Render Doxia documents from the list given, but not reports.
219      * 
220      * @param documents a collection of documents containing both Doxia source files and reports
221      * @return the sublist of documents that are not Doxia source files
222      */
223     private List<DocumentRenderer> renderDoxiaDocuments( Map<String, DocumentRenderer> documents,
224                                                          SiteRenderingContext context, File outputDir,
225                                                          boolean generated )
226                                                              throws RendererException, IOException
227     {
228         Map<String, DocumentRenderer> doxiaDocuments = new TreeMap<>();
229         List<DocumentRenderer> nonDoxiaDocuments = new ArrayList<>();
230 
231         Map<String, Integer> counts = new TreeMap<>();
232 
233         for ( Map.Entry<String, DocumentRenderer> entry : documents.entrySet() )
234         {
235             DocumentRenderer doc = entry.getValue();
236 
237             if ( doc instanceof DoxiaDocumentRenderer )
238             {
239                 doxiaDocuments.put( entry.getKey(), doc );
240 
241                 DoxiaDocumentRenderer doxia = (DoxiaDocumentRenderer) doc;
242 
243                 // count documents per parserId
244                 String parserId = doxia.getRenderingContext().getParserId();
245                 Integer count = counts.get( parserId );
246                 if ( count == null )
247                 {
248                     count = 1;
249                 }
250                 else
251                 {
252                     count++;
253                 }
254                 counts.put( parserId, count );
255             }
256             else
257             {
258                 nonDoxiaDocuments.add( doc );
259             }
260         }
261 
262         if ( doxiaDocuments.size() > 0 )
263         {
264             MessageBuilder mb = buffer();
265             mb.a( "Rendering " );
266             mb.strong( doxiaDocuments.size() + ( generated ? " generated" : "" ) + " Doxia document"
267                 + ( doxiaDocuments.size() > 1 ? "s" : "" ) );
268             mb.a( ": " );
269 
270             boolean first = true;
271             for ( Map.Entry<String, Integer> entry : counts.entrySet() )
272             {
273                 if ( first )
274                 {
275                     first = false;
276                 }
277                 else
278                 {
279                     mb.a( ", " );
280                 }
281                 mb.strong( entry.getValue() + " " + entry.getKey() );
282             }
283 
284             getLog().info( mb.toString() );
285 
286             siteRenderer.render( doxiaDocuments.values(), context, outputDir );
287         }
288 
289         return nonDoxiaDocuments;
290     }
291 
292     private File getOutputDirectory( Locale locale )
293     {
294         File file;
295         if ( locale.getLanguage().equals( Locale.getDefault().getLanguage() ) )
296         {
297             file = outputDirectory;
298         }
299         else
300         {
301             file = new File( outputDirectory, locale.getLanguage() );
302         }
303 
304         // Safety
305         if ( !file.exists() )
306         {
307             file.mkdirs();
308         }
309 
310         return file;
311     }
312 
313     public MavenProject getProject()
314     {
315         return project;
316     }
317 
318     public MavenSession getSession()
319     {
320         return mavenSession;
321     }
322 }