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.plugin.logging.Log;
29  import org.apache.maven.plugins.annotations.Component;
30  import org.apache.maven.plugins.annotations.Mojo;
31  import org.apache.maven.plugins.annotations.ResolutionScope;
32  import org.apache.maven.project.MavenProject;
33  import org.apache.maven.project.MavenProjectBuilder;
34  import org.apache.maven.project.ProjectBuildingException;
35  import org.codehaus.plexus.i18n.I18N;
36  import org.codehaus.plexus.util.StringUtils;
37  
38  import java.util.ArrayList;
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  
51  @Mojo( name = "plugin-management", requiresDependencyResolution = ResolutionScope.TEST )
52  public class PluginManagementReport
53      extends AbstractProjectInfoReport
54  {
55      
56      
57      
58  
59      
60  
61  
62      @Component
63      private MavenProjectBuilder mavenProjectBuilder;
64  
65      
66  
67  
68      @Component
69      private ArtifactFactory artifactFactory;
70  
71      
72      
73      
74  
75      @Override
76      public void executeReport( Locale locale )
77      {
78          PluginManagementRenderer r =
79              new PluginManagementRenderer( getLog(), getSink(), locale, getI18N( locale ),
80                                            project.getPluginManagement().getPlugins(), project, mavenProjectBuilder,
81                                            artifactFactory, localRepository );
82          r.render();
83      }
84  
85      
86      public String getOutputName()
87      {
88          return "plugin-management";
89      }
90  
91      @Override
92      protected String getI18Nsection()
93      {
94          return "pluginManagement";
95      }
96  
97      @Override
98      public boolean canGenerateReport()
99      {
100         return project.getPluginManagement() != null && project.getPluginManagement().getPlugins() != null
101             && !project.getPluginManagement().getPlugins().isEmpty();
102     }
103 
104     
105     
106     
107 
108     
109 
110 
111 
112 
113     protected static class PluginManagementRenderer
114         extends AbstractProjectInfoRenderer
115     {
116         private final Log log;
117 
118         private final List<Plugin> pluginManagement;
119 
120         private final MavenProject project;
121 
122         private final MavenProjectBuilder mavenProjectBuilder;
123 
124         private final ArtifactFactory artifactFactory;
125 
126         private final ArtifactRepository localRepository;
127 
128         
129 
130 
131 
132 
133 
134 
135 
136 
137 
138 
139         public PluginManagementRenderer( Log log, Sink sink, Locale locale, I18N i18n, List<Plugin> plugins,
140                                          MavenProject project, MavenProjectBuilder mavenProjectBuilder,
141                                          ArtifactFactory artifactFactory, ArtifactRepository localRepository )
142         {
143             super( sink, i18n, locale );
144 
145             this.log = log;
146 
147             this.pluginManagement = plugins;
148 
149             this.project = project;
150 
151             this.mavenProjectBuilder = mavenProjectBuilder;
152 
153             this.artifactFactory = artifactFactory;
154 
155             this.localRepository = localRepository;
156         }
157 
158         @Override
159         protected String getI18Nsection()
160         {
161             return "pluginManagement";
162         }
163 
164         @Override
165         public void renderBody()
166         {
167             
168             renderSectionPluginManagement();
169         }
170 
171         private void renderSectionPluginManagement()
172         {
173             String[] tableHeader = getPluginTableHeader();
174 
175             startSection( getTitle() );
176 
177             
178             Collections.sort( pluginManagement, getPluginComparator() );
179 
180             startTable();
181             tableHeader( tableHeader );
182 
183             for ( Plugin plugin : pluginManagement )
184             {
185                 VersionRange versionRange;
186                 if ( StringUtils.isEmpty( plugin.getVersion() ) )
187                 {
188                     versionRange = VersionRange.createFromVersion( Artifact.RELEASE_VERSION );
189                 }
190                 else
191                 {
192                     versionRange = VersionRange.createFromVersion( plugin.getVersion() );
193                 }
194 
195                 Artifact pluginArtifact = artifactFactory.createParentArtifact( plugin.getGroupId(), plugin
196                     .getArtifactId(), versionRange.toString() );
197                 @SuppressWarnings( "unchecked" )
198                 List<ArtifactRepository> artifactRepositories = project.getPluginArtifactRepositories();
199                 if ( artifactRepositories == null )
200                 {
201                     artifactRepositories = new ArrayList<ArtifactRepository>();
202                 }
203                 try
204                 {
205                     MavenProject pluginProject = mavenProjectBuilder.buildFromRepository( pluginArtifact,
206                                                                                           artifactRepositories,
207                                                                                           localRepository );
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( "dependencyManagement", "column.groupId" );
231             String artifactId = getI18nString( "dependencyManagement", "column.artifactId" );
232             String version = getI18nString( "dependencyManagement", "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 }