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.List;
26 import java.util.Locale;
27 import java.util.Set;
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.plugin.logging.Log;
35 import org.apache.maven.project.MavenProject;
36 import org.apache.maven.project.MavenProjectBuilder;
37 import org.apache.maven.project.ProjectBuildingException;
38 import org.codehaus.plexus.i18n.I18N;
39 import org.codehaus.plexus.util.StringUtils;
40
41
42
43
44
45
46
47
48
49
50 public class PluginsReport
51 extends AbstractProjectInfoReport
52 {
53
54
55
56
57
58
59
60
61
62 private MavenProjectBuilder mavenProjectBuilder;
63
64
65
66
67
68
69 private ArtifactFactory artifactFactory;
70
71
72
73
74
75 @Override
76 public void executeReport( Locale locale )
77 {
78 @SuppressWarnings( "unchecked" )
79 PluginsRenderer r = new PluginsRenderer( getLog(), getSink(), locale, getI18N( locale ), 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 @Override
92 protected String getI18Nsection()
93 {
94 return "plugins";
95 }
96
97 @Override
98 public boolean canGenerateReport()
99 {
100 return ( project.getPluginArtifacts() != null && !project.getPluginArtifacts().isEmpty() )
101 || ( project.getReportArtifacts() != null && !project.getReportArtifacts().isEmpty() );
102 }
103
104
105
106
107
108
109
110
111 protected static class PluginsRenderer
112 extends AbstractProjectInfoRenderer
113 {
114 private final Log log;
115
116 private final List<Artifact> plugins;
117
118 private final List<Artifact> reports;
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
140 public PluginsRenderer( Log log, Sink sink, Locale locale, I18N i18n, Set<Artifact> plugins,
141 Set<Artifact> reports, MavenProject project, MavenProjectBuilder mavenProjectBuilder,
142 ArtifactFactory artifactFactory, ArtifactRepository localRepository )
143 {
144 super( sink, i18n, locale );
145
146 this.log = log;
147
148 this.plugins = new ArrayList<Artifact>( plugins );
149
150 this.reports = new ArrayList<Artifact>( reports );
151
152 this.project = project;
153
154 this.mavenProjectBuilder = mavenProjectBuilder;
155
156 this.artifactFactory = artifactFactory;
157
158 this.localRepository = localRepository;
159 }
160
161 @Override
162 protected String getI18Nsection()
163 {
164 return "plugins";
165 }
166
167 @Override
168 public void renderBody()
169 {
170
171 renderSectionPlugins( true );
172
173
174 renderSectionPlugins( false );
175 }
176
177
178
179
180
181 private void renderSectionPlugins( boolean isPlugins )
182 {
183 List<Artifact> list = ( isPlugins ? plugins : reports );
184 String[] tableHeader = getPluginTableHeader();
185
186 startSection( ( isPlugins ? getI18nString( "title" )
187 : getI18nString( "report.title" ) ) );
188
189 if ( list == null || list.isEmpty() )
190 {
191
192 paragraph( ( isPlugins ? getI18nString( "nolist" )
193 : getI18nString( "report.nolist" ) ) );
194
195 endSection();
196
197 return;
198 }
199
200 Collections.sort( list, getArtifactComparator() );
201
202 startTable();
203 tableHeader( tableHeader );
204
205 for ( Artifact artifact : list )
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 @SuppressWarnings( "unchecked" )
220 List<ArtifactRepository> artifactRepositories = project.getPluginArtifactRepositories();
221 if ( artifactRepositories == null )
222 {
223 artifactRepositories = new ArrayList<ArtifactRepository>();
224 }
225 try
226 {
227 MavenProject pluginProject = mavenProjectBuilder.buildFromRepository( pluginArtifact,
228 artifactRepositories,
229 localRepository );
230 tableRow( getPluginRow( pluginProject.getGroupId(), pluginProject.getArtifactId(), pluginProject
231 .getVersion(), pluginProject.getUrl() ) );
232 }
233 catch ( ProjectBuildingException e )
234 {
235 log.info( "Could not build project for: " + artifact.getArtifactId() + ":" + e.getMessage(), e );
236 tableRow( getPluginRow( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(),
237 null ) );
238 }
239
240 }
241 endTable();
242
243 endSection();
244 }
245
246
247
248
249
250 private String[] getPluginTableHeader()
251 {
252
253 String groupId = getI18nString( "dependencyManagement", "column.groupId" );
254 String artifactId = getI18nString( "dependencyManagement", "column.artifactId" );
255 String version = getI18nString( "dependencyManagement", "column.version" );
256 return new String[] { groupId, artifactId, version };
257 }
258
259 private String[] getPluginRow( String groupId, String artifactId, String version, String link )
260 {
261 artifactId = ProjectInfoReportUtils.getArtifactIdCell( artifactId, link );
262 return new String[] { groupId, artifactId, version };
263 }
264
265 private Comparator<Artifact> getArtifactComparator()
266 {
267 return new Comparator<Artifact>()
268 {
269
270 public int compare( Artifact a1, Artifact a2 )
271 {
272 int result = a1.getGroupId().compareTo( a2.getGroupId() );
273 if ( result == 0 )
274 {
275 result = a1.getArtifactId().compareTo( a2.getArtifactId() );
276 }
277 return result;
278 }
279 };
280 }
281 }
282 }