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