View Javadoc
1   package org.apache.maven.plugin.changes;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.FileWriter;
24  import java.io.IOException;
25  import java.io.Writer;
26  
27  import java.net.URL;
28  
29  import java.text.SimpleDateFormat;
30  
31  import java.util.Collections;
32  import java.util.Date;
33  import java.util.List;
34  import java.util.Locale;
35  import java.util.Map;
36  import java.util.Properties;
37  import java.util.ResourceBundle;
38  
39  import org.apache.commons.collections.map.CaseInsensitiveMap;
40  import org.apache.maven.execution.MavenSession;
41  import org.apache.maven.plugins.annotations.Component;
42  import org.apache.maven.plugins.annotations.Mojo;
43  import org.apache.maven.plugins.annotations.Parameter;
44  import org.apache.maven.project.MavenProject;
45  import org.apache.maven.reporting.MavenReportException;
46  import org.apache.maven.shared.filtering.MavenFileFilter;
47  import org.apache.maven.shared.filtering.MavenFileFilterRequest;
48  import org.apache.maven.shared.filtering.MavenFilteringException;
49  
50  import org.codehaus.plexus.util.FileUtils;
51  import org.codehaus.plexus.util.IOUtil;
52  import org.codehaus.plexus.util.StringUtils;
53  import org.apache.commons.io.input.XmlStreamReader;
54  
55  /**
56   * Goal which creates a nicely formatted Changes Report in html format from a changes.xml file.
57   *
58   * @author <a href="mailto:jruiz@exist.com">Johnny R. Ruiz III</a>
59   * @version $Id: ChangesMojo.java 1586409 2014-04-10 18:48:13Z dennisl $
60   */
61  @Mojo( name = "changes-report", threadSafe = true )
62  public class ChangesMojo
63      extends AbstractChangesReport
64  {
65      /**
66       * A flag whether the report should also include changes from child modules. If set to <code>false</code>, only
67       * the changes from current project will be written to the report.
68       *
69       * @since 2.5
70       */
71      @Parameter( defaultValue = "false" )
72      private boolean aggregated;
73  
74      /**
75       * A flag whether the report should also include the dates of individual actions. If set to <code>false</code>, only
76       * the dates of releases will be written to the report.
77       *
78       * @since 2.1
79       */
80      @Parameter( property = "changes.addActionDate", defaultValue = "false" )
81      private boolean addActionDate;
82  
83      /**
84       * Whether HTML code within an action should be escaped. By changing this to
85       * <code>false</code> you can restore the behavior that was in version 2.2
86       * of this plugin, allowing you to use HTML code to format the content of an
87       * action.
88       * <p>
89       * <strong>Note:</strong> If you use HTML code in an action you need to
90       * place it inside a CDATA section.
91       * </p>
92       * <strong>Note:</strong> Putting any kind of markup inside a CDATA section
93       * might mess up the Changes Report or other generated documents, such as
94       * PDFs, that are based on your <code>changes.xml</code> file if you are not
95       * careful.
96       *
97       * @since 2.4
98       * @deprecated using markup inside CDATA sections does not work for all output formats!
99       */
100     @Parameter( defaultValue = "true" )
101     private boolean escapeHTML;
102 
103     /**
104      * The directory for interpolated changes.xml.
105      *
106      * @since 2.2
107      */
108     @Parameter( defaultValue = "${project.build.directory}/changes", required = true, readonly = true )
109     private File filteredOutputDirectory;
110 
111     /**
112      * applying filtering filtering "a la" resources plugin
113      *
114      * @since 2.2
115      */
116     @Parameter( defaultValue = "false" )
117     private boolean filteringChanges;
118 
119     /**
120      * Template string that is used to discover the URL to use to display an issue report.
121      * There are 2 template tokens you can use. <code>%URL%</code>: this is computed by getting the
122      * <code>&lt;issueManagement&gt;/&lt;url&gt;</code> value from the POM, and removing the last '/'
123      * and everything that comes after it. <code>%ISSUE%</code>: this is the issue number.
124      * <p>
125      * <strong>Note:</strong> In versions of this plugin prior to 2.0-beta-2 this parameter was called
126      * <code>link_template</code>.
127      * </p>
128      *
129      * @since 2.0-beta-2
130      * @deprecated As of 2.1 use issueLinkTemplatePerSystem : this one will be with system default
131      */
132     @Parameter( property = "changes.issueLinkTemplate", defaultValue = "%URL%/ViewIssue.jspa?key=%ISSUE%" )
133     private String issueLinkTemplate;
134 
135     /**
136      * Template strings per system that is used to discover the URL to use to display an issue report. Each key in this
137      * map denotes the (case-insensitive) identifier of the issue tracking system and its value gives the URL template.
138      * <p>
139      * There are 2 template tokens you can use. <code>%URL%</code>: this is computed by getting the
140      * <code>&lt;issueManagement&gt;/&lt;url&gt;</code> value from the POM, and removing the last '/'
141      * and everything that comes after it. <code>%ISSUE%</code>: this is the issue number.
142      * </p>
143      * <p>
144      * <strong>Note:</strong> The deprecated issueLinkTemplate will be used for a system called "default".
145      * </p>
146      * <p>
147      * <strong>Note:</strong> Starting with version 2.4 you usually don't need
148      * to specify this, unless you need to link to an issue management system in
149      * your Changes report that isn't supported out of the box. See the
150      * <a href="./usage.html">Usage page</a> for more
151      * information.
152      * </p>
153      *
154      * @since 2.1
155      */
156     @Parameter
157     private Map issueLinkTemplatePerSystem;
158 
159     /**
160      * @since 2.2
161      */
162     @Component
163     private MavenFileFilter mavenFileFilter;
164 
165     /**
166      * Format to use for publishDate. The value will be available with the following expression ${publishDate}
167      *
168      * @see java.text.SimpleDateFormat
169      * @since 2.2
170      */
171     @Parameter( defaultValue = "yyyy-MM-dd" )
172     private String publishDateFormat;
173 
174     /**
175      * Locale to use for publishDate when formatting
176      *
177      * @see java.util.Locale
178      * @since 2.2
179      */
180     @Parameter( defaultValue = "en" )
181     private String publishDateLocale;
182 
183     /**
184      * @since 2.2
185      */
186     @Component
187     protected MavenSession session;
188 
189     /**
190      * @since 2.4
191      */
192     @Parameter( defaultValue = "${project.issueManagement.system}", readonly = true )
193     private String system;
194 
195     /**
196      * The URI of a file containing all the team members. If this is set to the
197      * special value "none", no links will be generated for the team members.
198      *
199      * @since 2.4
200      */
201     @Parameter( defaultValue = "team-list.html" )
202     private String teamlist;
203 
204     /**
205      */
206     @Parameter( defaultValue = "${project.issueManagement.url}", readonly = true )
207     private String url;
208 
209     /**
210      * The type of the feed to generate.
211      *
212      * <p>
213      * Supported values are:
214      * <code>"rss_0.9", "rss_0.91N" (RSS 0.91 Netscape), "rss_0.91U" (RSS 0.91 Userland),
215      * "rss_0.92", "rss_0.93", "rss_0.94", "rss_1.0", "rss_2.0", "atom_0.3", "atom_1.0"</code>.
216      * </p>
217      *
218      * <p>If not specified, no feed is generated.</p>
219      *
220      * @since 2.9
221      */
222     @Parameter
223     private String feedType;
224 
225     /**
226      * The path of the <code>changes.xml</code> file that will be converted into an HTML report.
227      */
228     @Parameter( property = "changes.xmlPath", defaultValue = "src/changes/changes.xml" )
229     private File xmlPath;
230 
231     private ReleaseUtils releaseUtils = new ReleaseUtils( getLog() );
232 
233     private CaseInsensitiveMap caseInsensitiveIssueLinkTemplatePerSystem;
234 
235     /* --------------------------------------------------------------------- */
236     /* Public methods                                                        */
237     /* --------------------------------------------------------------------- */
238 
239     public boolean canGenerateReport()
240     {
241         // Run only at the execution root
242         if ( runOnlyAtExecutionRoot && !isThisTheExecutionRoot() )
243         {
244             getLog().info( "Skipping the Changes Report in this project because it's not the Execution Root" );
245             return false;
246         }
247         return xmlPath.isFile();
248     }
249 
250     public void executeReport( Locale locale )
251         throws MavenReportException
252     {
253         Date now = new Date();
254         SimpleDateFormat simpleDateFormat =
255                 new SimpleDateFormat( publishDateFormat, new Locale( publishDateLocale ) );
256         Properties additionalProperties = new Properties();
257         additionalProperties.put( "publishDate", simpleDateFormat.format( now ) );
258 
259         ChangesXML changesXml = getChangesFromFile( xmlPath, project, additionalProperties );
260         if ( changesXml == null )
261         {
262             return;
263         }
264 
265         if ( aggregated )
266         {
267             final String basePath = project.getBasedir().getAbsolutePath();
268             final String absolutePath = xmlPath.getAbsolutePath();
269             if ( !absolutePath.startsWith( basePath ) )
270             {
271                 getLog().warn( "xmlPath should be within the project dir for aggregated changes report." );
272                 return;
273             }
274             final String relativePath = absolutePath.substring( basePath.length() );
275 
276             List releaseList = changesXml.getReleaseList();
277             for ( Object o : project.getCollectedProjects() )
278             {
279                 final MavenProject childProject = (MavenProject) o;
280                 final File changesFile = new File( childProject.getBasedir(), relativePath );
281                 final ChangesXML childXml = getChangesFromFile( changesFile, childProject, additionalProperties );
282                 if ( childXml != null )
283                 {
284                     releaseList = releaseUtils.mergeReleases( releaseList, childProject.getName(),
285                                                               childXml.getReleaseList() );
286                 }
287             }
288             changesXml.setReleaseList( releaseList );
289         }
290 
291         ChangesReportGenerator report = new ChangesReportGenerator( changesXml.getReleaseList() );
292 
293         report.setAuthor( changesXml.getAuthor() );
294         report.setTitle( changesXml.getTitle() );
295 
296         report.setEscapeHTML ( escapeHTML );
297 
298         // Create a case insensitive version of issueLinkTemplatePerSystem
299         // We need something case insensitive to maintain backward compatibility
300         if ( issueLinkTemplatePerSystem == null )
301         {
302             caseInsensitiveIssueLinkTemplatePerSystem = new CaseInsensitiveMap();
303         }
304         else
305         {
306             caseInsensitiveIssueLinkTemplatePerSystem = new CaseInsensitiveMap( issueLinkTemplatePerSystem );
307         }
308 
309         // Set good default values for issue management systems here, but only
310         // if they have not been configured already by the user
311         addIssueLinkTemplate( ChangesReportGenerator.DEFAULT_ISSUE_SYSTEM_KEY, issueLinkTemplate );
312         addIssueLinkTemplate( "Bitbucket", "%URL%/issue/%ISSUE%" );
313         addIssueLinkTemplate( "Bugzilla", "%URL%/show_bug.cgi?id=%ISSUE%" );
314         addIssueLinkTemplate( "GitHub", "%URL%/%ISSUE%" );
315         addIssueLinkTemplate( "GoogleCode", "%URL%/detail?id=%ISSUE%" );
316         addIssueLinkTemplate( "JIRA", "%URL%/%ISSUE%" );
317         addIssueLinkTemplate( "Mantis", "%URL%/view.php?id=%ISSUE%" );
318         addIssueLinkTemplate( "MKS", "%URL%/viewissue?selection=%ISSUE%" );
319         addIssueLinkTemplate( "Redmine", "%URL%/issues/show/%ISSUE%" );
320         addIssueLinkTemplate( "Scarab", "%URL%/issues/id/%ISSUE%" );
321         addIssueLinkTemplate( "SourceForge", "http://sourceforge.net/support/tracker.php?aid=%ISSUE%" );
322         addIssueLinkTemplate( "SourceForge2", "%URL%/%ISSUE%" );
323         addIssueLinkTemplate( "Trac", "%URL%/ticket/%ISSUE%" );
324         addIssueLinkTemplate( "Trackplus", "%URL%/printItem.action?key=%ISSUE%" );
325         addIssueLinkTemplate( "YouTrack", "%URL%/issue/%ISSUE%" );
326         // @todo Add more issue management systems here
327         // Remember to also add documentation in usage.apt.vm
328 
329         // Show the current issueLinkTemplatePerSystem configuration
330         logIssueLinkTemplatePerSystem( caseInsensitiveIssueLinkTemplatePerSystem );
331 
332         report.setIssueLinksPerSystem( caseInsensitiveIssueLinkTemplatePerSystem );
333 
334         report.setSystem( system );
335 
336         report.setTeamlist ( teamlist );
337 
338         report.setUrl( url );
339 
340         report.setAddActionDate( addActionDate );
341 
342         if ( StringUtils.isEmpty( url ) )
343         {
344             getLog().warn( "No issue management URL defined in POM. Links to your issues will not work correctly." );
345         }
346 
347         boolean feedGenerated = false;
348 
349         if ( StringUtils.isNotEmpty( feedType ) )
350         {
351             feedGenerated = generateFeed( changesXml, locale );
352         }
353 
354         report.setLinkToFeed( feedGenerated );
355 
356         report.doGenerateReport( getBundle( locale ), getSink() );
357 
358         // Copy the images
359         copyStaticResources();
360     }
361 
362     public String getDescription( Locale locale )
363     {
364         return getBundle( locale ).getString( "report.issues.description" );
365     }
366 
367     public String getName( Locale locale )
368     {
369         return getBundle( locale ).getString( "report.issues.name" );
370     }
371 
372     public String getOutputName()
373     {
374         return "changes-report";
375     }
376 
377     /* --------------------------------------------------------------------- */
378     /* Private methods                                                       */
379     /* --------------------------------------------------------------------- */
380 
381     /**
382      * Parses specified changes.xml file. It also makes filtering if needed. If specified file doesn't exist
383      * it will log warning and return <code>null</code>.
384      *
385      * @param changesXml changes xml file to parse
386      * @param project maven project to parse changes for
387      * @param additionalProperties additional properties used for filtering
388      * @return parsed <code>ChangesXML</code> instance or null if file doesn't exist
389      * @throws MavenReportException if any errors occurs while parsing
390      */
391     private ChangesXML getChangesFromFile( File changesXml, MavenProject project, Properties additionalProperties )
392         throws MavenReportException
393     {
394         if ( !changesXml.exists() )
395         {
396             getLog().warn( "changes.xml file " + changesXml.getAbsolutePath() + " does not exist." );
397             return null;
398         }
399 
400         if ( filteringChanges )
401         {
402             if ( !filteredOutputDirectory.exists() )
403             {
404                 filteredOutputDirectory.mkdirs();
405             }
406             XmlStreamReader xmlStreamReader = null;
407             try
408             {
409                 // so we get encoding from the file itself
410                 xmlStreamReader = new XmlStreamReader( changesXml );
411                 String encoding = xmlStreamReader.getEncoding();
412                 File resultFile = new File( filteredOutputDirectory,
413                                             project.getGroupId() + "." + project.getArtifactId() + "-changes.xml" );
414 
415                 final MavenFileFilterRequest mavenFileFilterRequest =
416                         new MavenFileFilterRequest( changesXml, resultFile, true, project, Collections.EMPTY_LIST,
417                                                     false, encoding, session, additionalProperties );
418                 mavenFileFilter.copyFile( mavenFileFilterRequest );
419                 changesXml = resultFile;
420             }
421             catch ( IOException e )
422             {
423                 throw new MavenReportException( "Exception during filtering changes file : " + e.getMessage(), e );
424             }
425             catch ( MavenFilteringException e )
426             {
427                 throw new MavenReportException( "Exception during filtering changes file : " + e.getMessage(), e );
428             }
429             finally
430             {
431                 if ( xmlStreamReader != null )
432                 {
433                     IOUtil.close( xmlStreamReader );
434                 }
435             }
436 
437         }
438         return new ChangesXML( changesXml, getLog() );
439     }
440 
441     /**
442      * Add the issue link template for the given issue management system,
443      * but only if it has not already been configured.
444      *
445      * @param system The issue management system
446      * @param issueLinkTemplate The issue link template to use
447      * @since 2.4
448      */
449     private void addIssueLinkTemplate( String system, String issueLinkTemplate )
450     {
451         if ( caseInsensitiveIssueLinkTemplatePerSystem == null )
452         {
453             caseInsensitiveIssueLinkTemplatePerSystem = new CaseInsensitiveMap();
454         }
455         if ( !caseInsensitiveIssueLinkTemplatePerSystem.containsKey( system ) )
456         {
457             caseInsensitiveIssueLinkTemplatePerSystem.put( system, issueLinkTemplate );
458         }
459     }
460 
461     private void copyStaticResources()
462         throws MavenReportException
463     {
464         final String pluginResourcesBase = "org/apache/maven/plugin/changes";
465         String resourceNames[] = {
466             "images/add.gif",
467             "images/fix.gif",
468             "images/icon_help_sml.gif",
469             "images/remove.gif",
470             "images/rss.png",
471             "images/update.gif" };
472         try
473         {
474             getLog().debug( "Copying static resources." );
475             for ( String resourceName : resourceNames )
476             {
477                 URL url = this.getClass().getClassLoader().getResource( pluginResourcesBase + "/" + resourceName );
478                 FileUtils.copyURLToFile( url, new File( getReportOutputDirectory(), resourceName ) );
479             }
480         }
481         catch ( IOException e )
482         {
483             throw new MavenReportException( "Unable to copy static resources." );
484         }
485     }
486 
487     private ResourceBundle getBundle( Locale locale )
488     {
489         return ResourceBundle.getBundle( "changes-report", locale, this.getClass().getClassLoader() );
490     }
491 
492     protected String getTeamlist()
493     {
494         return teamlist;
495     }
496 
497     private void logIssueLinkTemplatePerSystem( Map issueLinkTemplatePerSystem )
498     {
499         if ( getLog().isDebugEnabled() )
500         {
501             if ( issueLinkTemplatePerSystem == null )
502             {
503                 getLog().debug( "No issueLinkTemplatePerSystem configuration was found" );
504             }
505             else
506             {
507                 for ( Object o : issueLinkTemplatePerSystem.entrySet() )
508                 {
509                     Map.Entry entry = (Map.Entry) o;
510                     getLog().debug( "issueLinkTemplatePerSystem[" + entry.getKey() + "] = " + entry.getValue() );
511                 }
512             }
513         }
514     }
515 
516     private boolean generateFeed( final ChangesXML changesXml, final Locale locale )
517     {
518         getLog().debug( "Generating " + feedType + " feed." );
519 
520         boolean success = true;
521 
522         final FeedGenerator feed = new FeedGenerator( locale );
523         feed.setLink( project.getUrl() + "/changes-report.html" ); // TODO: better way?
524         feed.setTitle( project.getName() + ": " + changesXml.getTitle() );
525         feed.setAuthor( changesXml.getAuthor() );
526         feed.setDateFormat( new SimpleDateFormat( publishDateFormat, new Locale( publishDateLocale ) ) );
527 
528         Writer writer = null;
529 
530         try
531         {
532             writer = new FileWriter( new File( getReportOutputDirectory(), "changes.rss" ) );
533             feed.export( changesXml.getReleaseList(), feedType, writer );
534         }
535         catch ( IOException ex )
536         {
537             success = false;
538             getLog().warn( "Failed to create rss feed: " + ex.getMessage() );
539             getLog().debug( ex );
540         }
541         finally
542         {
543             try
544             {
545                 if ( writer != null )
546                 {
547                     writer.close();
548                 }
549             }
550             catch ( IOException ex )
551             {
552                 getLog().warn( "Failed to close writer: " + ex.getMessage() );
553                 getLog().debug( ex );
554             }
555         }
556 
557         return success;
558     }
559 }