public void artifactResolved()

in impl/maven-core/src/main/java/org/apache/maven/internal/aether/ReverseTreeRepositoryListener.java [57:188]


    public void artifactResolved(RepositoryEvent event) {
        requireNonNull(event, "event cannot be null");

        if (!isLocalRepositoryArtifactOrMissing(event.getSession(), event.getArtifact())) {
            return;
        }

        RequestTrace trace = event.getTrace();

        CollectStepData collectStepTrace = null;
        ArtifactRequest artifactRequest = null;
        ArtifactDescriptorRequest artifactDescriptorRequest = null;
        Plugin plugin = null;

        while (trace != null) {
            Object data = trace.getData();
            if (data instanceof CollectStepData collectStepData) {
                collectStepTrace = collectStepData;
            } else if (data instanceof ArtifactDescriptorRequest artifactDescriptorRequestData) {
                artifactDescriptorRequest = artifactDescriptorRequestData;
            } else if (data instanceof ArtifactRequest artifactRequestData) {
                artifactRequest = artifactRequestData;
            } else if (data instanceof Plugin pluginData) {
                plugin = pluginData;
            } else if (data instanceof org.apache.maven.model.Plugin pluginData) {
                plugin = pluginData.getDelegate();
            }
            trace = trace.getParent();
        }

        Path trackingDir;
        boolean missing = event.getFile() == null;
        if (missing) {
            // missing artifact - let's track the path anyway
            File dir = event.getSession().getLocalRepository().getBasedir();
            dir = new File(
                    dir, event.getSession().getLocalRepositoryManager().getPathForLocalArtifact(event.getArtifact()));
            trackingDir = dir.getParentFile().toPath().resolve(".tracking");
        } else {
            trackingDir = event.getFile().getParentFile().toPath().resolve(".tracking");
        }

        String baseName;
        String ext = missing ? ".miss" : ".dep";
        Path trackingFile = null;

        StringBuilder indent = new StringBuilder();
        ArrayList<String> trackingData = new ArrayList<>();

        if (collectStepTrace == null && plugin != null) {
            ext = ".plugin";
            baseName = plugin.getGroupId() + "_" + plugin.getArtifactId() + "_" + plugin.getVersion();
            trackingFile = trackingDir.resolve(baseName + ext);
            if (Files.exists(trackingFile)) {
                return;
            }

            if (event.getArtifact() != null) {
                trackingData.add(indent.toString() + event.getArtifact());
                indent.append("  ");
            }
            trackingData.add(indent + plugin.getGroupId() + ":" + plugin.getArtifactId() + ":" + plugin.getVersion());
            indent.append("  ");

            InputLocation location = plugin.getLocation("");
            if (location != null && location.getSource() != null) {
                trackingData.add(indent + location.getSource().getModelId() + " (implicit)");
                indent.append("  ");
            }
        } else if (collectStepTrace != null) {
            if (collectStepTrace.getPath().get(0).getArtifact() == null) {
                return;
            }
            baseName = ArtifactIdUtils.toId(collectStepTrace.getPath().get(0).getArtifact())
                    .replace(":", "_");
            trackingFile = trackingDir.resolve(baseName + ext);
            if (Files.exists(trackingFile)) {
                return;
            }

            Artifact resolvedArtifact = event.getArtifact();
            Artifact nodeArtifact = collectStepTrace.getNode().getArtifact();

            if (isInScope(resolvedArtifact, nodeArtifact) || "pom".equals(resolvedArtifact.getExtension())) {
                Dependency node = collectStepTrace.getNode();
                trackingData.add(resolvedArtifact.toString());
                indent.append("  ");
                trackingData.add(indent.toString() + node + " (" + collectStepTrace.getContext() + ")");
                ListIterator<DependencyNode> iter = collectStepTrace
                        .getPath()
                        .listIterator(collectStepTrace.getPath().size());
                while (iter.hasPrevious()) {
                    DependencyNode curr = iter.previous();
                    indent.append("  ");
                    trackingData.add(indent.toString() + curr + " (" + collectStepTrace.getContext() + ")");
                }
            }
        }

        if (trackingFile == null) {
            return; // parent or imported bom ?
        }
        try {
            Files.createDirectories(trackingDir);

            trackingData.add("");
            if (!missing) {
                if (event.getRepository() != null) {
                    trackingData.add("Repository: " + event.getRepository());
                }
            } else {
                List<RemoteRepository> repositories = new ArrayList<>();
                if (artifactRequest != null && artifactRequest.getRepositories() != null) {
                    repositories.addAll(artifactRequest.getRepositories());
                } else if (artifactDescriptorRequest != null && artifactDescriptorRequest.getRepositories() != null) {
                    repositories.addAll(artifactDescriptorRequest.getRepositories());
                }
                if (!repositories.isEmpty()) {
                    trackingData.add("Configured repositories:");
                    for (RemoteRepository r : repositories) {
                        trackingData.add(" - " + r.getId() + " : " + r.getUrl());
                    }
                } else {
                    trackingData.add("No repositories configured");
                }
            }

            Files.write(trackingFile, trackingData, StandardCharsets.UTF_8);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }