javamavenopenrewrite

How to add a <relative /> tag to parent POM?


In OpenRewrite you can add a comment to a POM like this. You can replace a dependency like this. But how do you add an element to an XML element such as:

<parent>
  <groupId>abc</groupId>
  <artifactId>def</artifactId>
  <version>123</version>
</parent>

Adding a <relativePath>:

<parent>
  <groupId>abc</groupId>
  <artifactId>def</artifactId>
  <version>123</version>
  <relativePath>./</relativePath>
</parent>

Is this possible?


Solution

  • You can add tags to specific paths using the AddOrUpdateChildTag recipe: https://docs.openrewrite.org/recipes/xml/addorupdatechildtag

    type: specs.openrewrite.org/v1beta/recipe
    name: com.yourorg.AddOrUpdateChildTagExample
    displayName: Add or update child tag example
    recipeList:
      - org.openrewrite.xml.AddOrUpdateChildTag:
          parentXPath: /project//plugin//configuration
          newChildTag: <skip>true</skip>
    

    That said I do share Karl Heinz' concerns about adding <relativePath>./</relativePath> in the comments above, so best to clarify if you were instead aiming to add for instance <relativePath>../</relativePath>, or <relativePath/> instead.