0
<plugins>
        <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <check>
                        <haltOnFailure>false</haltOnFailure>
                        <totalLineRate>85</totalLineRate>
                    </check>
                    <formats>
                        <format>html</format>
                        <format>xml</format>
                    </formats>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>cobertura</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
    </plugins>
</build>
<reporting>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-report-plugin</artifactId>
        <version>3.0.0-M4</version>
        <configuration>

                    <formats>

                        <format>xml</format>
                    </formats>
                </configuration>
      </plugin>

need to check whether the "format" tag with value "xml" is given in pom file for cobertura plugin. tried to parse the xml file in bash

temp= $(grep -n -oP '(?<=<format>)[^<]+' pom.xml)

this returns all the value with tag including html, xml. How to parse this file and validate?

Arun AV
  • 86
  • 4
  • Does this answers your question: https://stackoverflow.com/questions/893585/how-to-parse-xml-in-bash – Santosh Garole May 04 '20 at 10:38
  • Does this answer your question? [How to parse XML in Bash?](https://stackoverflow.com/questions/893585/how-to-parse-xml-in-bash) – KamilCuk May 04 '20 at 10:41
  • 5
    [Don't Parse XML/HTML With Regex.](https://stackoverflow.com/a/1732454/3776858) I suggest to use an XML/HTML parser (xmlstarlet, xmllint ...). – Cyrus May 04 '20 at 10:49

1 Answers1

0

Your bash script almost does it! I would just recommend one change:

temp=$(grep -noP '(?<=<format>)[^<]+' pom.xml | grep -om 1 xml)

The change I made is to use a pipe* to an additional grep. Piping is pretty useful and lets you combine commands to do a lot of cool stuff!

What I would really do in a bash script is use an if statement though:

if grep -qom 1 '<format>xml</format>' pom.xml
then
    echo "pom.xml contains xml format tag"
fi
Lenna
  • 1,220
  • 6
  • 22