From 152606fe7ab2a9d7f9f55e9bbadb2e7961da993b Mon Sep 17 00:00:00 2001 From: Sam Snyder Date: Wed, 27 Mar 2024 11:59:54 -0700 Subject: [PATCH] Ensure that MavenPomDownloader's behavior around adding default repositories can be controlled from the execution context --- .../openrewrite/maven/MavenExecutionContextView.java | 11 +++++++++++ .../maven/internal/MavenPomDownloader.java | 7 +++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/MavenExecutionContextView.java b/rewrite-maven/src/main/java/org/openrewrite/maven/MavenExecutionContextView.java index 1790272ec54..27b50c83725 100644 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/MavenExecutionContextView.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/MavenExecutionContextView.java @@ -45,6 +45,7 @@ public class MavenExecutionContextView extends DelegatingExecutionContext { private static final String MAVEN_POM_CACHE = "org.openrewrite.maven.pomCache"; private static final String MAVEN_RESOLUTION_LISTENER = "org.openrewrite.maven.resolutionListener"; private static final String MAVEN_RESOLUTION_TIME = "org.openrewrite.maven.resolutionTime"; + private static final String IS_ENABLE_DEFAULT_REPOSITORIES = "org.openrewrite.maven.isEnableDefaultRepositories"; public MavenExecutionContextView(ExecutionContext delegate) { super(delegate); @@ -211,6 +212,16 @@ public MavenSettings getSettings() { return getMessage(MAVEN_SETTINGS, null); } + @Nullable + public Boolean isEnableDefaultRepositories() { + return getMessage(IS_ENABLE_DEFAULT_REPOSITORIES, null); + } + + public MavenExecutionContextView setEnableDefaultRepositories(@Nullable Boolean enableDefaultRepositories) { + putMessage(IS_ENABLE_DEFAULT_REPOSITORIES, enableDefaultRepositories); + return this; + } + private static List mapActiveProfiles(MavenSettings settings, String... activeProfiles) { if(settings.getActiveProfiles() == null) { return Arrays.asList(activeProfiles); diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/internal/MavenPomDownloader.java b/rewrite-maven/src/main/java/org/openrewrite/maven/internal/MavenPomDownloader.java index e40b3f4a7ce..0339832ac8c 100644 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/internal/MavenPomDownloader.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/internal/MavenPomDownloader.java @@ -97,6 +97,8 @@ public MavenPomDownloader(Map projectPoms, ExecutionContext ctx, this(projectPoms, HttpSenderExecutionContextView.view(ctx).getHttpSender(), ctx); this.mavenSettings = mavenSettings; this.activeProfiles = activeProfiles; + Boolean enableDefaultRepositories = MavenExecutionContextView.view(ctx).isEnableDefaultRepositories(); + this.addDefaultRepositories = enableDefaultRepositories == null || enableDefaultRepositories; } /** @@ -108,7 +110,8 @@ public MavenPomDownloader(Map projectPoms, ExecutionContext ctx, */ public MavenPomDownloader(ExecutionContext ctx) { this(emptyMap(), HttpSenderExecutionContextView.view(ctx).getHttpSender(), ctx); - this.addDefaultRepositories = false; + Boolean enableDefaultRepositories = MavenExecutionContextView.view(ctx).isEnableDefaultRepositories(); + this.addDefaultRepositories = enableDefaultRepositories != null && enableDefaultRepositories; } /** @@ -249,7 +252,7 @@ public MavenMetadata downloadMetadata(GroupArtifactVersion gav, @Nullable Resolv try { String scheme = URI.create(repo.getUri()).getScheme(); String uri = repo.getUri() + (repo.getUri().endsWith("/") ? "" : "/") + - gav.getGroupId().replace('.', '/') + '/' + + requireNonNull(gav.getGroupId()).replace('.', '/') + '/' + gav.getArtifactId() + '/' + (gav.getVersion() == null ? "" : gav.getVersion() + '/') + "maven-metadata.xml";