简体   繁体   中英

Read a property from a maven POM in a bash script (Linux)

I am using maven as my build tool. Below is a snippet from my POM.

    <properties>
        <geb.version>2.2</geb.version>
        <selenium.version>3.14.0</selenium.version>
        <groovy.version>2.4.14</groovy.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spock.version>1.1-groovy-2.4</spock.version>
        <surefire.plugin.version>2.20</surefire.plugin.version>
        <surefire.plugin.parallel>methods</surefire.plugin.parallel>
        <selenium.host>XXXXX</selenium.host>
    </properties>

I am writing an linux executable and I want to access <selenium.host> property in my bash script. How do I do this?

DEFAULT_ADDRESS=$(get address from POM here)

Please note that my script and POM file are in the same directory.

Cheers!

The best and reliable solution would be to use maven-help-plugin like this:

ADDRESS=$(mvn help:evaluate -Dexpression=selenium.host -q -DforceStdout)

If you don't have specified the version of maven-help-plugin in your pom file you should use the following:

ADDRESS=$(mvn org.apache.maven.plugins:maven-help-plugin:3.2.0:evaluate -Dexpression=selenium.host -q -DforceStdout)

The result is that the given property is read from pom file and then the value is assigned to ADDRESS

You can do it like that:

DEFAULT_ADDRESS=`cat pom.xml | grep "selenium.host" | cut -d'>' -f2 | cut -d'<' -f1`

You can use xmllint to parse xml from the command line:

address=$(xmllint --xpath "string(//properties/selenium.host)" file.xml)
echo "$address"

Or if xmllint is not available, and tag <selenium.host> only appears once:

grep -oP '(?<=<selenium.host>).*(?=</selenium.host>)' file.xml

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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