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