英文:
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:
@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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论