Skip to content

Support settings.xml in rewriteRun #5873

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

jevanlingen
Copy link
Contributor

What's changed?

The rewriteRun test runner now respects the local settings.xml file from the Maven configuration directory (~/.m2/settings.xml by default).

What's your motivation?

The settings.xml file is often used to define mirrors, which can redirect all artifact requests to an internal repository manager such as Artifactory. For example:

<mirrors>
  <mirror>
    <id>artifactory</id>
    <mirrorOf>*</mirrorOf>
    <url>https://my.own.artifactory.com</url>
  </mirror>
</mirrors>

For some company environments, direct access to external repositories such as https://repo.maven.apache.org/maven2 is blocked (often returning a 403). Without support for settings.xml, rewriteRun tests would fail in such environments because dependencies could not be resolved.

Additionally, this change supports scenarios where mirrors are used to discard certain repositories, for example:

<mirror>
  <id>skip-sonatype-nexus-snapshots</id>
  <mirrorOf>sonatype-nexus-snapshots</mirrorOf>
  <url>http://nonexistent.invalid</url>
</mirror>

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

@jevanlingen jevanlingen self-assigned this Aug 7, 2025
@jevanlingen jevanlingen added the enhancement New feature or request label Aug 7, 2025
@jevanlingen jevanlingen added the recipe Requested Recipe label Aug 7, 2025
@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Aug 7, 2025
@jevanlingen jevanlingen moved this from In Progress to Ready to Review in OpenRewrite Aug 7, 2025
github-actions[bot]

This comment was marked as spam.

@@ -139,6 +140,9 @@ private MavenPomDownloader(Map<Path, Pom> projectPoms, HttpSender httpSender, Ex
this.projectPomsByGav = projectPomsByGav(projectPoms);
this.httpSender = httpSender;
this.ctx = MavenExecutionContextView.view(ctx);
if (ctx.getMessage(MAVEN_SETTINGS_LOAD_FROM_DISK, false)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I now made it work with this flag. You could argue though, if there is no maven-settings available in the context, you always want to read the maven settings from local. What do you guys think?

@@ -378,6 +379,7 @@ default void rewriteRun(Consumer<RecipeSpec> spec, SourceSpec<?>... sourceSpecs)
recipeCtx = CursorValidatingExecutionContextView.view(recipeCtx)
.setValidateCursorAcyclic(TypeValidation.before(testMethodSpec, testClassSpec).cursorAcyclic())
.setValidateImmutableExecutionContext(TypeValidation.before(testMethodSpec, testClassSpec).immutableExecutionContext());
recipeCtx.putMessage(MAVEN_SETTINGS_LOAD_FROM_DISK, true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we also have to set this in the gradle/maven plugins or do they already do MavenSettings.readMavenSettingsFromDisk(ctx)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess they don't have to, because these plugins will just start running the tests. The tests themselves call the rewriteRun functions, which in turn will just set the MAVEN_SETTINGS_LOAD_FROM_DISK property. With other words, it does not really matter how/which build tool starts the tests, as you don't need any configuration from outside the scope of the tests.

github-actions[bot]

This comment was marked as spam.

@timtebeek timtebeek self-requested a review August 7, 2025 15:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request recipe Requested Recipe
Projects
Status: Ready to Review
Development

Successfully merging this pull request may close these issues.

2 participants