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