[ https://issues.apache.org/jira/browse/MNG-7619?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17642917#comment-17642917 ]
ASF GitHub Bot commented on MNG-7619: ------------------------------------- michael-o commented on code in PR #900: URL: https://github.com/apache/maven/pull/900#discussion_r1038874052 ########## maven-core/src/main/java/org/apache/maven/internal/aether/ReverseTreeRepositoryListener.java: ########## @@ -0,0 +1,114 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.internal.aether; + +import static java.util.Objects.requireNonNull; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ListIterator; +import java.util.Objects; +import org.eclipse.aether.AbstractRepositoryListener; +import org.eclipse.aether.RepositoryEvent; +import org.eclipse.aether.RequestTrace; +import org.eclipse.aether.artifact.Artifact; +import org.eclipse.aether.collection.CollectStepData; +import org.eclipse.aether.graph.Dependency; +import org.eclipse.aether.graph.DependencyNode; + +/** + * A class building reverse tree using {@link CollectStepData} trace data provided in {@link RepositoryEvent} + * events fired during collection. + * + * @since 3.9.0 + */ +class ReverseTreeRepositoryListener extends AbstractRepositoryListener { + private static final String EOL = System.lineSeparator(); Review Comment: LS, not EOL ########## maven-core/src/main/java/org/apache/maven/internal/aether/ReverseTreeRepositoryListener.java: ########## @@ -0,0 +1,114 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.internal.aether; + +import static java.util.Objects.requireNonNull; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ListIterator; +import java.util.Objects; +import org.eclipse.aether.AbstractRepositoryListener; +import org.eclipse.aether.RepositoryEvent; +import org.eclipse.aether.RequestTrace; +import org.eclipse.aether.artifact.Artifact; +import org.eclipse.aether.collection.CollectStepData; +import org.eclipse.aether.graph.Dependency; +import org.eclipse.aether.graph.DependencyNode; + +/** + * A class building reverse tree using {@link CollectStepData} trace data provided in {@link RepositoryEvent} + * events fired during collection. + * + * @since 3.9.0 + */ +class ReverseTreeRepositoryListener extends AbstractRepositoryListener { + private static final String EOL = System.lineSeparator(); + + @Override + public void artifactResolved(RepositoryEvent event) { + requireNonNull(event, "event cannot be null"); + + if (!event.getArtifact() + .getFile() + .getPath() + .startsWith(event.getSession().getLocalRepository().getBasedir().getPath())) { + return; // reactor artifact + } + RequestTrace trace = event.getTrace(); + CollectStepData collectStepTrace = null; + while (trace != null) { + if (trace.getData() instanceof CollectStepData) { + collectStepTrace = (CollectStepData) trace.getData(); + break; + } + trace = trace.getParent(); + } + + if (collectStepTrace == null) { + return; + } + + Artifact resolvedArtifact = event.getArtifact(); + Artifact nodeArtifact = collectStepTrace.getNode().getArtifact(); + + if (isInScope(resolvedArtifact, nodeArtifact)) { + Dependency node = collectStepTrace.getNode(); + String trackingData = node + " (" + collectStepTrace.getContext() + ")" + EOL; + String indent = ""; + ListIterator<DependencyNode> iter = collectStepTrace + .getPath() + .listIterator(collectStepTrace.getPath().size()); + while (iter.hasPrevious()) { + DependencyNode curr = iter.previous(); + indent += " "; + trackingData += indent + curr + " (" + collectStepTrace.getContext() + ")" + EOL; + } Review Comment: For cases with a loop a string builder is advised. > Maven should explain why an artifact is present in local repository > ------------------------------------------------------------------- > > Key: MNG-7619 > URL: https://issues.apache.org/jira/browse/MNG-7619 > Project: Maven > Issue Type: Improvement > Components: Dependencies > Reporter: Tamas Cservenak > Priority: Major > Fix For: 3.9.0, 4.0.x-candidate, 4.0.0-alpha-3 > > > Ability to make Maven record: > * why given artifact is present in local repository > * record reverse dep tree how this artifact got resolved > This is mostly for detecting dependency resolution anomalies, ideally best > combined with {{-Dmaven.repo.local}} when you use new/empty local repo to > build a project. After the build, you will end up with reverse dep trees in > {{.tracking}} directories for each artifact. > To enable, pass {{-Dmaven.repo.local.recordReverseTree}} on CLI. -- This message was sent by Atlassian Jira (v8.20.10#820010)