Home > database >  How to extract the artrifactId in the pom.xml using Powershell that are not in the parent,build or d
How to extract the artrifactId in the pom.xml using Powershell that are not in the parent,build or d

Time:06-24

I have the below pom.xml file and i want to read only below artifact data and ignore the artifact info that are in between the and tags using powershell regex match

**<groupId>com.frt</groupId>
<artifactId>documents-v1</artifactId>
<version>2.0.3</version>
<packaging>application</packaging>
<name>documents</name>
<description>To raise a client request </description**>
    <modelVersion>4.5.0</modelVersion>
    <parent>
        <groupId>com.frt</groupId>
        <artifactId>parent-pom</artifactId>
        <version>2.0.0</version>
    </parent>

    <groupId>com.frt</groupId>
    <artifactId>documents-v1</artifactId>
    <version>2.0.3</version>
    <packaging>application</packaging>
    <name>documents</name>
    <description>To raise a client request </description>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-clean-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.tools.maven</groupId>
                <artifactId>maven-plugin</artifactId>
                <configuration>
                    <classifier>application</classifier>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.munit.tools</groupId>
                <artifactId>munit-maven-plugin</artifactId>
                <configuration>
                    <runtimeProduct>4.5.0</runtimeProduct>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>9072cf0-111fc2b0fae6</groupId>
            <artifactId>json-logger</artifactId>
            <classifier>mulr-plugin</classifier>
        </dependency>
        <dependency>
            <groupId>org.mul2.connectors</groupId>
            <artifactId>mul2-http-connector</artifactId>
            <classifier>mul2-plugin</classifier>
        </dependency>

Below is what i tried and its giving all the artifact names, i am not sure how to get the way i want it

Get-Content pom.xml -Raw | % { [regex]::matches($_ , '<artifactId>([\s\S]*?)</artifactId>')} |% {$_.Groups[1].Value}

Any help would be appreciated

CodePudding user response:

Regex is a last resort. PowerShell can parse XML for you!

Try this instead:

$x = [xml](gc pom.xml)

This^ failed for me because I assume you did not include the entire contents of the XML file in your question.

$x | FL

After here you would find your property and next "dot" into it. If build was the property then...

$x.build | fl 

Rinse and repeat

  • Related