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.IOException;
24  import java.net.URL;
25  import java.util.Locale;
26  import java.util.Map;
27  import java.util.ResourceBundle;
28  
29  import org.apache.maven.reporting.MavenReportException;
30  import org.codehaus.plexus.util.FileUtils;
31  
32  /**
33   * Goal which creates a nicely formatted Changes Report in html format from a changes.xml file.
34   *
35   * @goal changes-report
36   * @author <a href="mailto:jruiz@exist.com">Johnny R. Ruiz III</a>
37   * @version $Id: ChangesMojo.html 816588 2012-05-08 12:37:27Z hboutemy $
38   */
39  public class ChangesMojo
40      extends AbstractChangesReport
41  {
42      /**
43       * The path of the <code>changes.xml</code> file that will be converted into an HTML report.
44       *
45       * @parameter expression="${changes.xmlPath}" default-value="src/changes/changes.xml"
46       */
47      private File xmlPath;
48  
49      /**
50       * Template string that is used to discover the URL to use to display an issue report.
51       * There are 2 template tokens you can use. <code>%URL%</code>: this is computed by getting the
52       * <code>&lt;issueManagement&gt;/&lt;url&gt;</code> value from the POM, and removing the last '/'
53       * and everything that comes after it. <code>%ISSUE%</code>: this is the issue number.
54       * <p>
55       * <strong>Note:</strong> In versions of this plugin prior to 2.0-beta-2 this parameter was called
56       * <code>link_template</code>.
57       * </p>
58       *
59       * @parameter expression="${changes.issueLinkTemplate}" default-value="%URL%/ViewIssue.jspa?key=%ISSUE%"
60       * @since 2.0-beta-2
61       * @deprecated As of 2.1 use issueLinkTemplatePerSystem : this one will be with system default
62       */
63      private String issueLinkTemplate;
64      
65      /**
66       * Template strings per system that is used to discover the URL to use to display an issue report. Each key in this
67       * map denotes the (case-sensitive) identifier of the issue tracking system and its value gives the URL template.
68       * <p>
69       * There are 2 template tokens you can use. <code>%URL%</code>: this is computed by getting the
70       * <code>&lt;issueManagement&gt;/&lt;url&gt;</code> value from the POM, and removing the last '/'
71       * and everything that comes after it. <code>%ISSUE%</code>: this is the issue number.
72       * </p>
73       * <p>
74       * <strong>Note:</strong> The deprecated issueLinkTemplate will be used for a system called "default".
75       * </p>
76       *
77       * @parameter
78       * @since 2.1
79       */    
80      private Map issueLinkTemplatePerSystem;
81  
82      /**
83       * @parameter default-value="${project.issueManagement.url}"
84       * @readonly
85       */
86      private String url;
87  
88      /**
89       * A flag whether the report should also include the dates of individual actions. If set to <code>false</code>, only
90       * the dates of releases will be written to the report.
91       * 
92       * @parameter expression="${changes.addActionDate}" default-value="false"
93       * @since 2.1
94       */        
95      private boolean addActionDate;    
96      
97      public boolean canGenerateReport()
98      {
99          return xmlPath.isFile();
100     }
101 
102     private void copyStaticResources()
103         throws MavenReportException
104     {
105         final String pluginResourcesBase = "org/apache/maven/plugin/changes";
106         String resourceNames[] = {
107             "images/add.gif",
108             "images/fix.gif",
109             "images/icon_help_sml.gif",
110             "images/remove.gif",
111             "images/rss.png",
112             "images/update.gif" };
113         try
114         {
115             getLog().debug( "Copying static resources." );
116             for ( int i = 0; i < resourceNames.length; i++ )
117             {
118                 URL url = this.getClass().getClassLoader().getResource( pluginResourcesBase + "/" + resourceNames[i] );
119                 FileUtils.copyURLToFile( url, new File( getReportOutputDirectory(), resourceNames[i] ) );
120             }
121         }
122         catch ( IOException e )
123         {
124             throw new MavenReportException( "Unable to copy static resources." );
125         }
126     }
127 
128     public void executeReport( Locale locale )
129         throws MavenReportException
130     {
131         
132         if ( !xmlPath.exists() )
133         {
134             getLog().warn( "changes.xml file " + xmlPath.getAbsolutePath() + " does not exist." );
135             return;
136         }
137         
138         ChangesReportGenerator report = new ChangesReportGenerator( xmlPath, getLog() );
139         
140         report.setIssueLinksPerSystem( issueLinkTemplatePerSystem ); 
141         report.setIssueLink( issueLinkTemplate );
142         
143         report.setUrl( url );
144 
145         report.setAddActionDate( addActionDate );
146         
147         if ( !report.canGenerateIssueLinks() )
148         {
149             getLog().warn( "No issue management URL defined in POM. Links to your issues will not work correctly." );
150         }
151 
152         report.doGenerateReport( getBundle( locale ), getSink() );
153 
154         // Copy the images
155         copyStaticResources();
156     }
157 
158     public String getName( Locale locale )
159     {
160         return getBundle( locale ).getString( "report.changes.name" );
161     }
162 
163     public String getDescription( Locale locale )
164     {
165         return getBundle( locale ).getString( "report.changes.description" );
166     }
167 
168     public String getOutputName()
169     {
170         return "changes-report";
171     }
172 
173     private ResourceBundle getBundle( Locale locale )
174     {
175         return ResourceBundle.getBundle( "changes-report", locale, this.getClass().getClassLoader() );
176     }
177 }