openrewrite / openrewrite/rewrite

`ModuleHasDependency` or `FindDependency` precondition fails to add dependency in Maven submodule

Open
#6,468 12 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug maven recipe
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.