Pull version in pom.xml

I want to add the Jenkins build number to my file pom.xml. I can do this with the Maven Version plugin , but first I need the original version from the file pom.xml.

For this, I use the command xmllint --xpath '/project/version/text()' pom.xml. However, I keep gettingXPath set is empty

Here's an example file pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <artifactId>das-artifact</artifactId>
  <name>das-name</name>
  <version>4.2</version>
</project>

And here is my conclusion:

$ xmllint --nsclean --xpath '/project/version/text()' pom.xml
XPath set is empty
$

However, if I change the item xmlnsto fooxmlns, it works:

Here's an example file pom.xml:

<project fooxmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <artifactId>das-artifact</artifactId>
  <name>das-name</name>
  <version>4.2</version>
</project>

And here is my conclusion:

$ xmllint --nsclean --xpath '/project/version/text()' pom.xml
4.2$

I am not too familiar with everything that can be done xmllint. Can I make it ignore the namespace attribute xmlns?

+4
source share
4 answers

, , @A_Di-Matteo ( Maven 3.3.9 3.4.0, ), build-helper-maven-plugin, :

 mvn build-helper:parse-version versions:set \
     -DnewVersion=\${parsedVersion.majorVersion}.\${parsedVersion.minorVersion}.\${parsedVersion.incrementalVersion}-WhatEverYouLikeToAdd \
     versions:commit
+3

local-name() xml- node.

Old-XPath: /project/version/text()
New-XPath: /*[local-name()="project"]/*[local-name()="version"]/text()

, bash,

$ xmllint --xpath '/*[local-name()="project"]/*[local-name()="version"]/text()' pom.xml
1.0.0-SNAPSHOT
+1

:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.das-group</groupId>
    <artifactId>das-artifact</artifactId>
    <version>1.1.0-123</version>
    <profiles>
        <profile>
            <id>arse-version</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>build-helper-maven-plugin</artifactId>
                        <version>1.12</version>
                        <executions>
                            <execution>
                                <id>parse-version</id>
                                <goals>
                                    <goal>parse-version</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>versions-maven-plugin</artifactId>
                        <version>2.3</version>
                        <executions>
                            <execution>
                                <phase>validate</phase>
                                <id>set-version</id>
                                <goals>
                                    <goal>set</goal>
                                    <goal>commit</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <newVersion>${parsedVersion.osgiVersion}-${BUILD_NUMBER}</newVersion>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

arse-version ( pom.xml, ), build-helper-maven-plugin versions-maven-plugin. validate.

, , Jenkins, :

$ mvn -Parse-version -DBUILD_NUMBER=$BUILD_NUMBER validate

Jenkinsfile. Python script. , xmllint xmllint ( - Mac Linux).

, , , . 1.0, № 23, 1.0.0-23. 1.2-3 , I get 1.2.3-23`. , , .

. khmarbaise .

. commit. pom.xml .

0

:

@djeikyb xml - xmllint, xpath - , :

sed < pom.xml '2 s/xmlns=".*"//g' | xmllint --xpath '/project/version/text()' - 2>/dev/null

- , xml, , , , , , kludge.....

0

Source: https://habr.com/ru/post/1651548/


All Articles