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