![](/img/trans.png)
[英]How to extract the artifactId from all pom.xml files using in a multimodule project (using shell script)?
[英]How to extract the GAV from a pom.xml file in a shell script
我需要從大量的pom.xml
文件中提取Maven GAV(groupId,artifactId,version)。 並非所有POM都具有父POM聲明,因此需要考慮父GAV和項目GAV之間的繼承。
我只想使用可以在linux shell中輕松編寫腳本的工具,例如bash。
我能找到的最佳解決方案是使用XSL轉換。 使用以下內容創建文件extract-gav.xsl
:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:pom="http://maven.apache.org/POM/4.0.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/pom:project">
<!-- this XML element just serves as a bracket and may be omitted -->
<xsl:element name="artifact">
<xsl:text> </xsl:text>
<!-- process coordinates declared at project and project/parent -->
<xsl:apply-templates select="pom:groupId|pom:parent/pom:groupId" mode="copy-coordinate"/>
<xsl:apply-templates select="pom:artifactId|pom:parent/pom:artifactId" mode="copy-coordinate"/>
<xsl:apply-templates select="pom:version|pom:parent/pom:version" mode="copy-coordinate"/>
</xsl:element>
</xsl:template>
<xsl:template match="*" mode="copy-coordinate">
<!-- omit parent coordinate if same coordinate is explicitly specified on project level -->
<xsl:if test="not(../../*[name(.)=name(current())])">
<!-- write coordinate as XML element without namespace declarations -->
<xsl:element name="{local-name()}">
<xsl:value-of select="."/>
</xsl:element>
<xsl:text> </xsl:text>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
然后可以在shell中調用此轉換(假設您已安裝libxslt),並使用命令xsltproc extract-gav.xsl pom.xml
這將以以下格式生成輸出:
<artifact>
<groupId>org.example.group</groupId>
<artifactId>example-artifact</artifactId>
<version>1.2.0</version>
</artifact>
如果您需要不同的格式,XSL轉換應該很容易適應,以便它適合您的需要。 例如,以下轉換將GAV寫為制表符分隔的純文本:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:pom="http://maven.apache.org/POM/4.0.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/pom:project">
<!-- process coordinates declared at project and project/parent -->
<xsl:apply-templates select="pom:groupId|pom:parent/pom:groupId" mode="copy-coordinate"/>
<xsl:apply-templates select="pom:artifactId|pom:parent/pom:artifactId" mode="copy-coordinate"/>
<xsl:apply-templates select="pom:version|pom:parent/pom:version" mode="copy-coordinate"/>
<xsl:text> </xsl:text>
</xsl:template>
<xsl:template match="*" mode="copy-coordinate">
<xsl:if test="not(../../*[name(.)=name(current())])">
<xsl:value-of select="."/>
<xsl:text>	</xsl:text>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
grep -v '\[' <( mvn help:evaluate -Dexpression="project.groupId" 2>/dev/null &&
mvn help:evaluate -Dexpression="project.artifactId" 2>/dev/null &&
mvn help:evaluate -Dexpression="project.version" 2>/dev/null )
我使用了一個名為pom的groovy腳本,我把它放在了PATH上。 它看起來像這樣:
#!/usr/bin/env groovy
def cli = new CliBuilder(usage:'pom')
cli.h('print usage')
cli.n('do not auto-print output')
cli.p(args:1, argName:'pom', 'the POM file to use')
def options = cli.parse(args)
def arguments = options.arguments()
if (options.h || arguments.size() == 0 ) {
println cli.usage()
} else {
def fileName = options.p ? options.p : "pom.xml"
def script = arguments[0]
def output = Eval.x(new XmlSlurper().parse(new File(fileName)), "x.${script}")
if (!options.n) println output
}
現在您可以提取如下值:
pom version
pom groupId
pom 'properties."project.build.sourceEncoding"'
pom -n 'modules.module.each { println it }'
pom -n 'dependencyManagement.dependencies.dependency.each { \
println "${it.groupId}:${it.artifactId}:${it.version}" \
}'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.