1   package org.apache.maven.report.projectinfo;
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
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  import java.util.Set;
29  
30  import org.apache.maven.artifact.Artifact;
31  import org.apache.maven.artifact.factory.ArtifactFactory;
32  import org.apache.maven.artifact.repository.ArtifactRepository;
33  import org.apache.maven.artifact.versioning.VersionRange;
34  import org.apache.maven.doxia.sink.Sink;
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  
45  
46  
47  
48  
49  
50  
51  
52  public class PluginsReport
53      extends AbstractProjectInfoReport
54  {
55      
56      
57      
58  
59      
60  
61  
62  
63  
64      private MavenProjectBuilder mavenProjectBuilder;
65  
66      
67  
68  
69  
70  
71      private ArtifactFactory artifactFactory;
72  
73      
74      
75      
76  
77      
78      public String getName( Locale locale )
79      {
80          return i18n.getString( "project-info-report", locale, "report.plugins.name" );
81      }
82  
83      
84      public String getDescription( Locale locale )
85      {
86          return i18n.getString( "project-info-report", locale, "report.plugins.description" );
87      }
88  
89      
90      public void executeReport( Locale locale )
91      {
92          PluginsRenderer r = new PluginsRenderer( getLog(), getSink(), locale, i18n, project.getPluginArtifacts(),
93                                                   project.getReportArtifacts(), project, mavenProjectBuilder,
94                                                   artifactFactory, localRepository );
95          r.render();
96      }
97  
98      
99      public String getOutputName()
100     {
101         return "plugins";
102     }
103 
104     
105     public boolean canGenerateReport()
106     {
107         return ( project.getPluginArtifacts() != null && !project.getPluginArtifacts().isEmpty() )
108             || ( project.getReportArtifacts() != null && !project.getReportArtifacts().isEmpty() );
109     }
110 
111     
112     
113     
114 
115     
116 
117 
118     protected static class PluginsRenderer
119         extends AbstractMavenReportRenderer
120     {
121         private final Log log;
122 
123         private final List plugins;
124 
125         private final List reports;
126 
127         private final Locale locale;
128 
129         private final I18N i18n;
130 
131         private final MavenProject project;
132 
133         private final MavenProjectBuilder mavenProjectBuilder;
134 
135         private final ArtifactFactory artifactFactory;
136 
137         private final ArtifactRepository localRepository;
138 
139         
140 
141 
142 
143 
144 
145 
146 
147 
148 
149 
150 
151         public PluginsRenderer( Log log, Sink sink, Locale locale, I18N i18n, Set plugins, Set reports,
152                                 MavenProject project, MavenProjectBuilder mavenProjectBuilder,
153                                 ArtifactFactory artifactFactory, ArtifactRepository localRepository )
154         {
155             super( sink );
156 
157             this.log = log;
158 
159             this.locale = locale;
160 
161             this.plugins = new ArrayList( plugins );
162 
163             this.reports = new ArrayList( reports );
164 
165             this.i18n = i18n;
166 
167             this.project = project;
168 
169             this.mavenProjectBuilder = mavenProjectBuilder;
170 
171             this.artifactFactory = artifactFactory;
172 
173             this.localRepository = localRepository;
174         }
175 
176         
177         public String getTitle()
178         {
179             return getReportString( "report.plugins.title" );
180         }
181 
182         
183         public void renderBody()
184         {
185             
186             renderSectionPlugins( true );
187 
188             
189             renderSectionPlugins( false );
190         }
191 
192         
193 
194 
195 
196         private void renderSectionPlugins( boolean isPlugins )
197         {
198             List list = ( isPlugins ? plugins : reports );
199             String[] tableHeader = getPluginTableHeader();
200 
201             startSection( ( isPlugins ? getReportString( "report.plugins.title" )
202                                      : getReportString( "report.plugins.report.title" ) ) );
203 
204             if ( list == null || list.isEmpty() )
205             {
206 
207                 paragraph(  ( isPlugins ? getReportString( "report.plugins.nolist" )
208                                         : getReportString( "report.plugins.report.nolist" ) ) );
209 
210                 endSection();
211 
212                 return;
213             }
214 
215             Collections.sort( list, getArtifactComparator() );
216 
217             startTable();
218             tableHeader( tableHeader );
219 
220             for ( Iterator iterator = list.iterator(); iterator.hasNext(); )
221             {
222                 Artifact artifact = (Artifact) iterator.next();
223 
224                 VersionRange versionRange;
225                 if ( StringUtils.isEmpty( artifact.getVersion() ) )
226                 {
227                     versionRange = VersionRange.createFromVersion( Artifact.RELEASE_VERSION );
228                 }
229                 else
230                 {
231                     versionRange = VersionRange.createFromVersion( artifact.getVersion() );
232                 }
233 
234                 Artifact pluginArtifact = artifactFactory.createParentArtifact( artifact.getGroupId(), artifact
235                     .getArtifactId(), versionRange.toString() );
236                 List artifactRepositories = project.getPluginArtifactRepositories();
237                 if ( artifactRepositories == null )
238                 {
239                     artifactRepositories = new ArrayList();
240                 }
241                 try
242                 {
243                     MavenProject pluginProject = mavenProjectBuilder.buildFromRepository( pluginArtifact,
244                                                                                           artifactRepositories,
245                                                                                           localRepository );
246                     tableRow( getPluginRow( pluginProject.getGroupId(), pluginProject.getArtifactId(), pluginProject
247                                             .getVersion(), pluginProject.getUrl() ) );
248                 }
249                 catch ( ProjectBuildingException e )
250                 {
251                     log.info( "Could not build project for: " + artifact.getArtifactId() + ":" + e.getMessage(), e );
252                     tableRow( getPluginRow( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(),
253                                             null ) );
254                 }
255 
256             }
257             endTable();
258 
259             endSection();
260         }
261 
262         
263         
264         
265 
266         private String[] getPluginTableHeader()
267         {
268             
269             String groupId = getReportString( "report.dependencyManagement.column.groupId" );
270             String artifactId = getReportString( "report.dependencyManagement.column.artifactId" );
271             String version = getReportString( "report.dependencyManagement.column.version" );
272             return new String[] { groupId, artifactId, version };
273         }
274 
275         private String[] getPluginRow( String groupId, String artifactId, String version, String link )
276         {
277             artifactId = ProjectInfoReportUtils.getArtifactIdCell( artifactId, link );
278             return new String[] { groupId, artifactId, version };
279         }
280 
281         private Comparator getArtifactComparator()
282         {
283             return new Comparator()
284             {
285                 
286                 public int compare( Object o1, Object o2 )
287                 {
288                     Artifact a1 = (Artifact) o1;
289                     Artifact a2 = (Artifact) o2;
290 
291                     int result = a1.getGroupId().compareTo( a2.getGroupId() );
292                     if ( result == 0 )
293                     {
294                         result = a1.getArtifactId().compareTo( a2.getArtifactId() );
295                     }
296                     return result;
297                 }
298             };
299         }
300 
301         private String getReportString( String key )
302         {
303             return i18n.getString( "project-info-report", locale, key );
304         }
305     }
306 }