Skip to content

Commit

Permalink
Adding a logging resolution listener
Browse files Browse the repository at this point in the history
  • Loading branch information
ammachado committed Jan 31, 2024
1 parent 20fbae7 commit 5b9c1d7
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package org.openrewrite.maven;

import org.apache.maven.plugin.logging.Log;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.maven.tree.GroupArtifactVersion;
import org.openrewrite.maven.tree.ManagedDependency;
import org.openrewrite.maven.tree.MavenRepository;
import org.openrewrite.maven.tree.Pom;
import org.openrewrite.maven.tree.ResolutionEventListener;
import org.openrewrite.maven.tree.ResolvedDependency;
import org.openrewrite.maven.tree.ResolvedGroupArtifactVersion;
import org.openrewrite.maven.tree.ResolvedPom;
import org.openrewrite.maven.tree.Scope;

import java.util.List;
import java.util.Objects;

public class MavenLoggingResolutionListener implements ResolutionEventListener {

private final Log logger;

public MavenLoggingResolutionListener(Log logger) {
this.logger = Objects.requireNonNull(logger, "logger cannot be null");
}

@Override
public void downloadMetadata(GroupArtifactVersion gav) {
if (logger.isDebugEnabled()) {
logger.debug("Downloading metadata for " + gav);
}
}

@Override
public void download(GroupArtifactVersion gav) {
if (logger.isDebugEnabled()) {
logger.debug("Downloading " + gav);
}
}

@Override
public void downloadSuccess(ResolvedGroupArtifactVersion gav, @Nullable ResolvedPom containing) {
if (logger.isDebugEnabled()) {
logger.debug("Downloaded " + gav + pomContaining(containing));
}
}

@Override
public void downloadError(GroupArtifactVersion gav, List<String> attemptedUris, @Nullable Pom containing) {
if (logger.isInfoEnabled()) {
StringBuilder sb = new StringBuilder("Failed to download " + gav + pomContaining(containing));
attemptedUris.forEach(uri -> sb.append("\n - ").append(uri));
logger.debug(sb);
}
}

@Override
public void parent(Pom parent, Pom containing) {
if (logger.isDebugEnabled()) {
logger.debug("Resolved parent pom " + containing + pomContaining(containing));
}
}

@Override
public void dependency(Scope scope, ResolvedDependency resolvedDependency, ResolvedPom containing) {
if (logger.isDebugEnabled()) {
logger.debug("Resolved dependency " + resolvedDependency + " with scope " + scope + pomContaining(containing));
}
}

@Override
public void bomImport(ResolvedGroupArtifactVersion gav, Pom containing) {
if (logger.isDebugEnabled()) {
logger.debug("Resolved imported dependency " + gav + pomContaining(containing));
}
}

@Override
public void property(String key, String value, Pom containing) {
if (logger.isDebugEnabled()) {
logger.debug("Resolved property \"" + key + "\" with value \"" + value + "\"" + pomContaining(containing));
}
}

@Override
public void dependencyManagement(ManagedDependency dependencyManagement, Pom containing) {
if (logger.isDebugEnabled()) {
logger.debug("Resolved managed dependency " + dependencyManagement + " on " + pomContaining(containing));
}
}

@Override
public void repository(MavenRepository mavenRepository, @Nullable ResolvedPom containing) {
if (logger.isDebugEnabled()) {
logger.debug("Resolving maven repository " + mavenRepository + pomContaining(containing));
}
}

@Override
public void repositoryAccessFailed(String uri, Throwable e) {
logger.info("Failed to access maven repository " + uri, e);
}

private static String pomContaining(@Nullable Pom containing) {
return containing != null ? " on " + containing : "";
}

private static String pomContaining(@Nullable ResolvedPom containing) {
return containing != null ? " on " + containing : "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ public Map<MavenProject, Xml.Document> parseMaven(List<MavenProject> mavenProjec
MavenSettings settings = buildSettings();
MavenExecutionContextView mavenExecutionContext = MavenExecutionContextView.view(ctx);
mavenExecutionContext.setMavenSettings(settings);
mavenExecutionContext.setResolutionListener(new MavenLoggingResolutionListener(logger));

if (pomCacheEnabled) {
//The default pom cache is enabled as a two-layer cache L1 == in-memory and L2 == RocksDb
Expand Down

0 comments on commit 5b9c1d7

Please sign in to comment.