1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.internal.aether;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.UncheckedIOException;
24 import java.nio.charset.StandardCharsets;
25 import java.nio.file.Files;
26 import java.nio.file.Path;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.ListIterator;
30 import java.util.Objects;
31
32 import org.apache.maven.api.model.InputLocation;
33 import org.apache.maven.api.model.Plugin;
34 import org.eclipse.aether.AbstractRepositoryListener;
35 import org.eclipse.aether.RepositoryEvent;
36 import org.eclipse.aether.RepositorySystemSession;
37 import org.eclipse.aether.RequestTrace;
38 import org.eclipse.aether.artifact.Artifact;
39 import org.eclipse.aether.collection.CollectStepData;
40 import org.eclipse.aether.graph.Dependency;
41 import org.eclipse.aether.graph.DependencyNode;
42 import org.eclipse.aether.repository.RemoteRepository;
43 import org.eclipse.aether.resolution.ArtifactDescriptorRequest;
44 import org.eclipse.aether.resolution.ArtifactRequest;
45 import org.eclipse.aether.util.artifact.ArtifactIdUtils;
46
47 import static java.util.Objects.requireNonNull;
48
49
50
51
52
53
54
55 class ReverseTreeRepositoryListener extends AbstractRepositoryListener {
56 @Override
57 public void artifactResolved(RepositoryEvent event) {
58 requireNonNull(event, "event cannot be null");
59
60 if (!isLocalRepositoryArtifactOrMissing(event.getSession(), event.getArtifact())) {
61 return;
62 }
63
64 RequestTrace trace = event.getTrace();
65
66 CollectStepData collectStepTrace = null;
67 ArtifactRequest artifactRequest = null;
68 ArtifactDescriptorRequest artifactDescriptorRequest = null;
69 Plugin plugin = null;
70
71 while (trace != null) {
72 Object data = trace.getData();
73 if (data instanceof CollectStepData collectStepData) {
74 collectStepTrace = collectStepData;
75 } else if (data instanceof ArtifactDescriptorRequest artifactDescriptorRequestData) {
76 artifactDescriptorRequest = artifactDescriptorRequestData;
77 } else if (data instanceof ArtifactRequest artifactRequestData) {
78 artifactRequest = artifactRequestData;
79 } else if (data instanceof Plugin pluginData) {
80 plugin = pluginData;
81 } else if (data instanceof org.apache.maven.model.Plugin pluginData) {
82 plugin = pluginData.getDelegate();
83 }
84 trace = trace.getParent();
85 }
86
87 Path trackingDir;
88 boolean missing = event.getFile() == null;
89 if (missing) {
90
91 File dir = event.getSession().getLocalRepository().getBasedir();
92 dir = new File(
93 dir, event.getSession().getLocalRepositoryManager().getPathForLocalArtifact(event.getArtifact()));
94 trackingDir = dir.getParentFile().toPath().resolve(".tracking");
95 } else {
96 trackingDir = event.getFile().getParentFile().toPath().resolve(".tracking");
97 }
98
99 String baseName;
100 String ext = missing ? ".miss" : ".dep";
101 Path trackingFile = null;
102
103 StringBuilder indent = new StringBuilder();
104 ArrayList<String> trackingData = new ArrayList<>();
105
106 if (collectStepTrace == null && plugin != null) {
107 ext = ".plugin";
108 baseName = plugin.getGroupId() + "_" + plugin.getArtifactId() + "_" + plugin.getVersion();
109 trackingFile = trackingDir.resolve(baseName + ext);
110 if (Files.exists(trackingFile)) {
111 return;
112 }
113
114 if (event.getArtifact() != null) {
115 trackingData.add(indent.toString() + event.getArtifact());
116 indent.append(" ");
117 }
118 trackingData.add(indent + plugin.getGroupId() + ":" + plugin.getArtifactId() + ":" + plugin.getVersion());
119 indent.append(" ");
120
121 InputLocation location = plugin.getLocation("");
122 if (location != null && location.getSource() != null) {
123 trackingData.add(indent + location.getSource().getModelId() + " (implicit)");
124 indent.append(" ");
125 }
126 } else if (collectStepTrace != null) {
127 if (collectStepTrace.getPath().get(0).getArtifact() == null) {
128 return;
129 }
130 baseName = ArtifactIdUtils.toId(collectStepTrace.getPath().get(0).getArtifact())
131 .replace(":", "_");
132 trackingFile = trackingDir.resolve(baseName + ext);
133 if (Files.exists(trackingFile)) {
134 return;
135 }
136
137 Artifact resolvedArtifact = event.getArtifact();
138 Artifact nodeArtifact = collectStepTrace.getNode().getArtifact();
139
140 if (isInScope(resolvedArtifact, nodeArtifact) || "pom".equals(resolvedArtifact.getExtension())) {
141 Dependency node = collectStepTrace.getNode();
142 trackingData.add(resolvedArtifact.toString());
143 indent.append(" ");
144 trackingData.add(indent.toString() + node + " (" + collectStepTrace.getContext() + ")");
145 ListIterator<DependencyNode> iter = collectStepTrace
146 .getPath()
147 .listIterator(collectStepTrace.getPath().size());
148 while (iter.hasPrevious()) {
149 DependencyNode curr = iter.previous();
150 indent.append(" ");
151 trackingData.add(indent.toString() + curr + " (" + collectStepTrace.getContext() + ")");
152 }
153 }
154 }
155
156 if (trackingFile == null) {
157 return;
158 }
159 try {
160 Files.createDirectories(trackingDir);
161
162 trackingData.add("");
163 if (!missing) {
164 if (event.getRepository() != null) {
165 trackingData.add("Repository: " + event.getRepository());
166 }
167 } else {
168 List<RemoteRepository> repositories = new ArrayList<>();
169 if (artifactRequest != null && artifactRequest.getRepositories() != null) {
170 repositories.addAll(artifactRequest.getRepositories());
171 } else if (artifactDescriptorRequest != null && artifactDescriptorRequest.getRepositories() != null) {
172 repositories.addAll(artifactDescriptorRequest.getRepositories());
173 }
174 if (!repositories.isEmpty()) {
175 trackingData.add("Configured repositories:");
176 for (RemoteRepository r : repositories) {
177 trackingData.add(" - " + r.getId() + " : " + r.getUrl());
178 }
179 } else {
180 trackingData.add("No repositories configured");
181 }
182 }
183
184 Files.write(trackingFile, trackingData, StandardCharsets.UTF_8);
185 } catch (IOException e) {
186 throw new UncheckedIOException(e);
187 }
188 }
189
190
191
192
193
194
195
196
197
198 static boolean isLocalRepositoryArtifactOrMissing(RepositorySystemSession session, Artifact artifact) {
199 return artifact.getFile() == null
200 || artifact.getFile()
201 .getPath()
202 .startsWith(session.getLocalRepository().getBasedir().getPath());
203 }
204
205
206
207
208
209
210
211 static CollectStepData lookupCollectStepData(RequestTrace trace) {
212 CollectStepData collectStepTrace = null;
213 while (trace != null) {
214 if (trace.getData() instanceof CollectStepData collectStepData) {
215 collectStepTrace = collectStepData;
216 break;
217 }
218 trace = trace.getParent();
219 }
220 return collectStepTrace;
221 }
222
223
224
225
226
227
228
229 static boolean isInScope(Artifact artifact, Artifact nodeArtifact) {
230 return Objects.equals(artifact.getGroupId(), nodeArtifact.getGroupId())
231 && Objects.equals(artifact.getArtifactId(), nodeArtifact.getArtifactId())
232 && Objects.equals(artifact.getVersion(), nodeArtifact.getVersion());
233 }
234 }