1 package org.apache.maven.plugin.changes;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.io.IOUtils;
23 import org.apache.maven.artifact.Artifact;
24 import org.apache.maven.artifact.factory.ArtifactFactory;
25 import org.apache.maven.artifact.repository.ArtifactRepository;
26 import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
27 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
28 import org.apache.maven.artifact.resolver.ArtifactResolver;
29 import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
30 import org.apache.maven.artifact.versioning.VersionRange;
31 import org.apache.maven.doxia.sink.render.RenderingContext;
32 import org.apache.maven.doxia.site.decoration.Body;
33 import org.apache.maven.doxia.site.decoration.DecorationModel;
34 import org.apache.maven.doxia.site.decoration.Skin;
35 import org.apache.maven.doxia.siterenderer.Renderer;
36 import org.apache.maven.doxia.siterenderer.RendererException;
37 import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
38 import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink;
39 import org.apache.maven.execution.MavenSession;
40 import org.apache.maven.plugin.MojoExecutionException;
41 import org.apache.maven.plugins.annotations.Component;
42 import org.apache.maven.plugins.annotations.Parameter;
43 import org.apache.maven.project.MavenProject;
44 import org.apache.maven.reporting.AbstractMavenReport;
45 import org.apache.maven.reporting.MavenReportException;
46 import org.codehaus.plexus.i18n.I18N;
47 import org.codehaus.plexus.util.ReaderFactory;
48
49 import java.io.File;
50 import java.io.FileOutputStream;
51 import java.io.OutputStreamWriter;
52 import java.io.IOException;
53 import java.io.Writer;
54 import java.util.HashMap;
55 import java.util.Locale;
56 import java.util.Map;
57
58
59
60
61
62
63
64
65
66
67 public abstract class AbstractChangesReport
68 extends AbstractMavenReport
69 {
70
71
72
73
74
75 @Parameter( property = "basedir", required = true )
76 protected String basedir;
77
78
79
80
81
82
83 @Parameter( defaultValue = "${project.reporting.outputDirectory}" )
84 private File outputDirectory;
85
86
87
88
89
90
91
92
93 @Parameter( property = "outputEncoding", defaultValue = "${project.reporting.outputEncoding}" )
94 private String outputEncoding;
95
96
97
98
99
100
101
102
103 @Parameter( property = "changes.runOnlyAtExecutionRoot", defaultValue = "false" )
104 protected boolean runOnlyAtExecutionRoot;
105
106
107
108
109
110
111 @Parameter( defaultValue = "${session}", readonly = true, required = true )
112 protected MavenSession mavenSession;
113
114
115
116
117 @Component
118 protected Renderer siteRenderer;
119
120
121
122
123 @Parameter( defaultValue = "${project}", readonly = true, required = true )
124 protected MavenProject project;
125
126
127
128
129 @Parameter( property = "localRepository", required = true, readonly = true )
130 protected ArtifactRepository localRepository;
131
132
133
134 @Component
135 protected ArtifactResolver resolver;
136
137
138
139 @Component
140 protected ArtifactFactory factory;
141
142
143
144
145 @Component
146 protected I18N i18n;
147
148 private File getSkinArtifactFile()
149 throws MojoExecutionException
150 {
151 Skin skin = Skin.getDefaultSkin();
152
153 String version = skin.getVersion();
154 Artifact artifact;
155 try
156 {
157 if ( version == null )
158 {
159 version = Artifact.RELEASE_VERSION;
160 }
161 VersionRange versionSpec = VersionRange.createFromVersionSpec( version );
162 artifact = factory.createDependencyArtifact( skin.getGroupId(), skin.getArtifactId(), versionSpec, "jar",
163 null, null );
164
165 resolver.resolve( artifact, project.getRemoteArtifactRepositories(), localRepository );
166 }
167 catch ( InvalidVersionSpecificationException e )
168 {
169 throw new MojoExecutionException( "The skin version '" + version + "' is not valid: " + e.getMessage() );
170 }
171 catch ( ArtifactResolutionException e )
172 {
173 throw new MojoExecutionException( "Unable to find skin", e );
174 }
175 catch ( ArtifactNotFoundException e )
176 {
177 throw new MojoExecutionException( "The skin does not exist: " + e.getMessage() );
178 }
179
180 return artifact.getFile();
181 }
182
183 public void execute()
184 throws MojoExecutionException
185 {
186 if ( !canGenerateReport() )
187 {
188 return;
189 }
190
191
192 FileOutputStream fileOutputStream = null;
193 try
194 {
195 DecorationModel model = new DecorationModel();
196 model.setBody( new Body() );
197 Map<String, String> attributes = new HashMap<String, String>();
198 attributes.put( "outputEncoding", getOutputEncoding() );
199 Locale locale = Locale.getDefault();
200 SiteRenderingContext siteContext = siteRenderer.createContextForSkin( getSkinArtifactFile(), attributes,
201 model, getName( locale ), locale );
202 siteContext.setOutputEncoding( getOutputEncoding() );
203
204 RenderingContext context = new RenderingContext( outputDirectory, getOutputName() + ".html" );
205
206 SiteRendererSink sink = new SiteRendererSink( context );
207 generate( sink, locale );
208
209 outputDirectory.mkdirs();
210
211 File file = new File( outputDirectory, getOutputName() + ".html" );
212 fileOutputStream = new FileOutputStream( file ) ;
213 Writer writer = new OutputStreamWriter( fileOutputStream, getOutputEncoding() );
214
215 siteRenderer.generateDocument( writer, sink, siteContext );
216
217 siteRenderer.copyResources( siteContext, new File( project.getBasedir(), "src/site/resources" ),
218 outputDirectory );
219 }
220 catch ( RendererException e )
221 {
222 throw new MojoExecutionException(
223 "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e );
224 }
225 catch ( IOException e )
226 {
227 throw new MojoExecutionException(
228 "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e );
229 }
230 catch ( MavenReportException e )
231 {
232 throw new MojoExecutionException(
233 "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e );
234 }
235 finally
236 {
237 IOUtils.closeQuietly( fileOutputStream );
238 }
239 }
240
241
242
243
244 protected String getOutputDirectory()
245 {
246 return outputDirectory.getAbsolutePath();
247 }
248
249
250
251
252
253
254
255 protected String getOutputEncoding()
256 {
257 return ( outputEncoding != null ) ? outputEncoding : ReaderFactory.UTF_8;
258 }
259
260
261
262
263 protected MavenProject getProject()
264 {
265 return project;
266 }
267
268
269
270
271 protected Renderer getSiteRenderer()
272 {
273 return siteRenderer;
274 }
275
276
277
278
279
280
281
282 protected boolean isThisTheExecutionRoot()
283 {
284 getLog().debug( "Root Folder:" + mavenSession.getExecutionRootDirectory() );
285 getLog().debug( "Current Folder:" + basedir );
286 boolean result = mavenSession.getExecutionRootDirectory().equalsIgnoreCase( basedir );
287 if ( result )
288 {
289 getLog().debug( "This is the execution root." );
290 }
291 else
292 {
293 getLog().debug( "This is NOT the execution root." );
294 }
295 return result;
296 }
297 }