Skip to content

Commit ac59798

Browse files
Prepend without clearing
1 parent fbc70c6 commit ac59798

1 file changed

Lines changed: 16 additions & 15 deletions

File tree

rewrite-gradle-tooling-model/model/src/main/java/org/openrewrite/gradle/toolingapi/OpenRewriteModelBuilder.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -173,29 +173,30 @@ private static String mirrorScriptSnippet() {
173173
"def __rewriteMirrorUrl = '" + escapeGroovy(url) + "'\n" +
174174
"def __rewriteMirrorUser = '" + escapeGroovy(user) + "'\n" +
175175
"def __rewriteMirrorPass = '" + escapeGroovy(pass) + "'\n" +
176-
// Prepend Artifactory as a Maven Central proxy without removing any repository the
177-
// build (or its plugins) declared. Gradle tries repositories in order and falls
178-
// through on 404, so Central artifacts come from Artifactory while Plugin Portal-only
179-
// markers (Nebula, Develocity, etc.) still resolve from gradlePluginPortal().
180-
"def __rewriteConfigureMirror = { container ->\n" +
181-
" def existing = new ArrayList(container as Collection)\n" +
182-
" container.clear()\n" +
176+
// Prepend Artifactory as a Maven Central proxy by adding it through Gradle lifecycle
177+
// hooks that fire BEFORE the user's settings.gradle / build.gradle is evaluated.
178+
// Doing it this way avoids clearing the repository container (which doesn't survive
179+
// re-add) and avoids touching pluginManagement.repositories (where Gradle's default
180+
// gradlePluginPortal() injection happens lazily after our settingsEvaluated hook
181+
// would run). Gradle tries repositories in order and falls through on 404, so Central
182+
// artifacts come from Artifactory while Plugin Portal markers still resolve normally.
183+
"def __rewriteAddMirror = { container ->\n" +
183184
" container.maven {\n" +
184185
" url = __rewriteMirrorUrl\n" +
185186
" credentials {\n" +
186187
" username = __rewriteMirrorUser\n" +
187188
" password = __rewriteMirrorPass\n" +
188189
" }\n" +
189190
" }\n" +
190-
" existing.each { container.add(it) }\n" +
191191
"}\n" +
192-
"allprojects {\n" +
193-
" buildscript.repositories { __rewriteConfigureMirror(delegate) }\n" +
194-
" repositories { __rewriteConfigureMirror(delegate) }\n" +
195-
"}\n" +
196-
"settingsEvaluated { settings ->\n" +
197-
" settings.pluginManagement.repositories { __rewriteConfigureMirror(delegate) }\n" +
198-
" try { settings.dependencyResolutionManagement.repositories { __rewriteConfigureMirror(delegate) } } catch (Throwable ignored) {}\n" +
192+
"try {\n" +
193+
" gradle.beforeSettings { settings ->\n" +
194+
" try { __rewriteAddMirror(settings.dependencyResolutionManagement.repositories) } catch (Throwable ignored) {}\n" +
195+
" }\n" +
196+
"} catch (Throwable ignored) {}\n" +
197+
"gradle.beforeProject { project ->\n" +
198+
" __rewriteAddMirror(project.buildscript.repositories)\n" +
199+
" __rewriteAddMirror(project.repositories)\n" +
199200
"}\n";
200201
}
201202

0 commit comments

Comments
 (0)