简体   繁体   English

脚本:如何定位特定的pom.xml文件并提取标记中的特定值

[英]Scripting: How to target specific pom.xml file and extract specific value in a tag

I am trying to accomplish the following tasks: 我正在尝试完成以下任务:

  1. Find all the files in the directories hierarchy called "pom.xml". 在名为“ pom.xml”的目录层次结构中找到所有文件。 (Do not show results) (不显示结果)
  2. Then I will identify those "pom.xml" with a parent/artifactId of "Maven_CAS_Parent" or "Maven_JAB_Parent". 然后,我将使用parent / artifactId为“ Maven_CAS_Parent”或“ Maven_JAB_Parent”的“ pom.xml”进行标识。 (I need the pom.xml path and the parent artifactId) (我需要pom.xml路径和父artifactId)
  3. After filtering, among in-scope "pom.xml"s, I try to extract the parent/version value into a text file called "check.txt". 过滤后,在范围内的“ pom.xml”中,我尝试将父级/版本值提取到名为“ check.txt”的文本文件中。 (Here I need the parent version) (这里我需要父版本)

One of the "pom.xml"s looks like this (simplified version): “ pom.xml”之一看起来像这样(简化版本):

<properties>
    <jiraKey>ORCAP</jiraKey>
    <commonName>${jiraKey} - CASDOP</commonName>
    <ear>CAS_ORCAP_EAR</ear>
    <war>CAS_ORCAP_WAR</war>
        <cas.version>1.4</cas.version>
        <cas.dep.version>1.3</cas.dep.version>
</properties>
<parent>
    <artifactId>Maven_CAS_Parent</artifactId>
    <groupId>csju.maven</groupId>
    <version>1.4.38</version>
</parent>
<modelVersion>4.0.0</modelVersion>
        <groupId>dgy.orcap</groupId>
        <artifactId>IHSB_ORCAP</artifactId>
        <version>1.0.2-SNAPSHOT</version>
<packaging>pom</packaging>
<name>${commonName}</name>
<description>none</description>
<url>${siteURL}${project.groupId}</url>

That being said, I want an output in this order: the path of the pom file, the parent artifcatId, the parent version. 话虽这么说,我想要按以下顺序输出:pom文件的路径,父artifcatId,父版本。 For example: 例如:

NSS-BUILD3-JOB1/pom.xml    Maven_CAS_Parent    1.4.38

I am pretty new to this but I got the following command. 我对此很陌生,但是得到了以下命令。 I know this command is not working and does not create a check.txt file, but how can I fix it or write a new one? 我知道此命令不起作用,并且不会创建check.txt文件,但是如何解决它或编写一个新文件呢?

#!/bin/bash
check()
{
echo -e 'setns x=http://maven.apache.org/POM/4.0.0\ncat /x:project/x:parent/x:version/text()' | xmllint --shell pom.xml | grep -v /
}

find . -type f -name pom.xml | xargs grep 'Maven_CAS_Parent\|Maven_JAB_Parent' | Check

You can do this by looping over the files found by find as shown in the example below: 您可以通过遍历find的文件来完成此操作,如下例所示:

while IFS= read -r file
do
    artifactId=$(xmllint --format --shell "$file" <<< "cat //parent/artifactId/text()" | grep -v /)
    if [[ $artifactId =~ ^(Maven_CAS_Parent|Maven_JAB_Parent)$ ]]
    then
        version=$(xmllint --format --shell "$file" <<< "cat //parent/version/text()" | grep -v /)
    else
        artifactId="Out of scope"
        version=""
    fi
    echo "$file $artifactId $version"    
done < <(find . -type f -name "pom.xml")

You will probably need to fix the xpaths to get them to work for you. 您可能需要修复xpath,以使其为您工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM