0

I want to replace the version number of multiple pom.xml files of a specific dependency.

As I understood:

sed is meant for replacing a string in one line and I have difficulties replacing the next line.

awk is intended for multiple lines. however I have difficulties replacing the text while using it with find. So far I came up with this solution but I don't know how to replace the string in the file? Is awk the right tool anyway for my purposes?

$ find . -name "pom.xml" -exec \
awk -v artifactId="junit" -v RS="<dependency>" '
    $0 ~ "<artifactId>" artifactId "</artifactId>" {
        sub("<version>.*</version>", "<version>NEW-VERSION</version>")
    }
    { printf "%s", $0 RS }
' {} \;

Input are typical pom.xml files:

...
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>VARIOUS DIFFERENT VERSION NUMBERS HERE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.6</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
...

Expected Output:

...
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>NEW-VERSION</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.6</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
...

I left Eds edit, since it made the query more readable and I leave the question despite Williams comment, that this is not the proper tool for version management.

Alv123
  • 431
  • 6
  • 18
  • [Don't Parse XML/HTML With Regex.](https://stackoverflow.com/a/1732454/3776858) I suggest to use an XML/HTML parser (xmlstarlet, xmllint ...). – Cyrus Jun 09 '21 at 15:55
  • Sounds like: https://stackoverflow.com/q/5726291/10971581 or https://stackoverflow.com/q/2341906/10971581 – jhnc Jun 09 '21 at 16:00
  • @jhnc unfortunately these are all different projects. – Alv123 Jun 10 '21 at 06:45
  • 1
    It's always important to format you code with useful indentation and other white space that emphasizes/supports the control flow of the program, especially when asking others to read it. See my recent edit for one way to lay out your code legibly. – Ed Morton Jun 10 '21 at 15:15
  • Please [edit] your question to include a [mcve] with concise, testable sample input and expected output so we can help you. See [ask] if that's not clear. – Ed Morton Jun 10 '21 at 15:17
  • It truly saddens me that this sort of thing still happens. Version numbers should *never* be embedded in files in the VCS. That this is so common makes me sad for the future of humanity. Tag the vcs. Derive the version from the tag. Anything else is insanity. – William Pursell Jun 10 '21 at 15:58
  • @WilliamPursell Thanks for the lesson, I didn't think about it, just wanted to do it quickly. I will leave the question as it is, for curiosity of achieving the task with shell tools and in the meanwhile will research on your suggestion. – Alv123 Jun 14 '21 at 11:36
  • @EdMorton Thank you, you were right. Being in a rush certainly did not help. I will leave the question despite William's hint and reformat it since I am still curios on how to achieve this task, even though it is not the best practice tool to do it. – Alv123 Jun 14 '21 at 11:37

1 Answers1

1

I don't understand why you don't know how to do this as all you have to do for versionId is exactly the same as you did for artifactId.

  1. Add -v versionId="NEW-VERSION" in the call to awk exactly like you have -v artifactId="junit", and
  2. Use that versionId in the sub replacement as "<version>" versionId "</version>" exactly like you used artifactIdin the condition before it "<artifactId>" artifactId "</artifactId>".
awk -v artifactId="junit" -v versionId="NEW-VERSION" -v RS="<dependency>" '
    $0 ~ "<artifactId>" artifactId "</artifactId>" {
        sub("<version>.*</version>", "<version>" versionId "</version>")
    }
    { printf "%s", $0 RS }
' pom.xml
...
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>NEW-VERSION</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.6</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
...
Ed Morton
  • 188,023
  • 17
  • 78
  • 185