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