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