Skip to content

Commit 7bc9f18

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 99bfe64 commit 7bc9f18

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ public static class Accumulator {
175175
Map<String, Set<GroupArtifact>> versionVariableUsages = new HashMap<>();
176176
Set<GroupArtifact> failedResolutions = new HashSet<>();
177177
Map<JavaProject, ResolvedDependency> modulesWithOldDependency = new HashMap<>();
178+
Map<JavaProject, List<MavenRepository>> moduleRepositories = new HashMap<>();
178179
}
179180

180181
@Override
@@ -210,6 +211,7 @@ public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) {
210211
if (StringUtils.matchesGlob(resolved.getGroupId(), oldGroupId) &&
211212
StringUtils.matchesGlob(resolved.getArtifactId(), oldArtifactId)) {
212213
acc.modulesWithOldDependency.put(maybeJp.get(), resolved);
214+
acc.moduleRepositories.put(maybeJp.get(), gradleProject.getMavenRepositories());
213215
break outer;
214216
}
215217
}
@@ -579,14 +581,27 @@ private SourceFile updateJavaSourceSet(SourceFile sf, ExecutionContext ctx) {
579581
effectiveNewGroupId, effectiveNewArtifactId, oldDep.getVersion(), null);
580582
ResolvedDependency newDep = oldDep
581583
.withGav(newGav)
582-
.withRepository(MavenRepository.MAVEN_CENTRAL);
584+
.withRepository(findRemoteRepository(maybeJp.get()));
583585
JavaSourceSet updated = updater.changeDependency(maybeSourceSet.get(), oldDep, newDep);
584586
if (updated != maybeSourceSet.get()) {
585587
updatedSourceSets.put(cacheKey, updated);
586588
return sf.withMarkers(sf.getMarkers().setByType(updated));
587589
}
588590
return sf;
589591
}
592+
593+
private MavenRepository findRemoteRepository(JavaProject jp) {
594+
List<MavenRepository> repos = acc.moduleRepositories.get(jp);
595+
if (repos != null) {
596+
for (MavenRepository repo : repos) {
597+
String uri = repo.getUri();
598+
if (uri != null && (uri.startsWith("http://") || uri.startsWith("https://"))) {
599+
return repo;
600+
}
601+
}
602+
}
603+
return MavenRepository.MAVEN_CENTRAL;
604+
}
590605
};
591606
}
592607

0 commit comments

Comments
 (0)