openrewrite / openrewrite/rewrite
`ModuleHasDependency` or `FindDependency` precondition fails to add dependency in Maven submodule
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 3.7k
- Forks
- 570
- Avg merge
- 13h 12m
- Merged PRs (30d)
- 261
Description
What version of OpenRewrite are you using?
- OpenRewrite 8.70.2
- Maven plugin 6.26.0
How are you running OpenRewrite?
Maven plugin
Can you share your configuration so that we can rule out any configuration issues?
Running from the commandline:
mvn org.openrewrite.maven:rewrite-maven-plugin:run \
-Drewrite.configLocation=src/main/resources/test.yaml \
-Drewrite.activeRecipes=com.xxx.Flyway
With test.yaml being:
---
type: specs.openrewrite.org/v1beta/recipe
name: com.xxx.Flyway
displayName: Perform Flyway migrations
description: Perform Flyway migrations.
preconditions:
- org.openrewrite.java.dependencies.search.ModuleHasDependency:
groupIdPattern: org.flywaydb
artifactIdPattern: flyway-database-*
recipeList:
- org.openrewrite.java.dependencies.AddDependency:
groupId: org.springframework.boot
artifactId: spring-boot-starter-flyway
version: 4.0.x
What is the smallest, simplest way to reproduce the problem?
See above.
What did you expect to see?
In my multi-module project, I expect the dependency org.flywaydb:flyway-database-postgresql to trigger adding a new dependency org.springframework.boot:spring-boot-starter-flyway
What did you see instead?
No change was made in my multi-module project where the dependency is in a submodule (that is, not the parent pom.xml.
Removing the precondition and the change WAS made.
Moving the dependency from the submodule to the parent pom.xml and the change WAS made.
Note that I also tried using org.openrewrite.maven.search.FindDependency and I experience exactly the same behavior.
I also tried putting this in a unit test, but the test works and does not reflect what I'm seeing when running from the commandline in my actual project:
@Test
void preconditionMultiModuleFindDependency() {
rewriteRun(spec -> spec.recipeFromYaml("""
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.PreconditionTest
description: Test.
preconditions:
- org.openrewrite.maven.search.FindDependency:
groupId: jakarta.activation
artifactId: jakarta.activation-api
recipeList:
- org.openrewrite.java.dependencies.AddDependency:
groupId: org.springframework.boot
artifactId: spring-boot-starter-flyway
version: 4.0.x
""", "org.openrewrite.PreconditionTest"),
pomXml(
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.sample</groupId>
<artifactId>sample</artifactId>
<version>1.0.0</version>
<modules>
<module>sample-module</module>
</modules>
</project>
"""
,
sourceSpecs -> sourceSpecs.path("pom.xml")),
pomXml(
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.sample</groupId>
<artifactId>sample-module</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
<version>2.1.2</version>
</dependency>
</dependencies>
</project>
""",
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.sample</groupId>
<artifactId>sample-module</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-flyway</artifactId>
<version>4.0.1</version>
</dependency>
</dependencies>
</project>
"""
,
sourceSpecs -> sourceSpecs.path("sample-module/pom.xml"))
);
}
If this doesn't immediately ring a bell then I'll create a demo project.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with ModuleHasDependency and FindDependency, then compare their multi-module behavior in the Maven plugin command shown in the issue with the preconditionMultiModuleFindDependency test. Run that test and the command-line reproduction; done means a dependency in a submodule causes AddDependency to add the expected dependency.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- build-system
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100