openrewrite

How to add "finalName" to a specific Maven pom.xml on a multi-module project?


I need to add a <finalName> tag to a specific Maven pom.xml on a multi-module project but I couldn't find any recipe that fits my needs.

As there is no Maven specific recipe for this, I tried the org.openrewrite.xml.AddOrUpdateChildTag recipe, but it added the tag to all pom.xml files.

Minimal reproducible example

Goal: Add <finalName> to the batch module

Project structure:

pom.xml - parent module
|- web/pom.xml - web module
|- batch/pom.xml - batch module

OpenRewrite recipe:

  - org.openrewrite.xml.AddOrUpdateChildTag:
      parentXPath: /project//build
      newChildTag: <finalName>batch</finalName>

Result:

pom.xml - with <finalName>
|- web/pom.xml - with <finalName>
|- batch/pom.xml - with <finalName>

What I want:

pom.xml
|- web/pom.xml
|- batch/pom.xml - with <finalName>

Is there any way to restrict the file I want the recipe to run?


Solution

  • Restricting your changes to a particular file is possible through a FindSourceFiles precondition: https://docs.openrewrite.org/reference/yaml-format-reference#preconditions

    That's a common request, such that we did not want to add a filePattern argument to any recipe, instead opting for preconditions as a generic mechanism. I hope that helps!