View Javadoc

1   package org.apache.maven.report.projectinfo;
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.util.ArrayList;
23  import java.util.Collections;
24  import java.util.Comparator;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.Locale;
28  
29  import org.apache.maven.artifact.Artifact;
30  import org.apache.maven.artifact.factory.ArtifactFactory;
31  import org.apache.maven.artifact.repository.ArtifactRepository;
32  import org.apache.maven.artifact.versioning.VersionRange;
33  import org.apache.maven.doxia.sink.Sink;
34  import org.apache.maven.model.Plugin;
35  import org.apache.maven.plugin.logging.Log;
36  import org.apache.maven.project.MavenProject;
37  import org.apache.maven.project.MavenProjectBuilder;
38  import org.apache.maven.project.ProjectBuildingException;
39  import org.apache.maven.reporting.AbstractMavenReportRenderer;
40  import org.codehaus.plexus.i18n.I18N;
41  import org.codehaus.plexus.util.StringUtils;
42  
43  /**
44   * Generates the Project Plugin Management report.
45   *
46   * @author Nick Stolwijk
47   * @version $Id: PluginManagementReport.java 728546 2008-12-21 22:56:51Z bentmann $
48   * @since 2.1
49   * @goal plugin-management
50   * @requiresDependencyResolution test
51   */
52  public class PluginManagementReport
53      extends AbstractProjectInfoReport
54  {
55      // ----------------------------------------------------------------------
56      // Mojo components
57      // ----------------------------------------------------------------------
58  
59      /**
60       * Maven Project Builder component.
61       *
62       * @component
63       */
64      private MavenProjectBuilder mavenProjectBuilder;
65  
66      /**
67       * Maven Artifact Factory component.
68       *
69       * @component
70       */
71      private ArtifactFactory artifactFactory;
72  
73      // ----------------------------------------------------------------------
74      // Public methods
75      // ----------------------------------------------------------------------
76  
77      /** {@inheritDoc} */
78      public String getName( Locale locale )
79      {
80          return i18n.getString( "project-info-report", locale, "report.pluginManagement.name" );
81      }
82  
83      /** {@inheritDoc} */
84      public String getDescription( Locale locale )
85      {
86          return i18n.getString( "project-info-report", locale, "report.pluginManagement.description" );
87      }
88  
89      /** {@inheritDoc} */
90      public void executeReport( Locale locale )
91      {
92          PluginManagementRenderer r = new PluginManagementRenderer( getLog(), getSink(), locale, i18n, project
93              .getPluginManagement().getPlugins(), project, mavenProjectBuilder, artifactFactory, localRepository );
94          r.render();
95      }
96  
97      /** {@inheritDoc} */
98      public String getOutputName()
99      {
100         return "plugin-management";
101     }
102 
103     /** {@inheritDoc} */
104     public boolean canGenerateReport()
105     {
106         return project.getPluginManagement() != null && project.getPluginManagement().getPlugins() != null
107             && !project.getPluginManagement().getPlugins().isEmpty();
108     }
109 
110     // ----------------------------------------------------------------------
111     // Private
112     // ----------------------------------------------------------------------
113 
114     /**
115      * Internal renderer class
116      *
117      * @author Nick Stolwijk
118      */
119     protected static class PluginManagementRenderer
120         extends AbstractMavenReportRenderer
121     {
122         private final Log log;
123 
124         private final List pluginManagement;
125 
126         private final Locale locale;
127 
128         private final I18N i18n;
129 
130         private final MavenProject project;
131 
132         private final MavenProjectBuilder mavenProjectBuilder;
133 
134         private final ArtifactFactory artifactFactory;
135 
136         private final ArtifactRepository localRepository;
137 
138         /**
139          * @param log
140          * @param sink
141          * @param locale
142          * @param i18n
143          * @param plugins
144          * @param project
145          * @param mavenProjectBuilder
146          * @param artifactFactory
147          * @param localRepository
148          */
149         public PluginManagementRenderer( Log log, Sink sink, Locale locale, I18N i18n, List plugins,
150                                          MavenProject project, MavenProjectBuilder mavenProjectBuilder,
151                                          ArtifactFactory artifactFactory, ArtifactRepository localRepository )
152         {
153             super( sink );
154 
155             this.log = log;
156 
157             this.locale = locale;
158 
159             this.pluginManagement = plugins;
160 
161             this.i18n = i18n;
162 
163             this.project = project;
164 
165             this.mavenProjectBuilder = mavenProjectBuilder;
166 
167             this.artifactFactory = artifactFactory;
168 
169             this.localRepository = localRepository;
170         }
171 
172 
173         /** {@inheritDoc} */
174         public String getTitle()
175         {
176             return getReportString( "report.pluginManagement.title" );
177         }
178 
179         /** {@inheritDoc} */
180         public void renderBody()
181         {
182             // === Section: Project PluginManagement.
183             renderSectionPluginManagement();
184         }
185 
186         private void renderSectionPluginManagement()
187         {
188             String[] tableHeader = getPluginTableHeader();
189 
190             startSection( getTitle() );
191 
192             // can't use straight artifact comparison because we want optional last
193             Collections.sort( pluginManagement, getPluginComparator() );
194 
195             startTable();
196             tableHeader( tableHeader );
197 
198             for ( Iterator iterator = pluginManagement.iterator(); iterator.hasNext(); )
199             {
200                 Plugin plugin = (Plugin) iterator.next();
201                 VersionRange versionRange;
202                 if ( StringUtils.isEmpty( plugin.getVersion() ) )
203                 {
204                     versionRange = VersionRange.createFromVersion( Artifact.RELEASE_VERSION );
205                 }
206                 else
207                 {
208                     versionRange = VersionRange.createFromVersion( plugin.getVersion() );
209                 }
210 
211                 Artifact pluginArtifact = artifactFactory.createParentArtifact( plugin.getGroupId(), plugin
212                     .getArtifactId(), versionRange.toString() );
213                 List artifactRepositories = project.getPluginArtifactRepositories();
214                 if ( artifactRepositories == null )
215                 {
216                     artifactRepositories = new ArrayList();
217                 }
218                 try
219                 {
220                     MavenProject pluginProject = mavenProjectBuilder.buildFromRepository( pluginArtifact,
221                                                                                           artifactRepositories,
222                                                                                           localRepository );
223                     tableRow( getPluginRow( pluginProject.getGroupId(), pluginProject.getArtifactId(), pluginProject
224                         .getVersion(), pluginProject.getUrl() ) );
225                 }
226                 catch ( ProjectBuildingException e )
227                 {
228                     log.info( "Could not build project for: " + plugin.getArtifactId() + ":" + e.getMessage(), e );
229                     tableRow( getPluginRow( plugin.getGroupId(), plugin.getArtifactId(), plugin.getVersion(), null ) );
230                 }
231 
232             }
233             endTable();
234 
235             endSection();
236         }
237 
238         // ----------------------------------------------------------------------
239         // Private methods
240         // ----------------------------------------------------------------------
241 
242         private String[] getPluginTableHeader()
243         {
244             // reused key...
245             String groupId = getReportString( "report.dependencyManagement.column.groupId" );
246             String artifactId = getReportString( "report.dependencyManagement.column.artifactId" );
247             String version = getReportString( "report.dependencyManagement.column.version" );
248             return new String[] { groupId, artifactId, version };
249         }
250 
251         private String[] getPluginRow( String groupId, String artifactId, String version, String link )
252         {
253             artifactId = ProjectInfoReportUtils.getArtifactIdCell( artifactId, link );
254             return new String[] { groupId, artifactId, version };
255         }
256 
257         private Comparator getPluginComparator()
258         {
259             return new Comparator()
260             {
261                 /** {@inheritDoc} */
262                 public int compare( Object o1, Object o2 )
263                 {
264                     Plugin a1 = (Plugin) o1;
265                     Plugin a2 = (Plugin) o2;
266 
267                     int result = a1.getGroupId().compareTo( a2.getGroupId() );
268                     if ( result == 0 )
269                     {
270                         result = a1.getArtifactId().compareTo( a2.getArtifactId() );
271                     }
272                     return result;
273                 }
274             };
275         }
276 
277         private String getReportString( String key )
278         {
279             return i18n.getString( "project-info-report", locale, key );
280         }
281     }
282 }