View Javadoc

1   package org.apache.maven.plugins.site;
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.doxia.siterenderer.RendererException;
23  import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugin.MojoFailureException;
26  import org.apache.maven.reporting.MavenReport;
27  
28  import java.io.File;
29  import java.io.IOException;
30  import java.util.Collections;
31  import java.util.Iterator;
32  import java.util.List;
33  import java.util.Locale;
34  import java.util.Map;
35  
36  /**
37   * Generates the site for a single project.
38   * <p>
39   * Note that links between module sites in a multi module build will <b>not</b>
40   * work.
41   * </p>
42   *
43   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
44   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
45   * @version $Id: SiteMojo.html 816554 2012-05-08 11:56:34Z hboutemy $
46   * @goal site
47   * @requiresDependencyResolution test
48   */
49  public class SiteMojo
50      extends AbstractSiteRenderingMojo
51  {
52      /**
53       * Directory containing the generated project sites and report distributions.
54       *
55       * @parameter expression="${siteOutputDirectory}" default-value="${project.reporting.outputDirectory}"
56       * @required
57       */
58      protected File outputDirectory;
59  
60      /**
61       * Convenience parameter that allows you to disable report generation.
62       *
63       * @parameter expression="${generateReports}" default-value="true"
64       */
65      private boolean generateReports;
66  
67      /**
68       * Generate a sitemap. The result will be a "sitemap.html" file at the site root.
69       *
70       * @parameter expression="${generateSitemap}" default-value="false"
71       * @since 2.1
72       */
73      private boolean generateSitemap;
74  
75      /**
76       * Whether to validate xml input documents.
77       * If set to true, <strong>all</strong> input documents in xml format
78       * (in particular xdoc and fml) will be validated and any error will
79       * lead to a build failure.
80       *
81       * @parameter expression="${validate}" default-value="false"
82       * @since 2.1.1
83       */
84      private boolean validate;
85  
86      /**
87       * {@inheritDoc}
88       *
89       * Generate the project site
90       * <p/>
91       * throws MojoExecutionException if any
92       *
93       * @see org.apache.maven.plugin.Mojo#execute()
94       */
95      public void execute()
96          throws MojoExecutionException, MojoFailureException
97      {
98          List filteredReports;
99          if ( generateReports )
100         {
101             filteredReports = filterReports( reports );
102         }
103         else
104         {
105             filteredReports = Collections.EMPTY_LIST;
106         }
107 
108         try
109         {
110             List localesList = siteTool.getAvailableLocales( locales );
111 
112             // Default is first in the list
113             Locale defaultLocale = (Locale) localesList.get( 0 );
114             Locale.setDefault( defaultLocale );
115 
116             for ( Iterator iterator = localesList.iterator(); iterator.hasNext(); )
117             {
118                 Locale locale = (Locale) iterator.next();
119 
120                 renderLocale( locale, filteredReports );
121             }
122         }
123         catch ( RendererException e )
124         {
125             throw new MojoExecutionException( "Error during page generation", e );
126         }
127         catch ( IOException e )
128         {
129             throw new MojoExecutionException( "Error during site generation", e );
130         }
131     }
132 
133     private void renderLocale( Locale locale,
134                                List reports )
135         throws IOException, RendererException, MojoFailureException, MojoExecutionException
136     {
137         SiteRenderingContext context = createSiteRenderingContext( locale );
138 
139         context.setInputEncoding( getInputEncoding() );
140         context.setOutputEncoding( getOutputEncoding() );
141         context.setValidate( validate );
142         if ( validate )
143         {
144             getLog().info( "Validation is switched on, xml input documents will be validated!" );
145         }
146 
147         Map documents = locateDocuments( context, reports, locale );
148 
149         File outputDir = getOutputDirectory( locale );
150 
151         // For external reports
152         for ( Iterator i = reports.iterator(); i.hasNext(); )
153         {
154             MavenReport report = (MavenReport) i.next();
155             report.setReportOutputDirectory( outputDir );
156         }
157 
158         siteRenderer.render( documents.values(), context, outputDir );
159 
160         if ( generateSitemap )
161         {
162             getLog().info( "Generating Sitemap." );
163 
164             new SiteMap( getOutputEncoding(), i18n )
165                     .generate( context.getDecoration(), generatedSiteDirectory, locale );
166         }
167 
168         // Generated docs must be done afterwards as they are often generated by reports
169         context.getSiteDirectories().clear();
170         context.addSiteDirectory( generatedSiteDirectory );
171 
172         documents = siteRenderer.locateDocumentFiles( context );
173 
174         siteRenderer.render( documents.values(), context, outputDir );
175     }
176 
177     private File getOutputDirectory( Locale locale )
178     {
179         File file;
180         if ( locale.getLanguage().equals( Locale.getDefault().getLanguage() ) )
181         {
182             file = outputDirectory;
183         }
184         else
185         {
186             file = new File( outputDirectory, locale.getLanguage() );
187         }
188 
189         // Safety
190         if ( !file.exists() )
191         {
192             file.mkdirs();
193         }
194 
195         return file;
196     }
197 }