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.plugin.logging.Log;
28 import org.apache.maven.plugins.annotations.Component;
29 import org.apache.maven.plugins.annotations.Mojo;
30 import org.apache.maven.plugins.annotations.ResolutionScope;
31 import org.apache.maven.project.MavenProject;
32 import org.apache.maven.project.MavenProjectBuilder;
33 import org.apache.maven.project.ProjectBuildingException;
34 import org.codehaus.plexus.i18n.I18N;
35 import org.codehaus.plexus.util.StringUtils;
36
37 import java.util.ArrayList;
38 import java.util.Collections;
39 import java.util.Comparator;
40 import java.util.List;
41 import java.util.Locale;
42 import java.util.Set;
43
44
45
46
47
48
49
50
51 @Mojo( name = "plugins", requiresDependencyResolution = ResolutionScope.TEST )
52 public class PluginsReport
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 @SuppressWarnings( "unchecked" )
79 PluginsRenderer r =
80 new PluginsRenderer( getLog(), getSink(), locale, getI18N( locale ), project.getPluginArtifacts(),
81 project.getReportArtifacts(), project, mavenProjectBuilder, artifactFactory,
82 localRepository );
83 r.render();
84 }
85
86
87 public String getOutputName()
88 {
89 return "plugins";
90 }
91
92 @Override
93 protected String getI18Nsection()
94 {
95 return "plugins";
96 }
97
98 @Override
99 public boolean canGenerateReport()
100 {
101 return ( project.getPluginArtifacts() != null && !project.getPluginArtifacts().isEmpty() )
102 || ( project.getReportArtifacts() != null && !project.getReportArtifacts().isEmpty() );
103 }
104
105
106
107
108
109
110
111
112 protected static class PluginsRenderer
113 extends AbstractProjectInfoRenderer
114 {
115 private final Log log;
116
117 private final List<Artifact> plugins;
118
119 private final List<Artifact> reports;
120
121 private final MavenProject project;
122
123 private final MavenProjectBuilder mavenProjectBuilder;
124
125 private final ArtifactFactory artifactFactory;
126
127 private final ArtifactRepository localRepository;
128
129
130
131
132
133
134
135
136
137
138
139
140
141 public PluginsRenderer( Log log, Sink sink, Locale locale, I18N i18n, Set<Artifact> plugins,
142 Set<Artifact> reports, MavenProject project, MavenProjectBuilder mavenProjectBuilder,
143 ArtifactFactory artifactFactory, ArtifactRepository localRepository )
144 {
145 super( sink, i18n, locale );
146
147 this.log = log;
148
149 this.plugins = new ArrayList<Artifact>( plugins );
150
151 this.reports = new ArrayList<Artifact>( reports );
152
153 this.project = project;
154
155 this.mavenProjectBuilder = mavenProjectBuilder;
156
157 this.artifactFactory = artifactFactory;
158
159 this.localRepository = localRepository;
160 }
161
162 @Override
163 protected String getI18Nsection()
164 {
165 return "plugins";
166 }
167
168 @Override
169 public void renderBody()
170 {
171
172 renderSectionPlugins( true );
173
174
175 renderSectionPlugins( false );
176 }
177
178
179
180
181
182 private void renderSectionPlugins( boolean isPlugins )
183 {
184 List<Artifact> list = ( isPlugins ? plugins : reports );
185 String[] tableHeader = getPluginTableHeader();
186
187 startSection( ( isPlugins ? getI18nString( "title" )
188 : getI18nString( "report.title" ) ) );
189
190 if ( list == null || list.isEmpty() )
191 {
192
193 paragraph( ( isPlugins ? getI18nString( "nolist" )
194 : getI18nString( "report.nolist" ) ) );
195
196 endSection();
197
198 return;
199 }
200
201 Collections.sort( list, getArtifactComparator() );
202
203 startTable();
204 tableHeader( tableHeader );
205
206 for ( Artifact artifact : list )
207 {
208 VersionRange versionRange;
209 if ( StringUtils.isEmpty( artifact.getVersion() ) )
210 {
211 versionRange = VersionRange.createFromVersion( Artifact.RELEASE_VERSION );
212 }
213 else
214 {
215 versionRange = VersionRange.createFromVersion( artifact.getVersion() );
216 }
217
218 Artifact pluginArtifact = artifactFactory.createParentArtifact( artifact.getGroupId(), artifact
219 .getArtifactId(), versionRange.toString() );
220 @SuppressWarnings( "unchecked" )
221 List<ArtifactRepository> artifactRepositories = project.getPluginArtifactRepositories();
222 if ( artifactRepositories == null )
223 {
224 artifactRepositories = new ArrayList<ArtifactRepository>();
225 }
226 try
227 {
228 MavenProject pluginProject = mavenProjectBuilder.buildFromRepository( pluginArtifact,
229 artifactRepositories,
230 localRepository );
231 tableRow( getPluginRow( pluginProject.getGroupId(), pluginProject.getArtifactId(), pluginProject
232 .getVersion(), pluginProject.getUrl() ) );
233 }
234 catch ( ProjectBuildingException e )
235 {
236 log.info( "Could not build project for: " + artifact.getArtifactId() + ":" + e.getMessage(), e );
237 tableRow( getPluginRow( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(),
238 null ) );
239 }
240
241 }
242 endTable();
243
244 endSection();
245 }
246
247
248
249
250
251 private String[] getPluginTableHeader()
252 {
253
254 String groupId = getI18nString( "dependencyManagement", "column.groupId" );
255 String artifactId = getI18nString( "dependencyManagement", "column.artifactId" );
256 String version = getI18nString( "dependencyManagement", "column.version" );
257 return new String[] { groupId, artifactId, version };
258 }
259
260 private String[] getPluginRow( String groupId, String artifactId, String version, String link )
261 {
262 artifactId = ProjectInfoReportUtils.getArtifactIdCell( artifactId, link );
263 return new String[] { groupId, artifactId, version };
264 }
265
266 private Comparator<Artifact> getArtifactComparator()
267 {
268 return new Comparator<Artifact>()
269 {
270
271 public int compare( Artifact a1, Artifact a2 )
272 {
273 int result = a1.getGroupId().compareTo( a2.getGroupId() );
274 if ( result == 0 )
275 {
276 result = a1.getArtifactId().compareTo( a2.getArtifactId() );
277 }
278 return result;
279 }
280 };
281 }
282 }
283 }