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