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.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 | 0 | public class PluginsReport |
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 | 0 | return i18n.getString( "project-info-report", locale, "report.plugins.name" ); |
81 | |
} |
82 | |
|
83 | |
|
84 | |
public String getDescription( Locale locale ) |
85 | |
{ |
86 | 0 | return i18n.getString( "project-info-report", locale, "report.plugins.description" ); |
87 | |
} |
88 | |
|
89 | |
|
90 | |
public void executeReport( Locale locale ) |
91 | |
{ |
92 | 0 | PluginsRenderer r = new PluginsRenderer( getLog(), getSink(), locale, i18n, project.getPluginArtifacts(), |
93 | |
project.getReportArtifacts(), project, mavenProjectBuilder, |
94 | |
artifactFactory, localRepository ); |
95 | 0 | r.render(); |
96 | 0 | } |
97 | |
|
98 | |
|
99 | |
public String getOutputName() |
100 | |
{ |
101 | 0 | return "plugins"; |
102 | |
} |
103 | |
|
104 | |
|
105 | |
public boolean canGenerateReport() |
106 | |
{ |
107 | 0 | return ( project.getPluginArtifacts() != null && !project.getPluginArtifacts().isEmpty() ) |
108 | |
|| ( project.getReportArtifacts() != null && !project.getReportArtifacts().isEmpty() ); |
109 | |
} |
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | 0 | protected static class PluginsRenderer |
119 | |
extends AbstractMavenReportRenderer |
120 | |
{ |
121 | |
private final Log log; |
122 | |
|
123 | |
private final List plugins; |
124 | |
|
125 | |
private final List reports; |
126 | |
|
127 | |
private final Locale locale; |
128 | |
|
129 | |
private final I18N i18n; |
130 | |
|
131 | |
private final MavenProject project; |
132 | |
|
133 | |
private final MavenProjectBuilder mavenProjectBuilder; |
134 | |
|
135 | |
private final ArtifactFactory artifactFactory; |
136 | |
|
137 | |
private final ArtifactRepository localRepository; |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
public PluginsRenderer( Log log, Sink sink, Locale locale, I18N i18n, Set plugins, Set reports, |
152 | |
MavenProject project, MavenProjectBuilder mavenProjectBuilder, |
153 | |
ArtifactFactory artifactFactory, ArtifactRepository localRepository ) |
154 | |
{ |
155 | 0 | super( sink ); |
156 | |
|
157 | 0 | this.log = log; |
158 | |
|
159 | 0 | this.locale = locale; |
160 | |
|
161 | 0 | this.plugins = new ArrayList( plugins ); |
162 | |
|
163 | 0 | this.reports = new ArrayList( reports ); |
164 | |
|
165 | 0 | this.i18n = i18n; |
166 | |
|
167 | 0 | this.project = project; |
168 | |
|
169 | 0 | this.mavenProjectBuilder = mavenProjectBuilder; |
170 | |
|
171 | 0 | this.artifactFactory = artifactFactory; |
172 | |
|
173 | 0 | this.localRepository = localRepository; |
174 | 0 | } |
175 | |
|
176 | |
|
177 | |
public String getTitle() |
178 | |
{ |
179 | 0 | return getReportString( "report.plugins.title" ); |
180 | |
} |
181 | |
|
182 | |
|
183 | |
public void renderBody() |
184 | |
{ |
185 | |
|
186 | 0 | renderSectionPlugins( true ); |
187 | |
|
188 | |
|
189 | 0 | renderSectionPlugins( false ); |
190 | 0 | } |
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
private void renderSectionPlugins( boolean isPlugins ) |
197 | |
{ |
198 | 0 | List list = ( isPlugins ? plugins : reports ); |
199 | 0 | String[] tableHeader = getPluginTableHeader(); |
200 | |
|
201 | 0 | startSection( ( isPlugins ? getReportString( "report.plugins.title" ) |
202 | |
: getReportString( "report.plugins.report.title" ) ) ); |
203 | |
|
204 | 0 | if ( list == null || list.isEmpty() ) |
205 | |
{ |
206 | |
|
207 | 0 | paragraph( ( isPlugins ? getReportString( "report.plugins.nolist" ) |
208 | |
: getReportString( "report.plugins.report.nolist" ) ) ); |
209 | |
|
210 | 0 | endSection(); |
211 | |
|
212 | 0 | return; |
213 | |
} |
214 | |
|
215 | 0 | Collections.sort( list, getArtifactComparator() ); |
216 | |
|
217 | 0 | startTable(); |
218 | 0 | tableHeader( tableHeader ); |
219 | |
|
220 | 0 | for ( Iterator iterator = list.iterator(); iterator.hasNext(); ) |
221 | |
{ |
222 | 0 | Artifact artifact = (Artifact) iterator.next(); |
223 | |
|
224 | |
VersionRange versionRange; |
225 | 0 | if ( StringUtils.isEmpty( artifact.getVersion() ) ) |
226 | |
{ |
227 | 0 | versionRange = VersionRange.createFromVersion( Artifact.RELEASE_VERSION ); |
228 | |
} |
229 | |
else |
230 | |
{ |
231 | 0 | versionRange = VersionRange.createFromVersion( artifact.getVersion() ); |
232 | |
} |
233 | |
|
234 | 0 | Artifact pluginArtifact = artifactFactory.createParentArtifact( artifact.getGroupId(), artifact |
235 | |
.getArtifactId(), versionRange.toString() ); |
236 | 0 | List artifactRepositories = project.getPluginArtifactRepositories(); |
237 | 0 | if ( artifactRepositories == null ) |
238 | |
{ |
239 | 0 | artifactRepositories = new ArrayList(); |
240 | |
} |
241 | |
try |
242 | |
{ |
243 | 0 | MavenProject pluginProject = mavenProjectBuilder.buildFromRepository( pluginArtifact, |
244 | |
artifactRepositories, |
245 | |
localRepository ); |
246 | 0 | tableRow( getPluginRow( pluginProject.getGroupId(), pluginProject.getArtifactId(), pluginProject |
247 | |
.getVersion(), pluginProject.getUrl() ) ); |
248 | |
} |
249 | 0 | catch ( ProjectBuildingException e ) |
250 | |
{ |
251 | 0 | log.info( "Could not build project for: " + artifact.getArtifactId() + ":" + e.getMessage(), e ); |
252 | 0 | tableRow( getPluginRow( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), |
253 | |
null ) ); |
254 | 0 | } |
255 | |
|
256 | |
} |
257 | 0 | endTable(); |
258 | |
|
259 | 0 | endSection(); |
260 | 0 | } |
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
private String[] getPluginTableHeader() |
267 | |
{ |
268 | |
|
269 | 0 | String groupId = getReportString( "report.dependencyManagement.column.groupId" ); |
270 | 0 | String artifactId = getReportString( "report.dependencyManagement.column.artifactId" ); |
271 | 0 | String version = getReportString( "report.dependencyManagement.column.version" ); |
272 | 0 | return new String[] { groupId, artifactId, version }; |
273 | |
} |
274 | |
|
275 | |
private String[] getPluginRow( String groupId, String artifactId, String version, String link ) |
276 | |
{ |
277 | 0 | artifactId = ProjectInfoReportUtils.getArtifactIdCell( artifactId, link ); |
278 | 0 | return new String[] { groupId, artifactId, version }; |
279 | |
} |
280 | |
|
281 | |
private Comparator getArtifactComparator() |
282 | |
{ |
283 | 0 | return new Comparator() |
284 | |
{ |
285 | |
|
286 | 0 | public int compare( Object o1, Object o2 ) |
287 | |
{ |
288 | 0 | Artifact a1 = (Artifact) o1; |
289 | 0 | Artifact a2 = (Artifact) o2; |
290 | |
|
291 | 0 | int result = a1.getGroupId().compareTo( a2.getGroupId() ); |
292 | 0 | if ( result == 0 ) |
293 | |
{ |
294 | 0 | result = a1.getArtifactId().compareTo( a2.getArtifactId() ); |
295 | |
} |
296 | 0 | return result; |
297 | |
} |
298 | |
}; |
299 | |
} |
300 | |
|
301 | |
private String getReportString( String key ) |
302 | |
{ |
303 | 0 | return i18n.getString( "project-info-report", locale, key ); |
304 | |
} |
305 | |
} |
306 | |
} |