I am currently using a sed script:
cd (root folder) first
find . -name pom.xml | xargs sed -i \"//,/\'<\\/dependencies>\'/s
Don't try to edit XML with sed
, it isn't made for this kind of structured data. sed
scripts that edit XML invariably break down when someone inserts benign whitespaces somewhere you didn't originally expect them, and nobody who edits XML expects things to break because of layout changes.
Instead, I'd use XSLT:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Identity template: just copy everything -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- special rule for version tags that include -SNAPSHOT and whose
parent tag has an artifactId subtag that contains scheduler-service -->
<xsl:template match="//version[contains(., '-SNAPSHOT') and not(contains(../artifactId, 'scheduler-service'))]">
<xsl:copy>
<!-- copy attributes -->
<xsl:apply-templates select="@*"/>
<!-- and only use the part of the node content before -SNAPSHOT -->
<xsl:value-of select="substring-before(., '-SNAPSHOT')"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Now you can use e.g.
xsltproc foobar.xsl pom.xml
or
xalan -in pom.xml -xsl foobar.xsl
depending on which XSLT processor you like, where foobar.xsl contains the above stylesheet.
By using XSLT
technologies:
STYLESHEET:
$ more pomConvertor.xslt
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:strip-space elements="*"/>
<!-- default behavior is to copy everything nodes and attribute-->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<!-- when you reach version node under dependency for which the artifactId does contain scheduler you copy the string before -SNAPSHOT -->
<xsl:template match="//dependencies/dependency/version[not(contains(../artifactId/text(),'scheduler'))]">
<version><xsl:value-of select="substring-before(text(),'-')"/></version>
</xsl:template>
</xsl:stylesheet>
INPUT:
$ more pom.xml
<pom>
<parent>
<groupId>com.techstack.scheduler</groupId>
<artifactId>scheduler-service</artifactId>
<version>0.0.9-SNAPSHOT</version>
</parent>
<artifactId>scheduler-webapp</artifactId>
<packaging>war</packaging>
<name>Scheduler Service Web Application</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>com.infor.techstack.scheduler</groupId>
<artifactId>scheduler-service-core</artifactId>
<version>0.0.9-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.security.authentication</groupId>
<artifactId>oauth10a-client</artifactId>
<version>0.0.26-SNAPSHOT</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>xerces</artifactId>
<groupId>xerces</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</pom>
OUTPUT:
$ xsltproc pomConvertor.xslt pom.xml
<?xml version="1.0"?>
<pom>
<parent>
<groupId>com.techstack.scheduler</groupId>
<artifactId>scheduler-service</artifactId>
<version>0.0.9-SNAPSHOT</version>
</parent>
<artifactId>scheduler-webapp</artifactId>
<packaging>war</packaging>
<name>Scheduler Service Web Application</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>com.infor.techstack.scheduler</groupId>
<artifactId>scheduler-service-core</artifactId>
<version>0.0.9-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.security.authentication</groupId>
<artifactId>oauth10a-client</artifactId>
<version>0.0.26</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>xerces</artifactId>
<groupId>xerces</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</pom>