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 java.io.File;
23 import java.io.IOException;
24 import java.net.URL;
25 import java.text.SimpleDateFormat;
26 import java.util.Collections;
27 import java.util.Date;
28 import java.util.Locale;
29 import java.util.Map;
30 import java.util.Properties;
31 import java.util.ResourceBundle;
32
33 import org.apache.maven.execution.MavenSession;
34 import org.apache.maven.reporting.MavenReportException;
35 import org.apache.maven.shared.filtering.MavenFileFilter;
36 import org.apache.maven.shared.filtering.MavenFileFilterRequest;
37 import org.apache.maven.shared.filtering.MavenFilteringException;
38 import org.codehaus.plexus.util.FileUtils;
39 import org.codehaus.plexus.util.IOUtil;
40 import org.codehaus.plexus.util.ReaderFactory;
41 import org.codehaus.plexus.util.xml.XmlStreamReader;
42
43
44
45
46
47
48
49
50 public class ChangesMojo
51 extends AbstractChangesReport
52 {
53
54
55
56
57
58 private File xmlPath;
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 private String issueLinkTemplate;
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91 private Map issueLinkTemplatePerSystem;
92
93
94
95
96
97 private String url;
98
99
100
101
102
103
104
105
106 private boolean addActionDate;
107
108
109
110
111
112
113
114 private MavenFileFilter mavenFileFilter;
115
116
117
118
119
120
121
122
123
124 protected MavenSession session;
125
126
127
128
129
130
131
132
133 private boolean filteringChanges;
134
135
136
137
138
139
140
141
142
143
144
145 private File filteredOutputDirectory;
146
147
148
149
150
151
152
153
154
155
156
157
158 private String publishDateFormat;
159
160
161
162
163
164
165
166
167
168
169
170
171 private String publishDateLocale;
172
173
174 public boolean canGenerateReport()
175 {
176 return xmlPath.isFile();
177 }
178
179 private void copyStaticResources()
180 throws MavenReportException
181 {
182 final String pluginResourcesBase = "org/apache/maven/plugin/changes";
183 String resourceNames[] = {
184 "images/add.gif",
185 "images/fix.gif",
186 "images/icon_help_sml.gif",
187 "images/remove.gif",
188 "images/rss.png",
189 "images/update.gif" };
190 try
191 {
192 getLog().debug( "Copying static resources." );
193 for ( int i = 0; i < resourceNames.length; i++ )
194 {
195 URL url = this.getClass().getClassLoader().getResource( pluginResourcesBase + "/" + resourceNames[i] );
196 FileUtils.copyURLToFile( url, new File( getReportOutputDirectory(), resourceNames[i] ) );
197 }
198 }
199 catch ( IOException e )
200 {
201 throw new MavenReportException( "Unable to copy static resources." );
202 }
203 }
204
205 public void executeReport( Locale locale )
206 throws MavenReportException
207 {
208
209 if ( !xmlPath.exists() )
210 {
211 getLog().warn( "changes.xml file " + xmlPath.getAbsolutePath() + " does not exist." );
212 return;
213 }
214 if ( filteringChanges )
215 {
216 if ( !filteredOutputDirectory.exists() )
217 {
218 filteredOutputDirectory.mkdirs();
219 }
220 XmlStreamReader xmlStreamReader = null;
221 try
222 {
223
224 xmlStreamReader = ReaderFactory.newXmlReader( xmlPath );
225 String encoding = xmlStreamReader.getEncoding();
226 File resultFile = new File( filteredOutputDirectory, "changes.xml" );
227 Date now = new Date();
228 SimpleDateFormat simpleDateFormat =
229 new SimpleDateFormat( publishDateFormat, new Locale( publishDateLocale ) );
230 Properties additionnalProperties = new Properties();
231 additionnalProperties.put( "publishDate", simpleDateFormat.format( now ) );
232 MavenFileFilterRequest mavenFileFilterRequest =
233 new MavenFileFilterRequest( xmlPath, resultFile, true, project, Collections.EMPTY_LIST, false,
234 encoding, session, additionnalProperties );
235 mavenFileFilter.copyFile( mavenFileFilterRequest );
236 xmlPath = resultFile;
237 }
238 catch ( IOException e )
239 {
240 throw new MavenReportException( "Exception during filtering changes file : " + e.getMessage(), e );
241 }
242 catch ( MavenFilteringException e )
243 {
244 throw new MavenReportException( "Exception during filtering changes file : " + e.getMessage(), e );
245 }
246 finally
247 {
248 if ( xmlStreamReader != null )
249 {
250 IOUtil.close( xmlStreamReader );
251 }
252 }
253
254 }
255
256 ChangesReportGenerator report = new ChangesReportGenerator( xmlPath, getLog() );
257
258 report.setIssueLinksPerSystem( issueLinkTemplatePerSystem );
259 report.setIssueLink( issueLinkTemplate );
260
261 report.setUrl( url );
262
263 report.setAddActionDate( addActionDate );
264
265 if ( !report.canGenerateIssueLinks() )
266 {
267 getLog().warn( "No issue management URL defined in POM. Links to your issues will not work correctly." );
268 }
269
270 report.doGenerateReport( getBundle( locale ), getSink() );
271
272
273 copyStaticResources();
274 }
275
276 public String getName( Locale locale )
277 {
278 return getBundle( locale ).getString( "report.changes.name" );
279 }
280
281 public String getDescription( Locale locale )
282 {
283 return getBundle( locale ).getString( "report.changes.description" );
284 }
285
286 public String getOutputName()
287 {
288 return "changes-report";
289 }
290
291 private ResourceBundle getBundle( Locale locale )
292 {
293 return ResourceBundle.getBundle( "changes-report", locale, this.getClass().getClassLoader() );
294 }
295 }