【问题标题】:Bash script to get the outputDirectory tag value of a fileset from Maven assembly.xml用于从 Maven assembly.xml 获取文件集的 outputDirectory 标记值的 Bash 脚本
【发布时间】:2020-05-26 12:36:55
【问题描述】:

我需要一个 Bash 脚本来从 Maven assembly.xml 中获取文件集下 outputDirectory 标记的值,这样我就可以处理创建的文件了。

我不是指开始标签和结束标签之间的实际字符集,而是标签为空时的默认值或该标签中存在变量时的值。

我创建了一个带有不同 fileSet 标签的程序集 xml,只是为了显示我需要处理的各种情况。我想要一个脚本,它会给我 Maven 看到的输出目录的路径

<assembly>
    <id>${deploy.name}</id>
    <formats>
        <format>zip</format>
    </formats>
    <fileSets>
        <fileSet>
            <outputDirectory></outputDirectory>
        </fileSet>    
        <fileSet>
            <outputDirectory>/</outputDirectory>
        </fileSet>
        <fileSet>
            <outputDirectory>/examples</outputDirectory>
        </fileSet>
        <fileSet>
            <outputDirectory>${Common.shared.location}</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

更新: Maven 将创建的文件保存在输出目录中。 outputDirectory 的基本目录默认是 ${project.build.directory} 的值,这里有很好的解释 Maven: specify the outputDirectory only for packaging a jar?

我在脚本中有一个变量,我希望它拥有 Maven 将保存输出文件的文件夹的路径。在下面的每个示例 outputDirectory 标记之后,有一行我认为应该是我想要的变量的值。

${project.build.directory}的值

/ 我不确定

/例子 ${project.build.directory}/examples 的值

${Common.shared.location} 取决于 ${Common.shared.location} 变量的值

谢谢

【问题讨论】:

  • 你能举一个期望输出的例子吗?你可以添加一个示例 assembly.xml 文件(如果它包含私人信息,则匿名)?你能展示一下你到目前为止所做的事情吗?

标签: bash maven maven-assembly-plugin


【解决方案1】:

您可以将sed 与正则表达式一起使用:

for match in $(sed -n 's|<outputDirectory>\(.*\)</outputDirectory>|\1|p' assembly.xml) ; do
  echo "$match"
done

说明:

sed 's|...|...|p' 打印正则表达式 &lt;outputDirectory&gt;\(.*\)&lt;/outputDirectory&gt; 的匹配项,它匹配两个 &lt;outputDirectory&gt; 标记之间的任何文本。 for match in ... 遍历匹配项并将它们打印到标准输出。

【讨论】:

  • 谢谢。但我希望有一个通用的解决方案可以给我 outputDirectory 标签的值,无论它是一个变量的普通值(在这种情况下,我想要参数的 value 。用于查找标签值我使用以下字符串:TAG_VALUE=echo -e 'setns x=http://maven.apache.org/POM/4.0.0\ncat /x:project/x:properties/x:neededTag/text()' | xmllint --shell ../folder/pom.xml | grep -v /
  • @Avi 你能在你的问题中添加一个预期输出的例子吗?我不确定我是否理解
猜你喜欢
  • 1970-01-01
  • 2018-07-03
  • 2017-10-03
  • 1970-01-01
  • 2021-03-20
  • 2019-02-10
  • 2013-11-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多