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
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
45
46
47
48
49
50
51
52 public class PluginManagementReport
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.pluginManagement.name" );
81 }
82
83
84 public String getDescription( Locale locale )
85 {
86 return i18n.getString( "project-info-report", locale, "report.pluginManagement.description" );
87 }
88
89
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
98 public String getOutputName()
99 {
100 return "plugin-management";
101 }
102
103
104 public boolean canGenerateReport()
105 {
106 return project.getPluginManagement() != null && project.getPluginManagement().getPlugins() != null
107 && !project.getPluginManagement().getPlugins().isEmpty();
108 }
109
110
111
112
113
114
115
116
117
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
140
141
142
143
144
145
146
147
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
174 public String getTitle()
175 {
176 return getReportString( "report.pluginManagement.title" );
177 }
178
179
180 public void renderBody()
181 {
182
183 renderSectionPluginManagement();
184 }
185
186 private void renderSectionPluginManagement()
187 {
188 String[] tableHeader = getPluginTableHeader();
189
190 startSection( getTitle() );
191
192
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
240
241
242 private String[] getPluginTableHeader()
243 {
244
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
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 }