1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.report.projectinfo.dependencies;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.jar.JarEntry;
28
29 import org.apache.maven.artifact.Artifact;
30 import org.apache.maven.project.MavenProject;
31 import org.apache.maven.shared.dependency.graph.DependencyNode;
32 import org.apache.maven.shared.jar.JarAnalyzer;
33 import org.apache.maven.shared.jar.JarData;
34 import org.apache.maven.shared.jar.classes.JarClasses;
35 import org.apache.maven.shared.jar.classes.JarClassesAnalysis;
36 import org.codehaus.plexus.util.StringUtils;
37
38
39
40
41 public class Dependencies {
42 private final MavenProject project;
43
44 private final DependencyNode dependencyNode;
45
46 private final JarClassesAnalysis classesAnalyzer;
47
48
49
50
51 private List<Artifact> projectDependencies;
52
53
54
55
56 private List<Artifact> projectTransitiveDependencies;
57
58
59
60
61 private List<Artifact> allDependencies;
62
63
64
65
66 private Map<String, List<Artifact>> dependenciesByScope;
67
68
69
70
71 private Map<String, List<Artifact>> transitiveDependenciesByScope;
72
73
74
75
76 private Map<String, JarData> dependencyDetails;
77
78
79
80
81
82
83
84
85 public Dependencies(MavenProject project, DependencyNode dependencyTreeNode, JarClassesAnalysis classesAnalyzer) {
86 this.project = project;
87 this.dependencyNode = dependencyTreeNode;
88 this.classesAnalyzer = classesAnalyzer;
89 }
90
91
92
93
94
95
96 public MavenProject getProject() {
97 return project;
98 }
99
100
101
102
103 public boolean hasDependencies() {
104 return (getProjectDependencies() != null) && (!getProjectDependencies().isEmpty());
105 }
106
107
108
109
110 public List<Artifact> getProjectDependencies() {
111 if (projectDependencies != null) {
112 return projectDependencies;
113 }
114
115 projectDependencies = new ArrayList<>();
116 for (DependencyNode dep : dependencyNode.getChildren()) {
117 projectDependencies.add(dep.getArtifact());
118 }
119
120 return projectDependencies;
121 }
122
123
124
125
126 public List<Artifact> getTransitiveDependencies() {
127 if (projectTransitiveDependencies != null) {
128 return projectTransitiveDependencies;
129 }
130
131 projectTransitiveDependencies = new ArrayList<>(getAllDependencies());
132 projectTransitiveDependencies.removeAll(getProjectDependencies());
133
134 return projectTransitiveDependencies;
135 }
136
137
138
139
140 public List<Artifact> getAllDependencies() {
141 if (allDependencies != null) {
142 return allDependencies;
143 }
144
145 allDependencies = new ArrayList<>();
146
147 addAllChildrenDependencies(dependencyNode);
148
149 return allDependencies;
150 }
151
152
153
154
155
156
157
158
159
160
161 public Map<String, List<Artifact>> getDependenciesByScope(boolean isTransitively) {
162 if (isTransitively) {
163 if (transitiveDependenciesByScope != null) {
164 return transitiveDependenciesByScope;
165 }
166
167 transitiveDependenciesByScope = new HashMap<>();
168 for (Artifact artifact : getTransitiveDependencies()) {
169 List<Artifact> multiValue = transitiveDependenciesByScope.get(artifact.getScope());
170 if (multiValue == null) {
171 multiValue = new ArrayList<>();
172 }
173
174 if (!multiValue.contains(artifact)) {
175 multiValue.add(artifact);
176 }
177 transitiveDependenciesByScope.put(artifact.getScope(), multiValue);
178 }
179
180 return transitiveDependenciesByScope;
181 }
182
183 if (dependenciesByScope != null) {
184 return dependenciesByScope;
185 }
186
187 dependenciesByScope = new HashMap<>();
188 for (Artifact artifact : getProjectDependencies()) {
189 List<Artifact> multiValue = dependenciesByScope.get(artifact.getScope());
190 if (multiValue == null) {
191 multiValue = new ArrayList<>();
192 }
193
194 if (!multiValue.contains(artifact)) {
195 multiValue.add(artifact);
196 }
197 dependenciesByScope.put(artifact.getScope(), multiValue);
198 }
199
200 return dependenciesByScope;
201 }
202
203
204
205
206
207
208 public JarData getJarDependencyDetails(Artifact artifact) throws IOException {
209 if (dependencyDetails == null) {
210 dependencyDetails = new HashMap<>();
211 }
212
213 JarData jarData = dependencyDetails.get(artifact.getId());
214 if (jarData != null) {
215 return jarData;
216 }
217
218 File file = getFile(artifact);
219
220 if (file.isDirectory()) {
221 jarData = new JarData(artifact.getFile(), null, new ArrayList<JarEntry>());
222
223 jarData.setJarClasses(new JarClasses());
224 } else {
225 JarAnalyzer jarAnalyzer = new JarAnalyzer(file);
226
227 try {
228 classesAnalyzer.analyze(jarAnalyzer);
229 } finally {
230 jarAnalyzer.closeQuietly();
231 }
232
233 jarData = jarAnalyzer.getJarData();
234 }
235
236 dependencyDetails.put(artifact.getId(), jarData);
237
238 return jarData;
239 }
240
241
242
243
244
245
246
247
248
249
250 private void addAllChildrenDependencies(DependencyNode dependencyNode) {
251 for (DependencyNode subdependencyNode : dependencyNode.getChildren()) {
252 Artifact artifact = subdependencyNode.getArtifact();
253
254 if (artifact.getGroupId().equals(project.getGroupId())
255 && artifact.getArtifactId().equals(project.getArtifactId())
256 && artifact.getVersion().equals(project.getVersion())) {
257 continue;
258 }
259
260 if (!allDependencies.contains(artifact)) {
261 allDependencies.add(artifact);
262 }
263
264 addAllChildrenDependencies(subdependencyNode);
265 }
266 }
267
268
269
270
271
272
273
274 public File getFile(Artifact artifact) {
275 File file = artifact.getFile();
276
277 if (file.isDirectory()) {
278
279 String filename = artifact.getArtifactId() + '-' + artifact.getVersion();
280 if (StringUtils.isNotEmpty(artifact.getClassifier())) {
281 filename += '-' + artifact.getClassifier();
282 }
283 filename += '.' + artifact.getType();
284
285 File jar = new File(file, "../" + filename);
286
287 if (jar.exists()) {
288 return jar;
289 }
290 }
291
292 return file;
293 }
294 }