1 | |
package org.apache.maven.report.projectinfo.dependencies.renderer; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.util.Collections; |
23 | |
import java.util.Comparator; |
24 | |
import java.util.List; |
25 | |
import java.util.Locale; |
26 | |
import java.util.Map; |
27 | |
|
28 | |
import org.apache.maven.artifact.Artifact; |
29 | |
import org.apache.maven.artifact.factory.ArtifactFactory; |
30 | |
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException; |
31 | |
import org.apache.maven.artifact.metadata.ArtifactMetadataSource; |
32 | |
import org.apache.maven.artifact.repository.ArtifactRepository; |
33 | |
import org.apache.maven.artifact.versioning.ArtifactVersion; |
34 | |
import org.apache.maven.doxia.sink.Sink; |
35 | |
import org.apache.maven.model.Dependency; |
36 | |
import org.apache.maven.model.License; |
37 | |
import org.apache.maven.plugin.logging.Log; |
38 | |
import org.apache.maven.project.MavenProject; |
39 | |
import org.apache.maven.project.MavenProjectBuilder; |
40 | |
import org.apache.maven.project.ProjectBuildingException; |
41 | |
import org.apache.maven.report.projectinfo.AbstractProjectInfoRenderer; |
42 | |
import org.apache.maven.report.projectinfo.ProjectInfoReportUtils; |
43 | |
import org.apache.maven.report.projectinfo.dependencies.ManagementDependencies; |
44 | |
import org.apache.maven.report.projectinfo.dependencies.RepositoryUtils; |
45 | |
import org.codehaus.plexus.i18n.I18N; |
46 | |
import org.codehaus.plexus.util.StringUtils; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public class DependencyManagementRenderer |
54 | |
extends AbstractProjectInfoRenderer |
55 | |
{ |
56 | |
private final ManagementDependencies dependencies; |
57 | |
|
58 | |
private final Log log; |
59 | |
|
60 | |
private final ArtifactMetadataSource artifactMetadataSource; |
61 | |
|
62 | |
private final ArtifactFactory artifactFactory; |
63 | |
|
64 | |
private final MavenProjectBuilder mavenProjectBuilder; |
65 | |
|
66 | |
private final List<ArtifactRepository> remoteRepositories; |
67 | |
|
68 | |
private final ArtifactRepository localRepository; |
69 | |
|
70 | |
private final RepositoryUtils repoUtils; |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
public DependencyManagementRenderer( Sink sink, Locale locale, I18N i18n, Log log, |
87 | |
ManagementDependencies dependencies, |
88 | |
ArtifactMetadataSource artifactMetadataSource, |
89 | |
ArtifactFactory artifactFactory, MavenProjectBuilder mavenProjectBuilder, |
90 | |
List<ArtifactRepository> remoteRepositories, |
91 | |
ArtifactRepository localRepository, RepositoryUtils repoUtils ) |
92 | |
{ |
93 | 1 | super( sink, i18n, locale ); |
94 | |
|
95 | 1 | this.log = log; |
96 | 1 | this.dependencies = dependencies; |
97 | 1 | this.artifactMetadataSource = artifactMetadataSource; |
98 | 1 | this.artifactFactory = artifactFactory; |
99 | 1 | this.mavenProjectBuilder = mavenProjectBuilder; |
100 | 1 | this.remoteRepositories = remoteRepositories; |
101 | 1 | this.localRepository = localRepository; |
102 | 1 | this.repoUtils = repoUtils; |
103 | 1 | } |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
@Override |
110 | |
protected String getI18Nsection() |
111 | |
{ |
112 | 9 | return "dependencyManagement"; |
113 | |
} |
114 | |
|
115 | |
@Override |
116 | |
public void renderBody() |
117 | |
{ |
118 | |
|
119 | |
|
120 | 1 | if ( !dependencies.hasDependencies() ) |
121 | |
{ |
122 | 0 | startSection( getTitle() ); |
123 | |
|
124 | 0 | paragraph( getI18nString( "nolist" ) ); |
125 | |
|
126 | 0 | endSection(); |
127 | |
|
128 | 0 | return; |
129 | |
} |
130 | |
|
131 | |
|
132 | 1 | renderSectionProjectDependencies(); |
133 | 1 | } |
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
private void renderSectionProjectDependencies() |
140 | |
{ |
141 | 1 | startSection( getTitle() ); |
142 | |
|
143 | |
|
144 | 1 | Map<String, List<Dependency>> dependenciesByScope = dependencies.getManagementDependenciesByScope(); |
145 | |
|
146 | 1 | renderDependenciesForAllScopes( dependenciesByScope ); |
147 | |
|
148 | 1 | endSection(); |
149 | 1 | } |
150 | |
|
151 | |
private void renderDependenciesForAllScopes( Map<String, List<Dependency>> dependenciesByScope ) |
152 | |
{ |
153 | 1 | renderDependenciesForScope( Artifact.SCOPE_COMPILE, dependenciesByScope.get( Artifact.SCOPE_COMPILE ) ); |
154 | 1 | renderDependenciesForScope( Artifact.SCOPE_RUNTIME, dependenciesByScope.get( Artifact.SCOPE_RUNTIME ) ); |
155 | 1 | renderDependenciesForScope( Artifact.SCOPE_TEST, dependenciesByScope.get( Artifact.SCOPE_TEST ) ); |
156 | 1 | renderDependenciesForScope( Artifact.SCOPE_PROVIDED, dependenciesByScope.get( Artifact.SCOPE_PROVIDED ) ); |
157 | 1 | renderDependenciesForScope( Artifact.SCOPE_SYSTEM, dependenciesByScope.get( Artifact.SCOPE_SYSTEM ) ); |
158 | 1 | } |
159 | |
|
160 | |
private String[] getDependencyTableHeader( boolean hasClassifier ) |
161 | |
{ |
162 | 1 | String groupId = getI18nString( "column.groupId" ); |
163 | 1 | String artifactId = getI18nString( "column.artifactId" ); |
164 | 1 | String version = getI18nString( "column.version" ); |
165 | 1 | String classifier = getI18nString( "column.classifier" ); |
166 | 1 | String type = getI18nString( "column.type" ); |
167 | 1 | String license = getI18nString( "column.license" ); |
168 | |
|
169 | 1 | if ( hasClassifier ) |
170 | |
{ |
171 | 0 | return new String[] { groupId, artifactId, version, classifier, type, license }; |
172 | |
} |
173 | |
|
174 | 1 | return new String[] { groupId, artifactId, version, type, license }; |
175 | |
} |
176 | |
|
177 | |
private void renderDependenciesForScope( String scope, List<Dependency> artifacts ) |
178 | |
{ |
179 | 5 | if ( artifacts != null ) |
180 | |
{ |
181 | |
|
182 | 1 | Collections.sort( artifacts, getDependencyComparator() ); |
183 | |
|
184 | 1 | startSection( scope ); |
185 | |
|
186 | 1 | paragraph( getI18nString( "intro." + scope ) ); |
187 | 1 | startTable(); |
188 | |
|
189 | 1 | boolean hasClassifier = false; |
190 | 1 | for ( Dependency dependency : artifacts ) |
191 | |
{ |
192 | 1 | if ( StringUtils.isNotEmpty( dependency.getClassifier() ) ) |
193 | |
{ |
194 | 0 | hasClassifier = true; |
195 | 0 | break; |
196 | |
} |
197 | |
} |
198 | |
|
199 | 1 | String[] tableHeader = getDependencyTableHeader( hasClassifier ); |
200 | 1 | tableHeader( tableHeader ); |
201 | |
|
202 | 1 | for ( Dependency dependency : artifacts ) |
203 | |
{ |
204 | 1 | tableRow( getDependencyRow( dependency, hasClassifier ) ); |
205 | |
} |
206 | 1 | endTable(); |
207 | |
|
208 | 1 | endSection(); |
209 | |
} |
210 | 5 | } |
211 | |
|
212 | |
@SuppressWarnings( "unchecked" ) |
213 | |
private String[] getDependencyRow( Dependency dependency, boolean hasClassifier ) |
214 | |
{ |
215 | 1 | Artifact artifact = |
216 | |
artifactFactory.createProjectArtifact( dependency.getGroupId(), dependency.getArtifactId(), |
217 | |
dependency.getVersion() ); |
218 | |
|
219 | 1 | StringBuffer licensesBuffer = new StringBuffer(); |
220 | 1 | String url = null; |
221 | |
try |
222 | |
{ |
223 | 1 | List<ArtifactVersion> versions = |
224 | |
artifactMetadataSource.retrieveAvailableVersions( artifact, localRepository, remoteRepositories ); |
225 | |
|
226 | 1 | if ( versions.size() > 0 ) |
227 | |
{ |
228 | 1 | Collections.sort( versions ); |
229 | |
|
230 | 1 | artifact.setVersion( versions.get( versions.size() - 1 ).toString() ); |
231 | |
} |
232 | |
|
233 | 1 | url = |
234 | |
ProjectInfoReportUtils.getArtifactUrl( artifactFactory, artifact, mavenProjectBuilder, |
235 | |
remoteRepositories, localRepository ); |
236 | |
|
237 | 1 | MavenProject artifactProject = repoUtils.getMavenProjectFromRepository( artifact ); |
238 | |
|
239 | 1 | List<License> licenses = artifactProject.getLicenses(); |
240 | 1 | for ( License license : licenses ) |
241 | |
{ |
242 | 1 | String licenseCell = ProjectInfoReportUtils.getArtifactIdCell( license.getName(), license.getUrl() ); |
243 | 1 | if ( licensesBuffer.length() > 0 ) |
244 | |
{ |
245 | 0 | licensesBuffer.append( ", " ); |
246 | |
} |
247 | 1 | licensesBuffer.append( licenseCell ); |
248 | 1 | } |
249 | |
} |
250 | 0 | catch ( ArtifactMetadataRetrievalException e ) |
251 | |
{ |
252 | 0 | log.warn( "Unable to retrieve versions for " + artifact.getId() + " from repository.", e ); |
253 | |
} |
254 | 0 | catch ( ProjectBuildingException e ) |
255 | |
{ |
256 | 0 | log.warn( "Unable to create Maven project for " + artifact.getId() + " from repository.", e ); |
257 | 1 | } |
258 | |
|
259 | 1 | String artifactIdCell = ProjectInfoReportUtils.getArtifactIdCell( artifact.getArtifactId(), url ); |
260 | |
|
261 | 1 | if ( hasClassifier ) |
262 | |
{ |
263 | 0 | return new String[] { dependency.getGroupId(), artifactIdCell, dependency.getVersion(), |
264 | |
dependency.getClassifier(), dependency.getType(), licensesBuffer.toString() }; |
265 | |
} |
266 | |
|
267 | 1 | return new String[] { dependency.getGroupId(), artifactIdCell, dependency.getVersion(), |
268 | |
dependency.getType(), licensesBuffer.toString() }; |
269 | |
} |
270 | |
|
271 | |
private Comparator<Dependency> getDependencyComparator() |
272 | |
{ |
273 | 1 | return new Comparator<Dependency>() |
274 | 1 | { |
275 | |
public int compare( Dependency a1, Dependency a2 ) |
276 | |
{ |
277 | 0 | int result = a1.getGroupId().compareTo( a2.getGroupId() ); |
278 | 0 | if ( result != 0 ) |
279 | |
{ |
280 | 0 | return result; |
281 | |
} |
282 | |
|
283 | 0 | result = a1.getArtifactId().compareTo( a2.getArtifactId() ); |
284 | 0 | if ( result != 0 ) |
285 | |
{ |
286 | 0 | return result; |
287 | |
} |
288 | |
|
289 | 0 | result = a1.getType().compareTo( a2.getType() ); |
290 | 0 | if ( result != 0 ) |
291 | |
{ |
292 | 0 | return result; |
293 | |
} |
294 | |
|
295 | 0 | if ( a1.getClassifier() == null ) |
296 | |
{ |
297 | 0 | if ( a2.getClassifier() != null ) |
298 | |
{ |
299 | 0 | return 1; |
300 | |
} |
301 | |
} |
302 | |
else |
303 | |
{ |
304 | 0 | if ( a2.getClassifier() != null ) |
305 | |
{ |
306 | 0 | result = a1.getClassifier().compareTo( a2.getClassifier() ); |
307 | |
} |
308 | |
else |
309 | |
{ |
310 | 0 | return -1; |
311 | |
} |
312 | |
} |
313 | |
|
314 | 0 | if ( result != 0 ) |
315 | |
{ |
316 | 0 | return result; |
317 | |
} |
318 | |
|
319 | |
|
320 | 0 | return a1.getVersion().compareTo( a2.getVersion() ); |
321 | |
} |
322 | |
}; |
323 | |
} |
324 | |
} |