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.codehaus.plexus.i18n.I18N;
40 import org.codehaus.plexus.util.StringUtils;
41
42
43
44
45
46
47
48
49
50
51 public class PluginsReport
52 extends AbstractProjectInfoReport
53 {
54
55
56
57
58
59
60
61
62
63 private MavenProjectBuilder mavenProjectBuilder;
64
65
66
67
68
69
70 private ArtifactFactory artifactFactory;
71
72
73
74
75
76
77 public void executeReport( Locale locale )
78 {
79 PluginsRenderer r = new PluginsRenderer( getLog(), getSink(), locale, i18n, project.getPluginArtifacts(),
80 project.getReportArtifacts(), project, mavenProjectBuilder,
81 artifactFactory, localRepository );
82 r.render();
83 }
84
85
86 public String getOutputName()
87 {
88 return "plugins";
89 }
90
91 protected String getI18Nsection()
92 {
93 return "plugins";
94 }
95
96
97 public boolean canGenerateReport()
98 {
99 return ( project.getPluginArtifacts() != null && !project.getPluginArtifacts().isEmpty() )
100 || ( project.getReportArtifacts() != null && !project.getReportArtifacts().isEmpty() );
101 }
102
103
104
105
106
107
108
109
110 protected static class PluginsRenderer
111 extends AbstractProjectInfoRenderer
112 {
113 private final Log log;
114
115 private final List plugins;
116
117 private final List reports;
118
119 private final MavenProject project;
120
121 private final MavenProjectBuilder mavenProjectBuilder;
122
123 private final ArtifactFactory artifactFactory;
124
125 private final ArtifactRepository localRepository;
126
127
128
129
130
131
132
133
134
135
136
137
138
139 public PluginsRenderer( Log log, Sink sink, Locale locale, I18N i18n, Set plugins, Set reports,
140 MavenProject project, MavenProjectBuilder mavenProjectBuilder,
141 ArtifactFactory artifactFactory, ArtifactRepository localRepository )
142 {
143 super( sink, i18n, locale );
144
145 this.log = log;
146
147 this.plugins = new ArrayList( plugins );
148
149 this.reports = new ArrayList( reports );
150
151 this.project = project;
152
153 this.mavenProjectBuilder = mavenProjectBuilder;
154
155 this.artifactFactory = artifactFactory;
156
157 this.localRepository = localRepository;
158 }
159
160 protected String getI18Nsection()
161 {
162 return "plugins";
163 }
164
165
166 public void renderBody()
167 {
168
169 renderSectionPlugins( true );
170
171
172 renderSectionPlugins( false );
173 }
174
175
176
177
178
179 private void renderSectionPlugins( boolean isPlugins )
180 {
181 List list = ( isPlugins ? plugins : reports );
182 String[] tableHeader = getPluginTableHeader();
183
184 startSection( ( isPlugins ? getI18nString( "title" )
185 : getI18nString( "report.title" ) ) );
186
187 if ( list == null || list.isEmpty() )
188 {
189
190 paragraph( ( isPlugins ? getI18nString( "nolist" )
191 : getI18nString( "report.nolist" ) ) );
192
193 endSection();
194
195 return;
196 }
197
198 Collections.sort( list, getArtifactComparator() );
199
200 startTable();
201 tableHeader( tableHeader );
202
203 for ( Iterator iterator = list.iterator(); iterator.hasNext(); )
204 {
205 Artifact artifact = (Artifact) iterator.next();
206
207 VersionRange versionRange;
208 if ( StringUtils.isEmpty( artifact.getVersion() ) )
209 {
210 versionRange = VersionRange.createFromVersion( Artifact.RELEASE_VERSION );
211 }
212 else
213 {
214 versionRange = VersionRange.createFromVersion( artifact.getVersion() );
215 }
216
217 Artifact pluginArtifact = artifactFactory.createParentArtifact( artifact.getGroupId(), artifact
218 .getArtifactId(), versionRange.toString() );
219 List artifactRepositories = project.getPluginArtifactRepositories();
220 if ( artifactRepositories == null )
221 {
222 artifactRepositories = new ArrayList();
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: " + artifact.getArtifactId() + ":" + e.getMessage(), e );
235 tableRow( getPluginRow( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(),
236 null ) );
237 }
238
239 }
240 endTable();
241
242 endSection();
243 }
244
245
246
247
248
249 private String[] getPluginTableHeader()
250 {
251
252 String groupId = getI18nString( "dependencyManagement", "column.groupId" );
253 String artifactId = getI18nString( "dependencyManagement", "column.artifactId" );
254 String version = getI18nString( "dependencyManagement", "column.version" );
255 return new String[] { groupId, artifactId, version };
256 }
257
258 private String[] getPluginRow( String groupId, String artifactId, String version, String link )
259 {
260 artifactId = ProjectInfoReportUtils.getArtifactIdCell( artifactId, link );
261 return new String[] { groupId, artifactId, version };
262 }
263
264 private Comparator getArtifactComparator()
265 {
266 return new Comparator()
267 {
268
269 public int compare( Object o1, Object o2 )
270 {
271 Artifact a1 = (Artifact) o1;
272 Artifact a2 = (Artifact) o2;
273
274 int result = a1.getGroupId().compareTo( a2.getGroupId() );
275 if ( result == 0 )
276 {
277 result = a1.getArtifactId().compareTo( a2.getArtifactId() );
278 }
279 return result;
280 }
281 };
282 }
283 }
284 }