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 816547 2012-05-08 11:51:09Z 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      /**
69       * Generate the project site
70       * <p/>
71       * throws MojoExecutionException if any
72       *
73       * @see org.apache.maven.plugin.Mojo#execute()
74       */
75      public void execute()
76          throws MojoExecutionException, MojoFailureException
77      {
78          List filteredReports;
79          if ( generateReports )
80          {
81              filteredReports = filterReports( reports );
82          }
83          else
84          {
85              filteredReports = Collections.EMPTY_LIST;
86          }
87  
88          try
89          {
90              List localesList = siteTool.getAvailableLocales( locales );
91  
92              // Default is first in the list
93              Locale defaultLocale = (Locale) localesList.get( 0 );
94              Locale.setDefault( defaultLocale );
95  
96              for ( Iterator iterator = localesList.iterator(); iterator.hasNext(); )
97              {
98                  Locale locale = (Locale) iterator.next();
99  
100                 renderLocale( locale, filteredReports );
101             }
102         }
103         catch ( RendererException e )
104         {
105             throw new MojoExecutionException( "Error during page generation", e );
106         }
107         catch ( IOException e )
108         {
109             throw new MojoExecutionException( "Error during site generation", e );
110         }
111     }
112 
113     private void renderLocale( Locale locale,
114                                List reports )
115         throws IOException, RendererException, MojoFailureException, MojoExecutionException
116     {
117         SiteRenderingContext context = createSiteRenderingContext( locale );
118 
119         context.setInputEncoding( getInputEncoding() );
120         context.setOutputEncoding( getOutputEncoding() );
121 
122         Map documents = locateDocuments( context, reports, locale );
123 
124         File outputDirectory = getOutputDirectory( locale );
125 
126         // For external reports
127         for ( Iterator i = reports.iterator(); i.hasNext(); )
128         {
129             MavenReport report = (MavenReport) i.next();
130             report.setReportOutputDirectory( outputDirectory );
131         }
132 
133         siteRenderer.render( documents.values(), context, outputDirectory );
134 
135         // Generated docs must be done afterwards as they are often generated by reports
136         context.getSiteDirectories().clear();
137         context.addSiteDirectory( generatedSiteDirectory );
138 
139         documents = siteRenderer.locateDocumentFiles( context );
140 
141         siteRenderer.render( documents.values(), context, outputDirectory );
142     }
143 
144     private File getOutputDirectory( Locale locale )
145     {
146         File file;
147         if ( locale.getLanguage().equals( Locale.getDefault().getLanguage() ) )
148         {
149             file = outputDirectory;
150         }
151         else
152         {
153             file = new File( outputDirectory, locale.getLanguage() );
154         }
155 
156         // Safety
157         if ( !file.exists() )
158         {
159             file.mkdirs();
160         }
161 
162         return file;
163     }
164 }