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