Skip to content

Commit

Permalink
Fix Gradle dependency resolution always checking mavenCentral().
Browse files Browse the repository at this point in the history
 In Gradle, Maven Central is not implicitly defined as an artifact repository.
  • Loading branch information
sambsnyd committed Nov 7, 2023
1 parent a042851 commit 635fdaa
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ static GradleProject addDependency(
resolvedGav = null;
transitiveDependencies = Collections.emptyList();
} else {
MavenPomDownloader mpd = new MavenPomDownloader(emptyMap(), ctx, null, null);
MavenPomDownloader mpd = new MavenPomDownloader(ctx);
Pom pom = mpd.download(gav, null, null, gp.getMavenRepositories());
ResolvedPom resolvedPom = pom.resolve(emptyList(), mpd, gp.getMavenRepositories(), ctx);
resolvedGav = resolvedPom.getGav();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import java.util.*;

import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static java.util.Objects.requireNonNull;

@Value
Expand Down Expand Up @@ -437,7 +436,7 @@ static GradleProject replaceVersion(GradleProject gp, ExecutionContext ctx, Grou
return gp;
}

MavenPomDownloader mpd = new MavenPomDownloader(emptyMap(), ctx, null, null);
MavenPomDownloader mpd = new MavenPomDownloader(ctx);
Pom pom = mpd.download(gav, null, null, gp.getMavenRepositories());
ResolvedPom resolvedPom = pom.resolve(emptyList(), mpd, gp.getMavenRepositories(), ctx);
ResolvedGroupArtifactVersion resolvedGav = resolvedPom.getGav();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import java.util.regex.Pattern;
import java.util.stream.Stream;

import static java.util.Collections.emptyMap;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toList;

Expand Down Expand Up @@ -87,6 +88,8 @@ public class MavenPomDownloader {

private final CheckedFunction1<HttpSender.Request, byte[]> sendRequest;

private boolean addDefaultRepositories = true;

/**
* @param projectPoms Other POMs in this project.
* @param ctx The execution context, which potentially contain Maven settings customization
Expand All @@ -104,9 +107,21 @@ public MavenPomDownloader(Map<Path, Pom> projectPoms, ExecutionContext ctx,
this.activeProfiles = activeProfiles;
}

/**
* A MavenPomDownloader for non-maven contexts where there are no project poms or assumption that maven central
* is implicitly added as a repository. In a Maven contexts, a non-empty projectPoms should be specified to
* {@link #MavenPomDownloader(Map, ExecutionContext)} for accurate results.
*
* @param ctx The execution context, which potentially contain Maven settings customization and {@link HttpSender} customization.
*/
public MavenPomDownloader(ExecutionContext ctx) {
this(emptyMap(), HttpSenderExecutionContextView.view(ctx).getHttpSender(), ctx);
this.addDefaultRepositories = false;
}

/**
* @param projectPoms Other POMs in this project.
* @param ctx The execution context, which potentially contain Maven settings customization and
* @param ctx The execution context, which potentially contain Maven settings customization
* and {@link HttpSender} customization.
*/
public MavenPomDownloader(Map<Path, Pom> projectPoms, ExecutionContext ctx) {
Expand Down Expand Up @@ -646,8 +661,9 @@ private Collection<MavenRepository> distinctNormalizedRepositories(List<MavenRep
normalizedRepositories.add(normalizedRepo);
}
}

normalizedRepositories.add(normalizeRepository(MavenRepository.MAVEN_CENTRAL, containingPom));
if(addDefaultRepositories) {
normalizedRepositories.add(normalizeRepository(MavenRepository.MAVEN_CENTRAL, containingPom));
}
return normalizedRepositories;
}

Expand Down

0 comments on commit 635fdaa

Please sign in to comment.