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 java.io.File;
23  import java.io.IOException;
24  
25  import java.util.Collections;
26  import java.util.List;
27  import java.util.Locale;
28  import java.util.Map;
29  
30  import org.apache.maven.doxia.siterenderer.DocumentRenderer;
31  import org.apache.maven.doxia.siterenderer.RendererException;
32  import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
33  import org.apache.maven.plugin.MojoExecutionException;
34  import org.apache.maven.plugin.MojoFailureException;
35  import org.apache.maven.reporting.MavenReport;
36  
37  /**
38   * Generates the site for a single project.
39   * <p>
40   * Note that links between module sites in a multi module build will <b>not</b>
41   * work.
42   * </p>
43   *
44   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
45   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
46   * @version $Id: SiteMojo.html 816558 2012-05-08 12:00:46Z hboutemy $
47   * @goal site
48   * @requiresDependencyResolution test
49   */
50  public class SiteMojo
51      extends AbstractSiteRenderingMojo
52  {
53      /**
54       * Directory where the project sites and report distributions will be generated.
55       *
56       * @parameter expression="${siteOutputDirectory}" default-value="${project.reporting.outputDirectory}"
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          checkMavenVersion();
99  
100         List<MavenReport> filteredReports;
101         if ( generateReports )
102         {
103             filteredReports = filterReports( reports );
104         }
105         else
106         {
107             filteredReports = Collections.emptyList();
108         }
109 
110         try
111         {
112             List<Locale> localesList = siteTool.getAvailableLocales( locales );
113 
114             // Default is first in the list
115             Locale defaultLocale = localesList.get( 0 );
116             Locale.setDefault( defaultLocale );
117 
118             for ( Locale locale : localesList )
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, List<MavenReport> reports )
134         throws IOException, RendererException, MojoFailureException, MojoExecutionException
135     {
136         SiteRenderingContext context = createSiteRenderingContext( locale );
137 
138         context.setInputEncoding( getInputEncoding() );
139         context.setOutputEncoding( getOutputEncoding() );
140         context.setValidate( validate );
141         if ( validate )
142         {
143             getLog().info( "Validation is switched on, xml input documents will be validated!" );
144         }
145 
146         Map<String, DocumentRenderer> documents = locateDocuments( context, reports, locale );
147 
148         File outputDir = getOutputDirectory( locale );
149 
150         // For external reports
151         for ( MavenReport report : reports )
152         {
153             report.setReportOutputDirectory( outputDir );
154         }
155 
156         siteRenderer.render( documents.values(), context, outputDir );
157 
158         if ( generateSitemap )
159         {
160             getLog().info( "Generating Sitemap." );
161 
162             new SiteMap( getOutputEncoding(), i18n )
163                     .generate( context.getDecoration(), generatedSiteDirectory, locale );
164         }
165 
166         // Generated docs must be done afterwards as they are often generated by reports
167         context.getSiteDirectories().clear();
168         context.addSiteDirectory( generatedSiteDirectory );
169 
170         documents = siteRenderer.locateDocumentFiles( context );
171 
172         siteRenderer.render( documents.values(), context, outputDir );
173     }
174 
175     private File getOutputDirectory( Locale locale )
176     {
177         File file;
178         if ( locale.getLanguage().equals( Locale.getDefault().getLanguage() ) )
179         {
180             file = outputDirectory;
181         }
182         else
183         {
184             file = new File( outputDirectory, locale.getLanguage() );
185         }
186 
187         // Safety
188         if ( !file.exists() )
189         {
190             file.mkdirs();
191         }
192 
193         return file;
194     }
195 }