OpenRewrite:在JUnit测试配方时启用本地Maven存储库?

huangapple go评论90阅读模式
英文:

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?

英文:

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?

答案1

得分: 0

因此,此注释被插入,因为OpenRewrite将尝试解析依赖项,但未能找到它。您可能希望添加一个显式的Maven存储库,类似于以下内容:

@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>"

(注意:上述内容是您提供的Java代码的一部分,不需要翻译。)

英文:

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 -&gt; spec
            .recipe(new UpgradeParentVersion(&quot;org.jenkins-ci.plugins&quot;, &quot;plugin&quot;, &quot;4.40&quot;, null, null))
            .executionContext(
              MavenExecutionContextView
                .view(new InMemoryExecutionContext())
                .setRepositories(List.of(
                  MavenRepository.builder().id(&quot;jenkins&quot;).uri(&quot;https://repo.jenkins-ci.org/public/&quot;).build()
                ))
            ),
          pomXml(
            &quot;&quot;&quot;
              &lt;project&gt;
                  &lt;parent&gt;

huangapple
  • 本文由 发表于 2023年6月29日 23:12:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582382.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定