Skip to content

Commit

Permalink
replace enableDefaultRepositories (#4109)
Browse files Browse the repository at this point in the history
  • Loading branch information
pstreef authored Mar 28, 2024
1 parent 5815658 commit be71765
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ public class MavenExecutionContextView extends DelegatingExecutionContext {
private static final String MAVEN_MIRRORS = "org.openrewrite.maven.mirrors";
private static final String MAVEN_CREDENTIALS = "org.openrewrite.maven.auth";
private static final String MAVEN_LOCAL_REPOSITORY = "org.openrewrite.maven.localRepo";
private static final String MAVEN_ADD_LOCAL_REPOSITORY = "org.openrewrite.maven.useLocalRepo";
private static final String MAVEN_ADD_CENTRAL_REPOSITORY = "org.openrewrite.maven.useCentralRepo";
private static final String MAVEN_REPOSITORIES = "org.openrewrite.maven.repos";
private static final String MAVEN_PINNED_SNAPSHOT_VERSIONS = "org.openrewrite.maven.pinnedSnapshotVersions";
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);
Expand Down Expand Up @@ -143,6 +144,24 @@ public MavenRepository getLocalRepository() {
return getMessage(MAVEN_LOCAL_REPOSITORY, MAVEN_LOCAL_DEFAULT);
}

public MavenExecutionContextView setAddLocalRepository(boolean useLocalRepository) {
putMessage(MAVEN_ADD_LOCAL_REPOSITORY, useLocalRepository);
return this;
}

public boolean getAddLocalRepository() {
return getMessage(MAVEN_ADD_LOCAL_REPOSITORY, true);
}

public MavenExecutionContextView setAddCentralRepository(boolean useLocalRepository) {
putMessage(MAVEN_ADD_CENTRAL_REPOSITORY, useLocalRepository);
return this;
}

public boolean getAddCentralRepository() {
return getMessage(MAVEN_ADD_CENTRAL_REPOSITORY, true);
}

public MavenExecutionContextView setRepositories(List<MavenRepository> repositories) {
putMessage(MAVEN_REPOSITORIES, repositories);
return this;
Expand Down Expand Up @@ -212,18 +231,8 @@ 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<String> mapActiveProfiles(MavenSettings settings, String... activeProfiles) {
if(settings.getActiveProfiles() == null) {
if (settings.getActiveProfiles() == null) {
return Arrays.asList(activeProfiles);
}
return Stream.concat(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ public class MavenPomDownloader {
@Nullable
private List<String> activeProfiles;

private boolean addDefaultRepositories = true;

/**
* @param projectPoms Other POMs in this project.
* @param ctx The execution context, which potentially contain Maven settings customization
Expand All @@ -97,8 +95,6 @@ public MavenPomDownloader(Map<Path, Pom> 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;
}

/**
Expand All @@ -110,8 +106,8 @@ public MavenPomDownloader(Map<Path, Pom> projectPoms, ExecutionContext ctx,
*/
public MavenPomDownloader(ExecutionContext ctx) {
this(emptyMap(), HttpSenderExecutionContextView.view(ctx).getHttpSender(), ctx);
Boolean enableDefaultRepositories = MavenExecutionContextView.view(ctx).isEnableDefaultRepositories();
this.addDefaultRepositories = enableDefaultRepositories != null && enableDefaultRepositories;
MavenExecutionContextView.view(ctx).setAddCentralRepository(false);
MavenExecutionContextView.view(ctx).setAddLocalRepository(false);
}

/**
Expand Down Expand Up @@ -665,7 +661,7 @@ Collection<MavenRepository> distinctNormalizedRepositories(
@Nullable ResolvedPom containingPom,
@Nullable String acceptsVersion) {
LinkedHashMap<String, MavenRepository> normalizedRepositories = new LinkedHashMap<>();
if (addDefaultRepositories) {
if (ctx.getAddLocalRepository()) {
normalizedRepositories.put(ctx.getLocalRepository().getId(), ctx.getLocalRepository());
}

Expand All @@ -683,7 +679,7 @@ Collection<MavenRepository> distinctNormalizedRepositories(
normalizedRepositories.put(normalizedRepo.getId(), normalizedRepo);
}
}
if (addDefaultRepositories && !normalizedRepositories.containsKey(MavenRepository.MAVEN_CENTRAL.getId())) {
if (!normalizedRepositories.containsKey(MavenRepository.MAVEN_CENTRAL.getId()) && ctx.getAddCentralRepository()) {
MavenRepository normalizedRepo = normalizeRepository(MavenRepository.MAVEN_CENTRAL, ctx, containingPom);
if (normalizedRepo != null) {
normalizedRepositories.put(normalizedRepo.getId(), normalizedRepo);
Expand Down

0 comments on commit be71765

Please sign in to comment.