View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.site.render;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.List;
26  import java.util.Locale;
27  import java.util.Map;
28  import java.util.TreeMap;
29  
30  import org.apache.maven.doxia.siterenderer.DocumentRenderer;
31  import org.apache.maven.doxia.siterenderer.DoxiaDocumentRenderer;
32  import org.apache.maven.doxia.siterenderer.RendererException;
33  import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
34  import org.apache.maven.doxia.tools.SiteTool;
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 extends AbstractSiteRenderingMojo {
62      /**
63       * Directory where the project sites and report distributions will be generated (as html/css/...).
64       */
65      @Parameter(property = "siteOutputDirectory", defaultValue = "${project.reporting.outputDirectory}")
66      protected File outputDirectory;
67  
68      /**
69       * Convenience parameter that allows you to disable report generation.
70       */
71      @Parameter(property = "generateReports", defaultValue = "true")
72      private boolean generateReports;
73  
74      /**
75       * Generate a sitemap. The result will be a "sitemap.html" file at the site root.
76       *
77       * @since 2.1
78       */
79      @Parameter(property = "generateSitemap", defaultValue = "false")
80      private boolean generateSitemap;
81  
82      /**
83       * Whether to validate xml input documents. If set to true, <strong>all</strong> input documents in xml format (in
84       * particular xdoc and fml) will be validated and any error will lead to a build failure.
85       *
86       * @since 2.1.1
87       */
88      @Parameter(property = "validate", defaultValue = "false")
89      private boolean validate;
90  
91      /**
92       * {@inheritDoc}
93       */
94      public void execute() throws MojoExecutionException, MojoFailureException {
95          if (skip) {
96              getLog().info("maven.site.skip = true: Skipping site generation");
97              return;
98          }
99  
100         if (getLog().isDebugEnabled()) {
101             getLog().debug("executing Site Mojo");
102         }
103 
104         checkInputEncoding();
105 
106         List<MavenReportExecution> reports;
107         if (generateReports) {
108             reports = getReports();
109         } else {
110             reports = Collections.emptyList();
111         }
112 
113         try {
114             List<Locale> localesList = getLocales();
115 
116             // Default is first in the list
117             Locale defaultLocale = localesList.get(0);
118 
119             for (Locale locale : localesList) {
120                 getLog().info("Rendering site for "
121                         + buffer().strong(
122                                         (!locale.equals(defaultLocale) ? "locale '" + locale + "'" : "default locale"))
123                                 .toString());
124                 renderLocale(locale, reports, localesList);
125             }
126         } catch (RendererException e) {
127             if (e.getCause() instanceof MavenReportException) {
128                 // issue caused by report, not really by Doxia Site Renderer
129                 throw new MojoExecutionException(e.getMessage(), e.getCause());
130             }
131             throw new MojoExecutionException(e.getMessage(), e);
132         } catch (IOException e) {
133             throw new MojoExecutionException("Error during site generation", e);
134         }
135     }
136 
137     private void renderLocale(Locale locale, List<MavenReportExecution> reports, List<Locale> supportedLocales)
138             throws IOException, RendererException, MojoFailureException, MojoExecutionException {
139         SiteRenderingContext context = createSiteRenderingContext(locale);
140         context.addSiteLocales(supportedLocales);
141         if (!locale.equals(SiteTool.DEFAULT_LOCALE)) {
142             context.addSiteDirectory(new File(generatedSiteDirectory, locale.toString()));
143         } else {
144             context.addSiteDirectory(generatedSiteDirectory);
145         }
146 
147         context.setInputEncoding(getInputEncoding());
148         context.setOutputEncoding(getOutputEncoding());
149         context.setValidate(validate);
150         if (validate) {
151             getLog().info("Validation is switched on, xml input documents will be validated!");
152         }
153 
154         File outputDir = getOutputDirectory(locale);
155 
156         Map<String, DocumentRenderer> documents = locateDocuments(context, reports, locale);
157 
158         // copy resources
159         siteRenderer.copyResources(context, outputDir);
160 
161         // 1. render Doxia documents first
162         List<DocumentRenderer> reportDocuments = renderDoxiaDocuments(documents, context, outputDir, false);
163 
164         // 2. then reports
165         // prepare external reports
166         for (MavenReportExecution mavenReportExecution : reports) {
167             MavenReport report = mavenReportExecution.getMavenReport();
168             report.setReportOutputDirectory(outputDir);
169         }
170 
171         siteRenderer.render(reportDocuments, context, outputDir);
172 
173         if (generateSitemap) {
174             getLog().info("Generating Sitemap");
175 
176             new SiteMap(getOutputEncoding(), i18n).generate(context.getSiteModel(), generatedSiteDirectory, locale);
177         }
178 
179         // 3. Generated docs must be (re-)done afterwards as they are often generated by reports
180         context.getSiteDirectories().clear();
181         if (!locale.equals(SiteTool.DEFAULT_LOCALE)) {
182             context.addSiteDirectory(new File(generatedSiteDirectory, locale.toString()));
183         } else {
184             context.addSiteDirectory(generatedSiteDirectory);
185         }
186 
187         Map<String, DocumentRenderer> generatedDocuments =
188                 siteRenderer.locateDocumentFiles(context, false /* not editable */);
189 
190         renderDoxiaDocuments(generatedDocuments, context, outputDir, true);
191 
192         // copy generated resources also
193         siteRenderer.copyResources(context, outputDir);
194     }
195 
196     /**
197      * Render Doxia documents from the list given, but not reports.
198      *
199      * @param documents a collection of documents containing both Doxia source files and reports
200      * @return the sublist of documents that are not Doxia source files
201      */
202     private List<DocumentRenderer> renderDoxiaDocuments(
203             Map<String, DocumentRenderer> documents, SiteRenderingContext context, File outputDir, boolean generated)
204             throws RendererException, IOException {
205         Map<String, DocumentRenderer> doxiaDocuments = new TreeMap<>();
206         List<DocumentRenderer> nonDoxiaDocuments = new ArrayList<>();
207 
208         Map<String, Integer> counts = new TreeMap<>();
209 
210         for (Map.Entry<String, DocumentRenderer> entry : documents.entrySet()) {
211             DocumentRenderer doc = entry.getValue();
212 
213             if (doc instanceof DoxiaDocumentRenderer) {
214                 doxiaDocuments.put(entry.getKey(), doc);
215 
216                 DoxiaDocumentRenderer doxia = (DoxiaDocumentRenderer) doc;
217 
218                 // count documents per parserId
219                 String parserId = doxia.getRenderingContext().getParserId();
220                 Integer count = counts.get(parserId);
221                 if (count == null) {
222                     count = 1;
223                 } else {
224                     count++;
225                 }
226                 counts.put(parserId, count);
227             } else {
228                 nonDoxiaDocuments.add(doc);
229             }
230         }
231 
232         if (doxiaDocuments.size() > 0) {
233             MessageBuilder mb = buffer();
234             mb.a("Rendering ");
235             mb.strong(doxiaDocuments.size() + (generated ? " generated" : "") + " Doxia document"
236                     + (doxiaDocuments.size() > 1 ? "s" : ""));
237             mb.a(": ");
238 
239             boolean first = true;
240             for (Map.Entry<String, Integer> entry : counts.entrySet()) {
241                 if (first) {
242                     first = false;
243                 } else {
244                     mb.a(", ");
245                 }
246                 mb.strong(entry.getValue() + " " + entry.getKey());
247             }
248 
249             getLog().info(mb.toString());
250 
251             siteRenderer.render(doxiaDocuments.values(), context, outputDir);
252         }
253 
254         return nonDoxiaDocuments;
255     }
256 
257     private File getOutputDirectory(Locale locale) {
258         File file;
259         if (!locale.equals(SiteTool.DEFAULT_LOCALE)) {
260             file = new File(outputDirectory, locale.toString());
261         } else {
262             file = outputDirectory;
263         }
264 
265         // Safety
266         if (!file.exists()) {
267             file.mkdirs();
268         }
269 
270         return file;
271     }
272 
273     public MavenProject getProject() {
274         return project;
275     }
276 
277     public MavenSession getSession() {
278         return mavenSession;
279     }
280 }