1 | |
package org.apache.maven.plugins.site; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
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 | |
import org.apache.maven.reporting.exec.MavenReportExecution; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | 1 | public class SiteMojo |
52 | |
extends AbstractSiteRenderingMojo |
53 | |
{ |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
protected File outputDirectory; |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
private boolean generateReports; |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
private boolean generateSitemap; |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
private boolean validate; |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
private boolean skip; |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
public void execute() |
105 | |
throws MojoExecutionException, MojoFailureException |
106 | |
{ |
107 | 0 | if ( skip ) |
108 | |
{ |
109 | 0 | getLog().info( "maven.site.skip = true: Skipping site generation" ); |
110 | 0 | return; |
111 | |
} |
112 | |
|
113 | 0 | if ( getLog().isDebugEnabled() ) |
114 | |
{ |
115 | 0 | getLog().debug( "executing Site Mojo" ); |
116 | |
} |
117 | |
|
118 | |
List<MavenReportExecution> reports; |
119 | 0 | if ( generateReports ) |
120 | |
{ |
121 | 0 | reports = getReports(); |
122 | |
} |
123 | |
else |
124 | |
{ |
125 | 0 | reports = Collections.emptyList(); |
126 | |
} |
127 | |
|
128 | |
try |
129 | |
{ |
130 | 0 | List<Locale> localesList = siteTool.getAvailableLocales( locales ); |
131 | |
|
132 | |
|
133 | 0 | Locale defaultLocale = localesList.get( 0 ); |
134 | 0 | Locale.setDefault( defaultLocale ); |
135 | |
|
136 | 0 | for ( Locale locale : localesList ) |
137 | |
{ |
138 | 0 | renderLocale( locale, reports ); |
139 | |
} |
140 | |
} |
141 | 0 | catch ( RendererException e ) |
142 | |
{ |
143 | 0 | throw new MojoExecutionException( "Error during page generation", e ); |
144 | |
} |
145 | 0 | catch ( IOException e ) |
146 | |
{ |
147 | 0 | throw new MojoExecutionException( "Error during site generation", e ); |
148 | 0 | } |
149 | 0 | } |
150 | |
|
151 | |
private void renderLocale( Locale locale, List<MavenReportExecution> reports ) |
152 | |
throws IOException, RendererException, MojoFailureException, MojoExecutionException |
153 | |
{ |
154 | 0 | SiteRenderingContext context = createSiteRenderingContext( locale ); |
155 | |
|
156 | 0 | context.setInputEncoding( getInputEncoding() ); |
157 | 0 | context.setOutputEncoding( getOutputEncoding() ); |
158 | 0 | context.setValidate( validate ); |
159 | 0 | if ( validate ) |
160 | |
{ |
161 | 0 | getLog().info( "Validation is switched on, xml input documents will be validated!" ); |
162 | |
} |
163 | |
|
164 | 0 | Map<String, DocumentRenderer> documents = locateDocuments( context, reports, locale ); |
165 | |
|
166 | 0 | File outputDir = getOutputDirectory( locale ); |
167 | |
|
168 | |
|
169 | 0 | for ( MavenReportExecution mavenReportExecution : reports ) |
170 | |
{ |
171 | 0 | MavenReport report = mavenReportExecution.getMavenReport(); |
172 | 0 | report.setReportOutputDirectory( outputDir ); |
173 | 0 | } |
174 | |
|
175 | 0 | siteRenderer.render( documents.values(), context, outputDir ); |
176 | |
|
177 | 0 | if ( generateSitemap ) |
178 | |
{ |
179 | 0 | getLog().info( "Generating Sitemap." ); |
180 | |
|
181 | 0 | new SiteMap( getOutputEncoding(), i18n ) |
182 | |
.generate( context.getDecoration(), generatedSiteDirectory, locale ); |
183 | |
} |
184 | |
|
185 | |
|
186 | 0 | context.getSiteDirectories().clear(); |
187 | 0 | context.addSiteDirectory( generatedSiteDirectory ); |
188 | |
|
189 | 0 | documents = siteRenderer.locateDocumentFiles( context ); |
190 | |
|
191 | 0 | siteRenderer.render( documents.values(), context, outputDir ); |
192 | 0 | } |
193 | |
|
194 | |
private File getOutputDirectory( Locale locale ) |
195 | |
{ |
196 | |
File file; |
197 | 0 | if ( locale.getLanguage().equals( Locale.getDefault().getLanguage() ) ) |
198 | |
{ |
199 | 0 | file = outputDirectory; |
200 | |
} |
201 | |
else |
202 | |
{ |
203 | 0 | file = new File( outputDirectory, locale.getLanguage() ); |
204 | |
} |
205 | |
|
206 | |
|
207 | 0 | if ( !file.exists() ) |
208 | |
{ |
209 | 0 | file.mkdirs(); |
210 | |
} |
211 | |
|
212 | 0 | return file; |
213 | |
} |
214 | |
} |