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