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 | 0 | 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 | 0 | PluginsRenderer r = new PluginsRenderer( getLog(), getSink(), locale, getI18N( locale ), project.getPluginArtifacts(), |
80 | |
project.getReportArtifacts(), project, mavenProjectBuilder, |
81 | |
artifactFactory, localRepository ); |
82 | 0 | r.render(); |
83 | 0 | } |
84 | |
|
85 | |
|
86 | |
public String getOutputName() |
87 | |
{ |
88 | 0 | return "plugins"; |
89 | |
} |
90 | |
|
91 | |
@Override |
92 | |
protected String getI18Nsection() |
93 | |
{ |
94 | 0 | return "plugins"; |
95 | |
} |
96 | |
|
97 | |
@Override |
98 | |
public boolean canGenerateReport() |
99 | |
{ |
100 | 0 | return ( project.getPluginArtifacts() != null && !project.getPluginArtifacts().isEmpty() ) |
101 | |
|| ( project.getReportArtifacts() != null && !project.getReportArtifacts().isEmpty() ); |
102 | |
} |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | 0 | 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 | 0 | super( sink, i18n, locale ); |
145 | |
|
146 | 0 | this.log = log; |
147 | |
|
148 | 0 | this.plugins = new ArrayList<Artifact>( plugins ); |
149 | |
|
150 | 0 | this.reports = new ArrayList<Artifact>( reports ); |
151 | |
|
152 | 0 | this.project = project; |
153 | |
|
154 | 0 | this.mavenProjectBuilder = mavenProjectBuilder; |
155 | |
|
156 | 0 | this.artifactFactory = artifactFactory; |
157 | |
|
158 | 0 | this.localRepository = localRepository; |
159 | 0 | } |
160 | |
|
161 | |
@Override |
162 | |
protected String getI18Nsection() |
163 | |
{ |
164 | 0 | return "plugins"; |
165 | |
} |
166 | |
|
167 | |
@Override |
168 | |
public void renderBody() |
169 | |
{ |
170 | |
|
171 | 0 | renderSectionPlugins( true ); |
172 | |
|
173 | |
|
174 | 0 | renderSectionPlugins( false ); |
175 | 0 | } |
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
private void renderSectionPlugins( boolean isPlugins ) |
182 | |
{ |
183 | 0 | List<Artifact> list = ( isPlugins ? plugins : reports ); |
184 | 0 | String[] tableHeader = getPluginTableHeader(); |
185 | |
|
186 | 0 | startSection( ( isPlugins ? getI18nString( "title" ) |
187 | |
: getI18nString( "report.title" ) ) ); |
188 | |
|
189 | 0 | if ( list == null || list.isEmpty() ) |
190 | |
{ |
191 | |
|
192 | 0 | paragraph( ( isPlugins ? getI18nString( "nolist" ) |
193 | |
: getI18nString( "report.nolist" ) ) ); |
194 | |
|
195 | 0 | endSection(); |
196 | |
|
197 | 0 | return; |
198 | |
} |
199 | |
|
200 | 0 | Collections.sort( list, getArtifactComparator() ); |
201 | |
|
202 | 0 | startTable(); |
203 | 0 | tableHeader( tableHeader ); |
204 | |
|
205 | 0 | for ( Artifact artifact : list ) |
206 | |
{ |
207 | |
VersionRange versionRange; |
208 | 0 | if ( StringUtils.isEmpty( artifact.getVersion() ) ) |
209 | |
{ |
210 | 0 | versionRange = VersionRange.createFromVersion( Artifact.RELEASE_VERSION ); |
211 | |
} |
212 | |
else |
213 | |
{ |
214 | 0 | versionRange = VersionRange.createFromVersion( artifact.getVersion() ); |
215 | |
} |
216 | |
|
217 | 0 | Artifact pluginArtifact = artifactFactory.createParentArtifact( artifact.getGroupId(), artifact |
218 | |
.getArtifactId(), versionRange.toString() ); |
219 | |
@SuppressWarnings( "unchecked" ) |
220 | 0 | List<ArtifactRepository> artifactRepositories = project.getPluginArtifactRepositories(); |
221 | 0 | if ( artifactRepositories == null ) |
222 | |
{ |
223 | 0 | artifactRepositories = new ArrayList<ArtifactRepository>(); |
224 | |
} |
225 | |
try |
226 | |
{ |
227 | 0 | MavenProject pluginProject = mavenProjectBuilder.buildFromRepository( pluginArtifact, |
228 | |
artifactRepositories, |
229 | |
localRepository ); |
230 | 0 | tableRow( getPluginRow( pluginProject.getGroupId(), pluginProject.getArtifactId(), pluginProject |
231 | |
.getVersion(), pluginProject.getUrl() ) ); |
232 | |
} |
233 | 0 | catch ( ProjectBuildingException e ) |
234 | |
{ |
235 | 0 | log.info( "Could not build project for: " + artifact.getArtifactId() + ":" + e.getMessage(), e ); |
236 | 0 | tableRow( getPluginRow( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), |
237 | |
null ) ); |
238 | 0 | } |
239 | |
|
240 | 0 | } |
241 | 0 | endTable(); |
242 | |
|
243 | 0 | endSection(); |
244 | 0 | } |
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
private String[] getPluginTableHeader() |
251 | |
{ |
252 | |
|
253 | 0 | String groupId = getI18nString( "dependencyManagement", "column.groupId" ); |
254 | 0 | String artifactId = getI18nString( "dependencyManagement", "column.artifactId" ); |
255 | 0 | String version = getI18nString( "dependencyManagement", "column.version" ); |
256 | 0 | return new String[] { groupId, artifactId, version }; |
257 | |
} |
258 | |
|
259 | |
private String[] getPluginRow( String groupId, String artifactId, String version, String link ) |
260 | |
{ |
261 | 0 | artifactId = ProjectInfoReportUtils.getArtifactIdCell( artifactId, link ); |
262 | 0 | return new String[] { groupId, artifactId, version }; |
263 | |
} |
264 | |
|
265 | |
private Comparator<Artifact> getArtifactComparator() |
266 | |
{ |
267 | 0 | return new Comparator<Artifact>() |
268 | 0 | { |
269 | |
|
270 | |
public int compare( Artifact a1, Artifact a2 ) |
271 | |
{ |
272 | 0 | int result = a1.getGroupId().compareTo( a2.getGroupId() ); |
273 | 0 | if ( result == 0 ) |
274 | |
{ |
275 | 0 | result = a1.getArtifactId().compareTo( a2.getArtifactId() ); |
276 | |
} |
277 | 0 | return result; |
278 | |
} |
279 | |
}; |
280 | |
} |
281 | |
} |
282 | |
} |