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.versioning.VersionRange;
24 import org.apache.maven.doxia.sink.Sink;
25 import org.apache.maven.model.Plugin;
26 import org.apache.maven.model.PluginManagement;
27 import org.apache.maven.plugin.logging.Log;
28 import org.apache.maven.plugins.annotations.Mojo;
29 import org.apache.maven.plugins.annotations.Parameter;
30 import org.apache.maven.plugins.annotations.ResolutionScope;
31 import org.apache.maven.project.DefaultProjectBuildingRequest;
32 import org.apache.maven.project.MavenProject;
33 import org.apache.maven.project.ProjectBuilder;
34 import org.apache.maven.project.ProjectBuildingException;
35 import org.apache.maven.project.ProjectBuildingRequest;
36 import org.apache.maven.repository.RepositorySystem;
37 import org.apache.maven.shared.artifact.filter.PatternExcludesArtifactFilter;
38 import org.codehaus.plexus.i18n.I18N;
39 import org.codehaus.plexus.util.StringUtils;
40
41 import java.util.Collections;
42 import java.util.Comparator;
43 import java.util.List;
44 import java.util.Locale;
45
46
47
48
49
50
51
52 @Mojo( name = "plugin-management", requiresDependencyResolution = ResolutionScope.TEST )
53 public class PluginManagementReport
54 extends AbstractProjectInfoReport
55 {
56
57
58
59
60
61
62
63
64 @Parameter
65 private List<String> pluginManagementExcludes = null;
66
67
68
69
70
71 @Override
72 public void executeReport( Locale locale )
73 {
74 PluginManagementRenderer r =
75 new PluginManagementRenderer( getLog(), getSink(), locale, getI18N( locale ),
76 project.getPluginManagement().getPlugins(), project, projectBuilder,
77 repositorySystem, getSession().getProjectBuildingRequest(),
78 pluginManagementExcludes );
79 r.render();
80 }
81
82
83 public String getOutputName()
84 {
85 return "plugin-management";
86 }
87
88 @Override
89 protected String getI18Nsection()
90 {
91 return "plugin-management";
92 }
93
94 @Override
95 public boolean canGenerateReport()
96 {
97 boolean result = super.canGenerateReport();
98 if ( result && skipEmptyReport )
99 {
100 result = getProject().getPluginManagement() != null
101 && !isEmpty( project.getPluginManagement().getPlugins() );
102 }
103
104 return result;
105 }
106
107
108
109
110
111
112
113
114
115
116 protected static class PluginManagementRenderer
117 extends AbstractProjectInfoRenderer
118 {
119
120 private final Log log;
121
122 private final List<Plugin> pluginManagement;
123
124 private final MavenProject project;
125
126 private final ProjectBuilder projectBuilder;
127
128 private final RepositorySystem repositorySystem;
129
130 private final ProjectBuildingRequest buildingRequest;
131
132 private final PatternExcludesArtifactFilter patternExcludesArtifactFilter;
133
134
135
136
137
138
139
140
141
142
143
144
145
146 public PluginManagementRenderer( Log log, Sink sink, Locale locale, I18N i18n, List<Plugin> plugins,
147 MavenProject project, ProjectBuilder projectBuilder,
148 RepositorySystem repositorySystem, ProjectBuildingRequest buildingRequest,
149 List<String> excludes )
150 {
151 super( sink, i18n, locale );
152
153 this.log = log;
154
155 this.pluginManagement = plugins;
156
157 this.project = project;
158
159 this.projectBuilder = projectBuilder;
160
161 this.repositorySystem = repositorySystem;
162
163 this.buildingRequest = buildingRequest;
164
165 this.patternExcludesArtifactFilter = new PatternExcludesArtifactFilter( excludes );
166 }
167
168 @Override
169 protected String getI18Nsection()
170 {
171 return "plugin-management";
172 }
173
174 @Override
175 public void renderBody()
176 {
177 PluginManagement projectPluginManagement = project.getPluginManagement();
178
179 if ( projectPluginManagement == null || projectPluginManagement.getPlugins() == null
180 || projectPluginManagement.getPlugins().isEmpty() )
181 {
182 startSection( getTitle() );
183
184 paragraph( getI18nString( "nolist" ) );
185
186 endSection();
187
188 return;
189 }
190
191
192 renderSectionPluginManagement();
193 }
194
195 private void renderSectionPluginManagement()
196 {
197 String[] tableHeader = getPluginTableHeader();
198
199 startSection( getTitle() );
200
201
202 Collections.sort( pluginManagement, getPluginComparator() );
203
204 startTable();
205 tableHeader( tableHeader );
206
207 ProjectBuildingRequest buildRequest = new DefaultProjectBuildingRequest( buildingRequest );
208 buildRequest.setRemoteRepositories( project.getPluginArtifactRepositories() );
209
210 for ( Plugin plugin : pluginManagement )
211 {
212 VersionRange versionRange;
213 if ( StringUtils.isEmpty( plugin.getVersion() ) )
214 {
215 versionRange = VersionRange.createFromVersion( Artifact.RELEASE_VERSION );
216 }
217 else
218 {
219 versionRange = VersionRange.createFromVersion( plugin.getVersion() );
220 }
221
222 Artifact pluginArtifact = repositorySystem.createProjectArtifact( plugin.getGroupId(), plugin
223 .getArtifactId(), versionRange.toString() );
224
225 if ( patternExcludesArtifactFilter.include( pluginArtifact ) )
226 {
227 try
228 {
229 MavenProject pluginProject =
230 projectBuilder.build( pluginArtifact, buildingRequest ).getProject();
231
232 tableRow( getPluginRow( pluginProject.getGroupId(), pluginProject.getArtifactId(),
233 pluginProject.getVersion(), pluginProject.getUrl() ) );
234 }
235 catch ( ProjectBuildingException e )
236 {
237 log.info( "Could not build project for: " + plugin.getArtifactId() + ":" + e.getMessage(), e );
238 tableRow( getPluginRow( plugin.getGroupId(), plugin.getArtifactId(), plugin.getVersion(),
239 null ) );
240 }
241 }
242 else
243 {
244 log.debug( "Excluding plugin " + pluginArtifact.getId() + " from report" );
245 }
246 }
247 endTable();
248
249 endSection();
250 }
251
252
253
254
255
256 private String[] getPluginTableHeader()
257 {
258
259 String groupId = getI18nString( "dependency-management", "column.groupId" );
260 String artifactId = getI18nString( "dependency-management", "column.artifactId" );
261 String version = getI18nString( "dependency-management", "column.version" );
262 return new String[] { groupId, artifactId, version };
263 }
264
265 private String[] getPluginRow( String groupId, String artifactId, String version, String link )
266 {
267 artifactId = ProjectInfoReportUtils.getArtifactIdCell( artifactId, link );
268 return new String[] { groupId, artifactId, version };
269 }
270
271 private Comparator<Plugin> getPluginComparator()
272 {
273 return new Comparator<Plugin>()
274 {
275
276 public int compare( Plugin a1, Plugin a2 )
277 {
278 int result = a1.getGroupId().compareTo( a2.getGroupId() );
279 if ( result == 0 )
280 {
281 result = a1.getArtifactId().compareTo( a2.getArtifactId() );
282 }
283 return result;
284 }
285 };
286 }
287
288 }
289 }