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