Skip to content

Commit 7566269

Browse files
Use project repositories instead of hardcoded Maven Central in Gradle ChangeDependency
Capture Maven repositories from GradleProject in the scanner and use them when downloading JARs for JavaSourceSet updates, falling back to Maven Central only if no remote repository is available. This matches the approach already used in Maven ChangeDependencyGroupIdAndArtifactId and both AddDependency recipes.
1 parent af0520b commit 7566269

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

rewrite-gradle/src/main/java/org/openrewrite/gradle/ChangeDependency.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ public static class Accumulator {
169169
Map<String, Object> versionVariableUpdates = new HashMap<>();
170170
Map<String, Set<GroupArtifact>> versionVariableUsages = new HashMap<>();
171171
Map<JavaProject, ResolvedDependency> modulesWithOldDependency = new HashMap<>();
172+
Map<JavaProject, List<MavenRepository>> moduleRepositories = new HashMap<>();
172173
}
173174

174175
@Override
@@ -204,6 +205,7 @@ public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) {
204205
if (StringUtils.matchesGlob(resolved.getGroupId(), oldGroupId) &&
205206
StringUtils.matchesGlob(resolved.getArtifactId(), oldArtifactId)) {
206207
acc.modulesWithOldDependency.put(maybeJp.get(), resolved);
208+
acc.moduleRepositories.put(maybeJp.get(), gradleProject.getMavenRepositories());
207209
break outer;
208210
}
209211
}
@@ -562,14 +564,27 @@ private SourceFile updateJavaSourceSet(SourceFile sf, ExecutionContext ctx) {
562564
effectiveNewGroupId, effectiveNewArtifactId, oldDep.getVersion(), null);
563565
ResolvedDependency newDep = oldDep
564566
.withGav(newGav)
565-
.withRepository(MavenRepository.MAVEN_CENTRAL);
567+
.withRepository(findRemoteRepository(maybeJp.get()));
566568
JavaSourceSet updated = updater.changeDependency(maybeSourceSet.get(), oldDep, newDep);
567569
if (updated != maybeSourceSet.get()) {
568570
updatedSourceSets.put(cacheKey, updated);
569571
return sf.withMarkers(sf.getMarkers().setByType(updated));
570572
}
571573
return sf;
572574
}
575+
576+
private MavenRepository findRemoteRepository(JavaProject jp) {
577+
List<MavenRepository> repos = acc.moduleRepositories.get(jp);
578+
if (repos != null) {
579+
for (MavenRepository repo : repos) {
580+
String uri = repo.getUri();
581+
if (uri != null && (uri.startsWith("http://") || uri.startsWith("https://"))) {
582+
return repo;
583+
}
584+
}
585+
}
586+
return MavenRepository.MAVEN_CENTRAL;
587+
}
573588
};
574589
}
575590

0 commit comments

Comments
 (0)