Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

bash

maven

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!

like image 580
Archit Arora Avatar asked Jan 27 '23 00:01

Archit Arora


2 Answers

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

like image 143
khmarbaise Avatar answered Feb 05 '23 16:02

khmarbaise


You can do it like that:

DEFAULT_ADDRESS=`cat pom.xml | grep "selenium.host" | cut -d'>' -f2 | cut -d'<' -f1`
like image 44
Bartosz Wardziński Avatar answered Feb 05 '23 17:02

Bartosz Wardziński