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.plugin.MojoExecutionException; |
40 | |
import org.apache.maven.project.MavenProject; |
41 | |
import org.apache.maven.reporting.AbstractMavenReport; |
42 | |
import org.apache.maven.reporting.MavenReportException; |
43 | |
import org.codehaus.plexus.i18n.I18N; |
44 | |
import org.codehaus.plexus.util.ReaderFactory; |
45 | |
|
46 | |
import java.io.File; |
47 | |
import java.io.FileOutputStream; |
48 | |
import java.io.OutputStreamWriter; |
49 | |
import java.io.IOException; |
50 | |
import java.io.Writer; |
51 | |
import java.util.HashMap; |
52 | |
import java.util.Locale; |
53 | |
import java.util.Map; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | 0 | public abstract class AbstractChangesReport |
65 | |
extends AbstractMavenReport |
66 | |
{ |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
private File outputDirectory; |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
private String outputEncoding; |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
protected Renderer siteRenderer; |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
protected MavenProject project; |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
protected ArtifactRepository localRepository; |
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
protected ArtifactResolver resolver; |
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
protected ArtifactFactory factory; |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
protected I18N i18n; |
127 | |
|
128 | |
private File getSkinArtifactFile() |
129 | |
throws MojoExecutionException |
130 | |
{ |
131 | 0 | Skin skin = Skin.getDefaultSkin(); |
132 | |
|
133 | 0 | String version = skin.getVersion(); |
134 | |
Artifact artifact; |
135 | |
try |
136 | |
{ |
137 | 0 | if ( version == null ) |
138 | |
{ |
139 | 0 | version = Artifact.RELEASE_VERSION; |
140 | |
} |
141 | 0 | VersionRange versionSpec = VersionRange.createFromVersionSpec( version ); |
142 | 0 | artifact = factory.createDependencyArtifact( skin.getGroupId(), skin.getArtifactId(), versionSpec, "jar", |
143 | |
null, null ); |
144 | |
|
145 | 0 | resolver.resolve( artifact, project.getRemoteArtifactRepositories(), localRepository ); |
146 | |
} |
147 | 0 | catch ( InvalidVersionSpecificationException e ) |
148 | |
{ |
149 | 0 | throw new MojoExecutionException( "The skin version '" + version + "' is not valid: " + e.getMessage() ); |
150 | |
} |
151 | 0 | catch ( ArtifactResolutionException e ) |
152 | |
{ |
153 | 0 | throw new MojoExecutionException( "Unable to find skin", e ); |
154 | |
} |
155 | 0 | catch ( ArtifactNotFoundException e ) |
156 | |
{ |
157 | 0 | throw new MojoExecutionException( "The skin does not exist: " + e.getMessage() ); |
158 | 0 | } |
159 | |
|
160 | 0 | return artifact.getFile(); |
161 | |
} |
162 | |
|
163 | |
public void execute() |
164 | |
throws MojoExecutionException |
165 | |
{ |
166 | 0 | if ( !canGenerateReport() ) |
167 | |
{ |
168 | 0 | return; |
169 | |
} |
170 | |
|
171 | |
|
172 | 0 | FileOutputStream fileOutputStream = null; |
173 | |
try |
174 | |
{ |
175 | 0 | DecorationModel model = new DecorationModel(); |
176 | 0 | model.setBody( new Body() ); |
177 | 0 | Map attributes = new HashMap(); |
178 | 0 | attributes.put( "outputEncoding", getOutputEncoding() ); |
179 | 0 | Locale locale = Locale.getDefault(); |
180 | 0 | SiteRenderingContext siteContext = siteRenderer.createContextForSkin( getSkinArtifactFile(), attributes, |
181 | |
model, getName( locale ), locale ); |
182 | 0 | siteContext.setOutputEncoding( getOutputEncoding() ); |
183 | |
|
184 | 0 | RenderingContext context = new RenderingContext( outputDirectory, getOutputName() + ".html" ); |
185 | |
|
186 | 0 | SiteRendererSink sink = new SiteRendererSink( context ); |
187 | 0 | generate( sink, locale ); |
188 | |
|
189 | 0 | outputDirectory.mkdirs(); |
190 | |
|
191 | 0 | File file = new File( outputDirectory, getOutputName() + ".html" ); |
192 | 0 | fileOutputStream = new FileOutputStream( file ) ; |
193 | 0 | Writer writer = new OutputStreamWriter( fileOutputStream, getOutputEncoding() ); |
194 | |
|
195 | 0 | siteRenderer.generateDocument( writer, sink, siteContext ); |
196 | |
|
197 | 0 | siteRenderer.copyResources( siteContext, new File( project.getBasedir(), "src/site/resources" ), |
198 | |
outputDirectory ); |
199 | |
} |
200 | 0 | catch ( RendererException e ) |
201 | |
{ |
202 | 0 | throw new MojoExecutionException( |
203 | |
"An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e ); |
204 | |
} |
205 | 0 | catch ( IOException e ) |
206 | |
{ |
207 | 0 | throw new MojoExecutionException( |
208 | |
"An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e ); |
209 | |
} |
210 | 0 | catch ( MavenReportException e ) |
211 | |
{ |
212 | 0 | throw new MojoExecutionException( |
213 | |
"An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e ); |
214 | |
} |
215 | |
finally { |
216 | 0 | IOUtils.closeQuietly( fileOutputStream ); |
217 | 0 | } |
218 | 0 | } |
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
protected String getOutputDirectory() |
224 | |
{ |
225 | 0 | return outputDirectory.getAbsolutePath(); |
226 | |
} |
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
protected String getOutputEncoding() |
235 | |
{ |
236 | 0 | return ( outputEncoding != null ) ? outputEncoding : ReaderFactory.UTF_8; |
237 | |
} |
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
protected MavenProject getProject() |
243 | |
{ |
244 | 0 | return project; |
245 | |
} |
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
protected Renderer getSiteRenderer() |
251 | |
{ |
252 | 0 | return siteRenderer; |
253 | |
} |
254 | |
} |