1 | |
package org.apache.maven.report.projectinfo; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.maven.artifact.Artifact; |
23 | |
import org.apache.maven.artifact.factory.ArtifactFactory; |
24 | |
import org.apache.maven.artifact.repository.ArtifactRepository; |
25 | |
import org.apache.maven.artifact.resolver.ArtifactResolver; |
26 | |
import org.apache.maven.doxia.sink.render.RenderingContext; |
27 | |
import org.apache.maven.doxia.site.decoration.Body; |
28 | |
import org.apache.maven.doxia.site.decoration.DecorationModel; |
29 | |
import org.apache.maven.doxia.siterenderer.Renderer; |
30 | |
import org.apache.maven.doxia.siterenderer.RendererException; |
31 | |
import org.apache.maven.doxia.siterenderer.SiteRenderingContext; |
32 | |
import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink; |
33 | |
import org.apache.maven.doxia.tools.SiteTool; |
34 | |
import org.apache.maven.doxia.tools.SiteToolException; |
35 | |
import org.apache.maven.plugin.MojoExecutionException; |
36 | |
import org.apache.maven.project.MavenProject; |
37 | |
import org.apache.maven.reporting.AbstractMavenReport; |
38 | |
import org.apache.maven.reporting.MavenReportException; |
39 | |
import org.codehaus.plexus.i18n.I18N; |
40 | |
import org.codehaus.plexus.util.IOUtil; |
41 | |
|
42 | |
import java.io.File; |
43 | |
import java.io.FileOutputStream; |
44 | |
import java.io.IOException; |
45 | |
import java.io.OutputStreamWriter; |
46 | |
import java.io.Writer; |
47 | |
import java.util.HashMap; |
48 | |
import java.util.Locale; |
49 | |
import java.util.Map; |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | 17 | public abstract class AbstractProjectInfoReport |
59 | |
extends AbstractMavenReport |
60 | |
{ |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
protected SiteTool siteTool; |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
protected Renderer siteRenderer; |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
protected ArtifactResolver resolver; |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
protected ArtifactFactory factory; |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
protected I18N i18n; |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
protected File outputDirectory; |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
protected MavenProject project; |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
protected ArtifactRepository localRepository; |
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
public void execute() |
139 | |
throws MojoExecutionException |
140 | |
{ |
141 | 17 | if ( !canGenerateReport() ) |
142 | |
{ |
143 | 0 | return; |
144 | |
} |
145 | |
|
146 | |
|
147 | 17 | Writer writer = null; |
148 | |
try |
149 | |
{ |
150 | 17 | String filename = getOutputName() + ".html"; |
151 | |
|
152 | 17 | DecorationModel model = new DecorationModel(); |
153 | 17 | model.setBody( new Body() ); |
154 | |
|
155 | 17 | Map attributes = new HashMap(); |
156 | 17 | attributes.put( "outputEncoding", "UTF-8" ); |
157 | 17 | attributes.put( "project", project ); |
158 | |
|
159 | 17 | Locale locale = Locale.getDefault(); |
160 | 17 | Artifact defaultSkin = |
161 | |
siteTool.getDefaultSkinArtifact( localRepository, project.getRemoteArtifactRepositories() ); |
162 | |
|
163 | 17 | SiteRenderingContext siteContext = siteRenderer.createContextForSkin( defaultSkin.getFile(), attributes, |
164 | |
model, getName( locale ), locale ); |
165 | |
|
166 | 17 | RenderingContext context = new RenderingContext( outputDirectory, filename ); |
167 | |
|
168 | 17 | SiteRendererSink sink = new SiteRendererSink( context ); |
169 | |
|
170 | 17 | generate( sink, null, locale ); |
171 | |
|
172 | 14 | outputDirectory.mkdirs(); |
173 | |
|
174 | 14 | writer = new OutputStreamWriter( new FileOutputStream( new File( outputDirectory, filename ) ), "UTF-8" ); |
175 | |
|
176 | 14 | siteRenderer.generateDocument( writer, sink, siteContext ); |
177 | |
|
178 | 14 | siteRenderer.copyResources( siteContext, new File( project.getBasedir(), "src/site/resources" ), |
179 | |
outputDirectory ); |
180 | |
} |
181 | 0 | catch ( RendererException e ) |
182 | |
{ |
183 | 0 | throw new MojoExecutionException( |
184 | |
"An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e ); |
185 | |
} |
186 | 0 | catch ( IOException e ) |
187 | |
{ |
188 | 0 | throw new MojoExecutionException( |
189 | |
"An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e ); |
190 | |
} |
191 | 0 | catch ( SiteToolException e ) |
192 | |
{ |
193 | 0 | throw new MojoExecutionException( |
194 | |
"An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e ); |
195 | |
} |
196 | 0 | catch ( MavenReportException e ) |
197 | |
{ |
198 | 0 | throw new MojoExecutionException( |
199 | |
"An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e ); |
200 | |
} |
201 | |
finally |
202 | |
{ |
203 | 17 | IOUtil.close( writer ); |
204 | 14 | } |
205 | 14 | } |
206 | |
|
207 | |
|
208 | |
public String getCategoryName() |
209 | |
{ |
210 | 0 | return CATEGORY_PROJECT_INFORMATION; |
211 | |
} |
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
protected String getOutputDirectory() |
219 | |
{ |
220 | 1 | return outputDirectory.getAbsolutePath(); |
221 | |
} |
222 | |
|
223 | |
|
224 | |
public File getReportOutputDirectory() |
225 | |
{ |
226 | 14 | return outputDirectory; |
227 | |
} |
228 | |
|
229 | |
|
230 | |
public void setReportOutputDirectory( File reportOutputDirectory ) |
231 | |
{ |
232 | 0 | this.outputDirectory = reportOutputDirectory; |
233 | 0 | } |
234 | |
|
235 | |
|
236 | |
protected MavenProject getProject() |
237 | |
{ |
238 | 14 | return project; |
239 | |
} |
240 | |
|
241 | |
|
242 | |
protected Renderer getSiteRenderer() |
243 | |
{ |
244 | 0 | return siteRenderer; |
245 | |
} |
246 | |
|
247 | |
protected String getI18nString( Locale locale, String key ) |
248 | |
{ |
249 | 33 | return i18n.getString( "project-info-report", locale, "report." + getI18Nsection() + '.' + key ); |
250 | |
} |
251 | |
|
252 | |
protected abstract String getI18Nsection(); |
253 | |
|
254 | |
|
255 | |
public String getName( Locale locale ) |
256 | |
{ |
257 | 16 | return getI18nString( locale, "name" ); |
258 | |
} |
259 | |
|
260 | |
|
261 | |
public String getDescription( Locale locale ) |
262 | |
{ |
263 | 0 | return getI18nString( locale, "description" ); |
264 | |
} |
265 | |
} |