Search code examples
javamavenopenrewrite

OpenRewrite: Enabling local Maven repository during JUnit testing of a recipe?


I wrote an OpenRewrite recipe which changes Maven pom.xml from containing this:

<dependency>
  <groupId>a</groupId>
  <artifactId>b</artifactId>
  <version>1.0.0</version>
</dependency>

To this:

<dependency>
  <groupId>c</groupId>
  <artifactId>d</artifactId>
  <version>@@@SNAPSHOT@@@</version>
</dependency>

When running the Rewrite Maven Plugin, everything works well, because 1.0.0 is in Maven Central, and @@@SNAPSHOT@@@ is in my local repo. The rewriting succeeds. However, when running the same recipe in a test environment using JUnit, resolution of the SNAPSHOT fails, inserting the following into the rewritten POM:

<!--~~(c:d:@@@SNAPSHOT@@@ failed. Unable to download POM. Tried repositories: https://repo.maven.apache.org/maven2: HTTP 404)~~>-->
<!--~~(c:d:@@@SNAPSHOT@@@ failed. Unable to download POM. Tried repositories: https://repo.maven.apache.org/maven2: Did not attempt to download because of a previous failure to retrieve from this repository.)~~>-->

If I had to guess, it is because in that environment, OpenRewrite does not look into the local repo. What can I do to make it find the SNAPSHOT?


Solution

  • So the comment is inserted because OpenRewrite will try to resolve the the dependency, and is failing to find it. You might want to add an explicit Maven Repository with something similar to this:

    https://github.com/openrewrite/rewrite/blob/ad60832bb60e6da996f395d4abb09eb43f298822/rewrite-maven/src/test/java/org/openrewrite/maven/UpgradeParentVersionTest.java#L66-L72

        @Test
        void nonMavenCentralRepository() {
            rewriteRun(
              spec -> spec
                .recipe(new UpgradeParentVersion("org.jenkins-ci.plugins", "plugin", "4.40", null, null))
                .executionContext(
                  MavenExecutionContextView
                    .view(new InMemoryExecutionContext())
                    .setRepositories(List.of(
                      MavenRepository.builder().id("jenkins").uri("https://repo.jenkins-ci.org/public/").build()
                    ))
                ),
              pomXml(
                """
                  <project>
                      <parent>