Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a logging resolution event listener #725

Merged
merged 8 commits into from
Feb 20, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed 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
*
* https://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.openrewrite.maven;

import org.apache.maven.plugin.logging.Log;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.maven.tree.*;

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

class MavenLoggingResolutionEventListener implements ResolutionEventListener {

private final Log logger;

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

@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) {
StringBuilder sb = new StringBuilder("Failed to download " + gav + pomContaining(containing) + ". Attempted URIs:");
attemptedUris.forEach(uri -> sb.append("\n - ").append(uri));
logger.error(sb);
}

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

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

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

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