How to get Maven project version to the bash command line











up vote
140
down vote

favorite
37












Previous I issued a question on how to change Maven project vesion from command line which lead me to a new issue.



Previously I was able to get the version number since the version was stored as a property that was easy to grep and parse from the command line (bash). Now that the pom.xml element is used for this, it no longer is unique since all the dependencies and maybe some others too use this. I think there is no way to get the current version number with a bash script without external tools for parsing xml or some very context-aware sed command.



The most clean solution in my opinnion would be for Maven to hand out this version information. I was thinking of writing a custom maven plugin for retrieving different properties but I thought I'd ask here first.



So, is there any easy way to get the value of ${project.version} to the command line? Thanks in advance.



Solution



Thank you for the help. I had to cd to the directory manually but that can be done easily. In my bash script I have



version=`cd $project_loc && mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | sed -n -e '/^[.*]/ !{ /^[0-9]/ { p; q } }'`


Which gives me the current version that I can then advance. Grepping might be simplier but I thought I'd like as robust as possible, so I'm satisfied with the first line that starts with a number and try to handle this as a version number.



# Advances the last number of the given version string by one.
function advance_version () {
local v=$1
# Get the last number. First remove any suffixes (such as '-SNAPSHOT').
local cleaned=`echo $v | sed -e 's/[^0-9][^0-9]*$//'`
local last_num=`echo $cleaned | sed -e 's/[0-9]*.//g'`
local next_num=$(($last_num+1))
# Finally replace the last number in version string with the new one.
echo $v | sed -e "s/[0-9][0-9]*([^0-9]*)$/$next_num/"
}


And I use this by simply calling



new_version=$(advance_version $version)


Hope this helps someone.










share|improve this question




















  • 1




    The solution seems to miss a ' to finish the sed expression.
    – nawroth
    Oct 14 '11 at 12:48










  • Which sed exp? I can't seem to notice.
    – mkko
    Oct 14 '11 at 16:17






  • 1




    make that a mvn -o for faster execution
    – Nathan Bubna
    Nov 30 '12 at 22:51






  • 2




    You can replace that complex sed expression with a simple grep -e '^[[:digit:]]'
    – bpedman
    Sep 14 '13 at 4:14








  • 2




    If you use -o as recommended by @NathanBubna then that will put maven in 'offline' mode. If you don't already have the maven help plugin and dependent jars downloaded then the build will fail. Got burned by that for a bit, hope it helps somebody else.
    – phillipuniverse
    Feb 8 '14 at 22:53















up vote
140
down vote

favorite
37












Previous I issued a question on how to change Maven project vesion from command line which lead me to a new issue.



Previously I was able to get the version number since the version was stored as a property that was easy to grep and parse from the command line (bash). Now that the pom.xml element is used for this, it no longer is unique since all the dependencies and maybe some others too use this. I think there is no way to get the current version number with a bash script without external tools for parsing xml or some very context-aware sed command.



The most clean solution in my opinnion would be for Maven to hand out this version information. I was thinking of writing a custom maven plugin for retrieving different properties but I thought I'd ask here first.



So, is there any easy way to get the value of ${project.version} to the command line? Thanks in advance.



Solution



Thank you for the help. I had to cd to the directory manually but that can be done easily. In my bash script I have



version=`cd $project_loc && mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | sed -n -e '/^[.*]/ !{ /^[0-9]/ { p; q } }'`


Which gives me the current version that I can then advance. Grepping might be simplier but I thought I'd like as robust as possible, so I'm satisfied with the first line that starts with a number and try to handle this as a version number.



# Advances the last number of the given version string by one.
function advance_version () {
local v=$1
# Get the last number. First remove any suffixes (such as '-SNAPSHOT').
local cleaned=`echo $v | sed -e 's/[^0-9][^0-9]*$//'`
local last_num=`echo $cleaned | sed -e 's/[0-9]*.//g'`
local next_num=$(($last_num+1))
# Finally replace the last number in version string with the new one.
echo $v | sed -e "s/[0-9][0-9]*([^0-9]*)$/$next_num/"
}


And I use this by simply calling



new_version=$(advance_version $version)


Hope this helps someone.










share|improve this question




















  • 1




    The solution seems to miss a ' to finish the sed expression.
    – nawroth
    Oct 14 '11 at 12:48










  • Which sed exp? I can't seem to notice.
    – mkko
    Oct 14 '11 at 16:17






  • 1




    make that a mvn -o for faster execution
    – Nathan Bubna
    Nov 30 '12 at 22:51






  • 2




    You can replace that complex sed expression with a simple grep -e '^[[:digit:]]'
    – bpedman
    Sep 14 '13 at 4:14








  • 2




    If you use -o as recommended by @NathanBubna then that will put maven in 'offline' mode. If you don't already have the maven help plugin and dependent jars downloaded then the build will fail. Got burned by that for a bit, hope it helps somebody else.
    – phillipuniverse
    Feb 8 '14 at 22:53













up vote
140
down vote

favorite
37









up vote
140
down vote

favorite
37






37





Previous I issued a question on how to change Maven project vesion from command line which lead me to a new issue.



Previously I was able to get the version number since the version was stored as a property that was easy to grep and parse from the command line (bash). Now that the pom.xml element is used for this, it no longer is unique since all the dependencies and maybe some others too use this. I think there is no way to get the current version number with a bash script without external tools for parsing xml or some very context-aware sed command.



The most clean solution in my opinnion would be for Maven to hand out this version information. I was thinking of writing a custom maven plugin for retrieving different properties but I thought I'd ask here first.



So, is there any easy way to get the value of ${project.version} to the command line? Thanks in advance.



Solution



Thank you for the help. I had to cd to the directory manually but that can be done easily. In my bash script I have



version=`cd $project_loc && mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | sed -n -e '/^[.*]/ !{ /^[0-9]/ { p; q } }'`


Which gives me the current version that I can then advance. Grepping might be simplier but I thought I'd like as robust as possible, so I'm satisfied with the first line that starts with a number and try to handle this as a version number.



# Advances the last number of the given version string by one.
function advance_version () {
local v=$1
# Get the last number. First remove any suffixes (such as '-SNAPSHOT').
local cleaned=`echo $v | sed -e 's/[^0-9][^0-9]*$//'`
local last_num=`echo $cleaned | sed -e 's/[0-9]*.//g'`
local next_num=$(($last_num+1))
# Finally replace the last number in version string with the new one.
echo $v | sed -e "s/[0-9][0-9]*([^0-9]*)$/$next_num/"
}


And I use this by simply calling



new_version=$(advance_version $version)


Hope this helps someone.










share|improve this question















Previous I issued a question on how to change Maven project vesion from command line which lead me to a new issue.



Previously I was able to get the version number since the version was stored as a property that was easy to grep and parse from the command line (bash). Now that the pom.xml element is used for this, it no longer is unique since all the dependencies and maybe some others too use this. I think there is no way to get the current version number with a bash script without external tools for parsing xml or some very context-aware sed command.



The most clean solution in my opinnion would be for Maven to hand out this version information. I was thinking of writing a custom maven plugin for retrieving different properties but I thought I'd ask here first.



So, is there any easy way to get the value of ${project.version} to the command line? Thanks in advance.



Solution



Thank you for the help. I had to cd to the directory manually but that can be done easily. In my bash script I have



version=`cd $project_loc && mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | sed -n -e '/^[.*]/ !{ /^[0-9]/ { p; q } }'`


Which gives me the current version that I can then advance. Grepping might be simplier but I thought I'd like as robust as possible, so I'm satisfied with the first line that starts with a number and try to handle this as a version number.



# Advances the last number of the given version string by one.
function advance_version () {
local v=$1
# Get the last number. First remove any suffixes (such as '-SNAPSHOT').
local cleaned=`echo $v | sed -e 's/[^0-9][^0-9]*$//'`
local last_num=`echo $cleaned | sed -e 's/[0-9]*.//g'`
local next_num=$(($last_num+1))
# Finally replace the last number in version string with the new one.
echo $v | sed -e "s/[0-9][0-9]*([^0-9]*)$/$next_num/"
}


And I use this by simply calling



new_version=$(advance_version $version)


Hope this helps someone.







maven-2 command-line






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 23 '17 at 12:18









Community

11




11










asked Aug 23 '10 at 6:53









mkko

1,80232127




1,80232127








  • 1




    The solution seems to miss a ' to finish the sed expression.
    – nawroth
    Oct 14 '11 at 12:48










  • Which sed exp? I can't seem to notice.
    – mkko
    Oct 14 '11 at 16:17






  • 1




    make that a mvn -o for faster execution
    – Nathan Bubna
    Nov 30 '12 at 22:51






  • 2




    You can replace that complex sed expression with a simple grep -e '^[[:digit:]]'
    – bpedman
    Sep 14 '13 at 4:14








  • 2




    If you use -o as recommended by @NathanBubna then that will put maven in 'offline' mode. If you don't already have the maven help plugin and dependent jars downloaded then the build will fail. Got burned by that for a bit, hope it helps somebody else.
    – phillipuniverse
    Feb 8 '14 at 22:53














  • 1




    The solution seems to miss a ' to finish the sed expression.
    – nawroth
    Oct 14 '11 at 12:48










  • Which sed exp? I can't seem to notice.
    – mkko
    Oct 14 '11 at 16:17






  • 1




    make that a mvn -o for faster execution
    – Nathan Bubna
    Nov 30 '12 at 22:51






  • 2




    You can replace that complex sed expression with a simple grep -e '^[[:digit:]]'
    – bpedman
    Sep 14 '13 at 4:14








  • 2




    If you use -o as recommended by @NathanBubna then that will put maven in 'offline' mode. If you don't already have the maven help plugin and dependent jars downloaded then the build will fail. Got burned by that for a bit, hope it helps somebody else.
    – phillipuniverse
    Feb 8 '14 at 22:53








1




1




The solution seems to miss a ' to finish the sed expression.
– nawroth
Oct 14 '11 at 12:48




The solution seems to miss a ' to finish the sed expression.
– nawroth
Oct 14 '11 at 12:48












Which sed exp? I can't seem to notice.
– mkko
Oct 14 '11 at 16:17




Which sed exp? I can't seem to notice.
– mkko
Oct 14 '11 at 16:17




1




1




make that a mvn -o for faster execution
– Nathan Bubna
Nov 30 '12 at 22:51




make that a mvn -o for faster execution
– Nathan Bubna
Nov 30 '12 at 22:51




2




2




You can replace that complex sed expression with a simple grep -e '^[[:digit:]]'
– bpedman
Sep 14 '13 at 4:14






You can replace that complex sed expression with a simple grep -e '^[[:digit:]]'
– bpedman
Sep 14 '13 at 4:14






2




2




If you use -o as recommended by @NathanBubna then that will put maven in 'offline' mode. If you don't already have the maven help plugin and dependent jars downloaded then the build will fail. Got burned by that for a bit, hope it helps somebody else.
– phillipuniverse
Feb 8 '14 at 22:53




If you use -o as recommended by @NathanBubna then that will put maven in 'offline' mode. If you don't already have the maven help plugin and dependent jars downloaded then the build will fail. Got burned by that for a bit, hope it helps somebody else.
– phillipuniverse
Feb 8 '14 at 22:53












22 Answers
22






active

oldest

votes

















up vote
172
down vote



accepted










The Maven Help Plugin is somehow already proposing something for this:






  • help:evaluate evaluates Maven expressions given by the user in an interactive mode.




Here is how you would invoke it on the command line to get the ${project.version}:



mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate 
-Dexpression=project.version





share|improve this answer

















  • 7




    Okay thanks. I think this is the closest I'll get to a clean solution. Any ideas how could I surpress the maven output and filter out those [INFO] messages? I didn't find a switch for maven. Otherwise I'll just add some command line scripting to parse the version number.
    – mkko
    Aug 24 '10 at 5:47






  • 70




    I'm removing all logging (INFO,WARNING,etc) and 'Download' messages with mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version|grep -Ev '(^[|Downloadw+:)'
    – Chadwick
    Mar 29 '13 at 18:58






  • 3




    You might want to also add 2> /dev/null as otherwise you can get Picked up _JAVA_OPTIONS:
    – Jakub Bochenski
    Nov 2 '15 at 21:06








  • 6




    wouldn't expect anything less verbose from maven
    – Andy
    Jan 13 '16 at 11:33






  • 20




    Here's an approach I favor: printf 'VERSION=${project.version}n0n' | mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate | grep '^VERSION'
    – ash
    Jul 1 '16 at 17:47


















up vote
137
down vote













Tom's solution with the Exec Maven Plugin is much better, but still more complicated than it needs to be. For me it's as simple as:



MVN_VERSION=$(mvn -q 
-Dexec.executable=echo
-Dexec.args='${project.version}'
--non-recursive
exec:exec)





share|improve this answer



















  • 24




    I think this is the easiest way since it does not imply use of grep or similar things. Quick note: you may make it a bit shorter: mvn -q -Dexec.executable="echo" -Dexec.args='${project.version}' --non-recursive exec:exec
    – scrutari
    Sep 13 '16 at 16:57






  • 1




    This solution worked well for me as I wanted to print groupId, artifactId and version.. -Dexec.args='${project.groupId}:${project.artifactId}:${project.version}'.
    – James H.
    Jan 5 '17 at 16:03












  • [ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.3.2:exec (default-cli) on project audit-events-processor-parent: Command execution failed. Cannot run program "maven" (in directory "/tmp"): error=2, No such file or directory shrug yet another answer that doesn't work for me, oh well
    – cbmanica
    Jan 31 '17 at 20:03










  • Thank you this is the best solution I have seen so far, since it does not require to apply fragile grep/sed commands or similar
    – Mike76
    Aug 16 '17 at 7:55










  • What is set -o errexit
    – theonlygusti
    Nov 6 at 15:05


















up vote
45
down vote













mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | grep -v '['





share|improve this answer























  • make that mvn -o for faster execution
    – Nathan Bubna
    Nov 30 '12 at 22:50






  • 1




    Does not work to suppress "Downloaded:" lines.
    – ceving
    Feb 28 '13 at 10:34






  • 1




    See my answer (quickshiftin) below which filters 'Downloaded:' lines too.
    – quickshiftin
    Oct 8 '13 at 20:42






  • 9




    If you use -o as recommended by @NathanBubna then that will put maven in 'offline' mode. If you don't already have the maven help plugin and dependent jars downloaded then the build will fail. Got burned by that for a bit, hope it helps somebody else.
    – phillipuniverse
    Feb 8 '14 at 22:52










  • You might want to also add 2> /dev/null as otherwise you can get Picked up _JAVA_OPTIONS:
    – Jakub Bochenski
    Nov 2 '15 at 21:06


















up vote
28
down vote













The top answer is pretty garbage in my opinion, you have to use a bunch of grep to hack out the maven console output. Why not use the right tool for the job? Using xpath syntax is the best approach to retrieving the version number, since it is the intended method of accessing a XML data structure. The expression below is traversing the pom using the "local name" of the elements, in other words ignoring namespace declarations which may or may not be present in the xml.



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





share|improve this answer























  • @bobmarksie I second that; using maven and scrubbing the output with regexp's is painful.
    – mhvelplund
    Jun 13 at 6:52








  • 2




    What about a project that inherits from a parent pom.
    – Wes
    Jun 15 at 11:53






  • 1




    "Why not use the right tool for the job?" IE !Maven?
    – jeremyjjbrown
    Jun 18 at 15:47




















up vote
23
down vote













This will avoid the need for grepping off log entries from the output:



mvn -Dexec.executable='echo' -Dexec.args='${project.version}' --non-recursive exec:exec -q





share|improve this answer























  • While this code may answer the question, providing additional context regarding why and/or how it answers the question would significantly improve its long-term value. Please edit your answer to add some explanation.
    – Toby Speight
    Apr 12 '16 at 13:02






  • 1




    I think for this approach it is necessary to put the "-q" after mvn.
    – Rudge
    Aug 7 '17 at 19:47










  • This is better than the accepted solution; no need to filter out maven noisy output.
    – Willian Mitsuda
    Jun 20 at 3:17


















up vote
12
down vote













I have been doing some research and found the following:




  1. Maven has been blamed for not being easily integrable within system operation scripts since it does not follow some good practices regarding CLI tools. (ref: https://youtu.be/1ILEw6Qca3U?t=372)



  2. Inspired on the previous assertion, I decided to give a look on maven's source code as well as on maven-help-plugin. It seems that they have fixed a little bit the maven's -q switch (I am using version 3.5.3), so now if you pass it, you won't get all the annoying non-sense logging stuff that prevents maven from being used within automated scripts. So you should be able to use something like this:



    mvn help:evaluate -Dexpression=project.version -q


    The problem is that this command prints nothing because by default the help plugin outputs through the logger which has been silenced by the -q switch. (latest available version of the plugin is 3.1.0 released on June, 3rd 2018)




  3. Karl Heinz Marbaise (https://github.com/khmarbaise) fixed it by adding an optional parameter that allows you to call it in the following way:



    mvn help:evaluate -Dexpression=project.version -q -DforceStdout


    The commit description is available at: (https://github.com/apache/maven-help-plugin/commit/316656983d780c04031bbadd97d4ab245c84d014)








share|improve this answer



















  • 1




    This would be my preferred solution and is also mentioned on the official help pages. However, it doesn't work for me. I am on MacOS with Maven v3.5.3. When I don't use the -q switch it prints out the version correctly (in between the log lines). Any ideas?
    – gucce
    Jul 12 at 12:23












  • Hi @gucce, What is the exact command line you are using? I am also on a Mac OSX with Maven 3.5.3. Are you using the latest version of the plugin?
    – montoyaedu
    Jul 16 at 12:04






  • 1




    This should be the accepted answer. It works in Windows and Linux (I didn't try Max OSX), and it's simple. Thank you.
    – trash80
    Jul 24 at 18:10






  • 1




    @montoyaedu Sorry for not responding. In the meantime I have updated to maven 3.5.4 which works fine with the -q switch.
    – gucce
    Oct 10 at 8:58










  • @gucce I can confirm that maven version used affects this: with 3.5.2 (package version on debian 9) the output with -q -DforceStdout was empty, even ensuring version 3.1.0 of the plugin was used with pluginManagement) ; i configured maven wrapper with version 3.5.4 of maven, and it worked correctly
    – Gorkk
    Oct 26 at 16:29


















up vote
11
down vote













python -c "import xml.etree.ElementTree as ET; 
print(ET.parse(open('pom.xml')).getroot().find(
'{http://maven.apache.org/POM/4.0.0}version').text)"


As long as you have python 2.5 or greater, this should work. If you have a lower version than that, install python-lxml and change the import to lxml.etree. This method is quick and doesn't require downloading any extra plugins. It also works on malformed pom.xml files that don't validate with xmllint, like the ones I need to parse. Tested on Mac and Linux.






share|improve this answer




























    up vote
    8
    down vote













    I kept running into side cases when using some of the other answers here, so here's yet another alternative.



    version=$(printf 'VERt${project.version}' | mvn help:evaluate | grep '^VER' | cut -f2)





    share|improve this answer

















    • 1




      printf 'VERt${project.version}' | mvn help:evaluate 2> /dev/null | grep '^VER' | cut -f2
      – yegeniy
      Oct 24 '16 at 15:37


















    up vote
    7
    down vote













    If you don't mind to write the version into a temporary file, there is another solution (without grep/sed) that works well for me. (EDIT: see rjrjr's answer for a much simpler solution without any temporary file hassle)



    I use the Exec Maven Plugin along with the echo binary. In contrast to the Maven Help Plugin, the Exec Plugin allows output redirection into a file, which can be used to bypass grep/sed, and makes it even possible to parse strange things like multiline version strings (with CDATA block in version tag), at least to a certain extent.



    #!/usr/bin/env sh

    MVN_VERSION=""
    VERSION_FILE=$( mktemp mvn_project_version_XXXXX )
    trap "rm -f -- "$VERSION_FILE"" INT EXIT

    mvn -Dexec.executable="echo"
    -Dexec.args='${project.version}'
    -Dexec.outputFile="$VERSION_FILE"
    --non-recursive
    --batch-mode
    org.codehaus.mojo:exec-maven-plugin:1.3.1:exec > /dev/null 2>&1 ||
    { echo "Maven invocation failed!" 1>&2; exit 1; }

    # if you just care about the first line of the version, which will be
    # sufficent for pretty much every use case I can imagine, you can use
    # the read builtin
    [ -s "$VERSION_FILE" ] && read -r MVN_VERSION < "$VERSION_FILE"

    # Otherwise, you could use cat.
    # Note that this still has issues when there are leading whitespaces
    # in the multiline version string
    #MVN_VERSION=$( cat "$VERSION_FILE" )

    printf "Maven project version: %sn" "$MVN_VERSION"





    share|improve this answer



















    • 6




      This is a much better solution than the helper plugin, and you don't need all that i/o. Just add a -q flag and the only output will be the version. So: mvn -q -Dexec.executable="echo" -Dexec.args='${project.version}' --non-recursive org.codehaus.mojo:exec-maven-plugin:1.3.1:exec
      – rjrjr
      Oct 22 '14 at 17:51












    • rjrjr: Great! The only thing I need to mention here is error handling: If mvn fails, you get an invalid version string. So some validation is required, like checking mvn return code or the string itself.
      – Tom
      Oct 23 '14 at 6:19


















    up vote
    5
    down vote













    I noticed some spurious Downloaded: lines coming in the output that were breaking my original assignment. Here's the filter I've settled on; hope it helps!



    version=$(mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | egrep -v '^[|Downloading:' | tr -d ' n')


    EDIT



    Not 100% sure why, but when running this through a post-build script in Jenkins, the output was coming out as [INFO]version, e.g. [INFO]0.3.2.



    I dumped the output to a file and ran it through my first filter directly from BASH, it works fine.., so again, unsure what's going on in Jenkins land.



    To get it 100% in Jenkins, I've added a follow-up sed filter; here's my latest



    version=$(mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | egrep -v '^[|Downloading:' | tr -d ' n' | sed -E 's/[.*]//g')


    EDIT



    One last note here.. I found out tr was still resulting in things like /r/n0.3.2 (again only when running via Jenkins). Switched to awk and the problem has gone away! My final working result



    mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version 
    | egrep -v '^[|Downloading:' | sed 's/[^0-9.]//g' | awk 1 ORS=''





    share|improve this answer






























      up vote
      5
      down vote













      Just for the record, it's possible to configure Maven's Simple SLF4J logging directly in the command line to output only what we need by configuring:





      • org.slf4j.simpleLogger.defaultLogLevel=WARN and

      • org.slf4j.simpleLogger.log.org.apache.maven.plugins.help=INFO


      as documented at http://www.slf4j.org/api/org/slf4j/impl/SimpleLogger.html



      MAVEN_OPTS="
      -Dorg.slf4j.simpleLogger.defaultLogLevel=WARN
      -Dorg.slf4j.simpleLogger.log.org.apache.maven.plugins.help=INFO"
      mvn help:evaluate -o -Dexpression=project.version


      As a result, one can run simply tail -1 and get:



      $ MAVEN_OPTS="
      -Dorg.slf4j.simpleLogger.defaultLogLevel=WARN
      -Dorg.slf4j.simpleLogger.log.org.apache.maven.plugins.help=INFO"
      mvn help:evaluate -o -Dexpression=project.version | tail -1

      1.0.0-SNAPSHOT


      Note that this is a one-liner. MAVEN_OPTS are being rewritten only for this particular mvn execution.






      share|improve this answer




























        up vote
        4
        down vote













        A simple maven only solution



        mvn -q -N org.codehaus.mojo:exec-maven-plugin:1.3.1:exec 
        -Dexec.executable='echo'
        -Dexec.args='${project.version}'


        And for bonus points parsed part of a version



        mvn -q -N org.codehaus.mojo:build-helper-maven-plugin:3.0.0:parse-version 
        org.codehaus.mojo:exec-maven-plugin:1.3.1:exec
        -Dexec.executable='echo'
        -Dexec.args='${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.incrementalVersion}'





        share|improve this answer





















        • 'echo' doesn't work in Windows.
          – trash80
          Jul 24 at 18:02


















        up vote
        4
        down vote













        I've recently developed the Release Candidate Maven plugin that solves this exact problem so that you don't have to resort to any hacky shell scripts and parsing the output of the maven-help-plugin.



        For example, to print the version of your Maven project to a terminal, run:



        mvn com.smartcodeltd:release-candidate-maven-plugin:LATEST:version


        which gives output similar to maven-help-plugin:



        [INFO] Detected version: '1.0.0-SNAPSHOT'
        1.0.0-SNAPSHOT
        [INFO] ------------------------------------------------------------------------
        [INFO] BUILD SUCCESS
        [INFO] ------------------------------------------------------------------------


        However, you can also specify an arbitrary output format (so that the version could be picked up from the log by a CI server such as TeamCity):



        mvn com.smartcodeltd:release-candidate-maven-plugin:LATEST:version 
        -DoutputTemplate="##teamcity[setParameter name='env.PROJECT_VERSION' value='{{ version }}']"


        Which results in:



        [INFO] Detected version: '1.0.0-SNAPSHOT'
        ##teamcity[setParameter name='env.PROJECT_VERSION' value='1.0.0-SNAPSHOT']
        [INFO] ------------------------------------------------------------------------
        [INFO] BUILD SUCCESS
        [INFO] ------------------------------------------------------------------------


        To save the output to a file (so that a CI server such as Jenkins could use it):



        mvn com.smartcodeltd:release-candidate-maven-plugin:LATEST:version 
        -DoutputTemplate="PROJECT_VERSION={{ version }}"
        -DoutputUri="file://${project.basedir}/version.properties"


        The resulting version.properties file will look as follows:



        PROJECT_VERSION=1.0.0-SNAPSHOT




        On top of all the above, Release Candidate also allows you to set the version of your project (which is something you'd probably do on your CI server) based on the API version you've defined in your POM.



        If you'd like to see an example of Release Candidate being used as part of the Maven lifecycle, have a look at the pom.xml of my other open-source project - Build Monitor for Jenkins.






        share|improve this answer






























          up vote
          3
          down vote













          The easy to understand all-in-one solution that outputs the maven project version, and suppresses extraneous output from [INFO] and Download messages:



          mvn -o org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | grep -v '['


          Same thing, but split onto two lines:



          mvn -o org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate 
          -Dexpression=project.version | grep -v '['


          Outputs: 4.3-SNAPSHOT



          So, using your project.version in a simple bash script:



          projectVersion=`mvn -o org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | grep -v '['`
          cd "target/"$projectVersion"-build"


          Other solutions on this page didn't seem to combine all the tricks into one.






          share|improve this answer




























            up vote
            2
            down vote













            Should be easier since this bug is fixed in maven-help-plugin 3.0.0: MPH-99 Evaluate has no output in quiet mode.






            share|improve this answer























            • Sadly 3.0.0 is not published
              – Víctor Romero
              Feb 15 at 0:03


















            up vote
            1
            down vote













            Exec plugin works without any output parsing because output can be redirected into file and injected back into the job environment via EnvInject plugin:



            enter image description here






            share|improve this answer




























              up vote
              1
              down vote













              I found right balance for me. After mvn package maven-archiver plugin creates target/maven-archiver/pom.properties with contents like this



              version=0.0.1-SNAPSHOT
              groupId=somegroup
              artifactId=someArtifact


              and I am using bash just to execute it



              . ./target/maven-archiver/pom.properties


              then



              echo $version
              0.0.1-SNAPSHOT


              Of course this is not safe at all to execute this file, but execution can easily be converted into perl or bash script to read and set environment variable from that file.






              share|improve this answer




























                up vote
                0
                down vote













                This worked for me, offline and without depending on mvn:



                VERSION=$(grep --max-count=1 '<version>' <your_path>/pom.xml | awk -F '>' '{ print $2 }' | awk -F '<' '{ print $1 }')
                echo $VERSION





                share|improve this answer




























                  up vote
                  0
                  down vote













                  Either you have mvn give you the answer (as most answers suggest), or you extract the answer from the pom.xml. The only drawback of the second approach is that you can very easily extract the value of the <version/> tag, but it will be meaningful only if it's literal, that is, not a Maven property. I chose this approach anyway because:





                  • mvn is way to verbose and I simply don't like filtering its output.

                  • Starting mvn is very slow compared to reading the pom.xml.

                  • I always use literal values in <version/>.


                  mvn-version is a zsh shell script that uses xmlstarlet to read the pom.xml and print the version of the project (if it exists) or the version of the parent project (if it exists):



                  $ mvn-version .
                  1.0.0-SNAPSHOT


                  The advantage is that it's way quicker than running mvn:



                  $ time mvn-version .
                  1.1.0-SNAPSHOT
                  mvn-version . 0.01s user 0.01s system 75% cpu 0.019 total

                  $ time mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate
                  > -Dexpression=project.version
                  mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate 4.17s user 0.21s system 240% cpu 1.823 total


                  The difference on my machine is greater than two orders of magnitude.






                  share|improve this answer




























                    up vote
                    -2
                    down vote













                    mvn help:evaluate -Dexpression=project.version | sed -e 1h -e '2,3{H;g}' -e '/[INFO] BUILD SUCCESS/ q' -e '1,2d' -e '{N;D}' | sed -e '1q'


                    I'm just adding small sed filter improvement I have recently implemented to extract project.version from maven output.






                    share|improve this answer



















                    • 2




                      Does not work here.
                      – ceving
                      Feb 28 '13 at 10:30










                    • Does not work for me either
                      – Joseph Earl
                      May 30 '13 at 15:20










                    • For me it works, i have just corrected the regex.
                      – Gábor Lipták
                      Jun 19 '13 at 13:21


















                    up vote
                    -2
                    down vote













                    VERSION=$(head -50 pom.xml | awk -F'>' '/SNAPSHOT/ {print $2}' | awk -F'<' '{print $1}')


                    This is what I used to get the version number, thought there would have been a better maven way to do so






                    share|improve this answer




























                      up vote
                      -4
                      down vote













                      Maven footer is pretty standard:



                      [INFO] ------------------------------------------------------------------------
                      [INFO] BUILD SUCCESS
                      [INFO] ------------------------------------------------------------------------
                      [INFO] Total time: 1.609s
                      [INFO] Finished at: Wed May 21 18:02:38 MSK 2014
                      [INFO] Final Memory: 17M/736M
                      [INFO] ------------------------------------------------------------------------


                      So you can use the following code:



                      > version=$(mvn help:evaluate -Dexpression=project.version | tail -8 | head -1)
                      > echo $version





                      share|improve this answer





















                        Your Answer






                        StackExchange.ifUsing("editor", function () {
                        StackExchange.using("externalEditor", function () {
                        StackExchange.using("snippets", function () {
                        StackExchange.snippets.init();
                        });
                        });
                        }, "code-snippets");

                        StackExchange.ready(function() {
                        var channelOptions = {
                        tags: "".split(" "),
                        id: "1"
                        };
                        initTagRenderer("".split(" "), "".split(" "), channelOptions);

                        StackExchange.using("externalEditor", function() {
                        // Have to fire editor after snippets, if snippets enabled
                        if (StackExchange.settings.snippets.snippetsEnabled) {
                        StackExchange.using("snippets", function() {
                        createEditor();
                        });
                        }
                        else {
                        createEditor();
                        }
                        });

                        function createEditor() {
                        StackExchange.prepareEditor({
                        heartbeatType: 'answer',
                        convertImagesToLinks: true,
                        noModals: true,
                        showLowRepImageUploadWarning: true,
                        reputationToPostImages: 10,
                        bindNavPrevention: true,
                        postfix: "",
                        imageUploader: {
                        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                        allowUrls: true
                        },
                        onDemand: true,
                        discardSelector: ".discard-answer"
                        ,immediatelyShowMarkdownHelp:true
                        });


                        }
                        });














                        draft saved

                        draft discarded


















                        StackExchange.ready(
                        function () {
                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3545292%2fhow-to-get-maven-project-version-to-the-bash-command-line%23new-answer', 'question_page');
                        }
                        );

                        Post as a guest















                        Required, but never shown

























                        22 Answers
                        22






                        active

                        oldest

                        votes








                        22 Answers
                        22






                        active

                        oldest

                        votes









                        active

                        oldest

                        votes






                        active

                        oldest

                        votes








                        up vote
                        172
                        down vote



                        accepted










                        The Maven Help Plugin is somehow already proposing something for this:






                        • help:evaluate evaluates Maven expressions given by the user in an interactive mode.




                        Here is how you would invoke it on the command line to get the ${project.version}:



                        mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate 
                        -Dexpression=project.version





                        share|improve this answer

















                        • 7




                          Okay thanks. I think this is the closest I'll get to a clean solution. Any ideas how could I surpress the maven output and filter out those [INFO] messages? I didn't find a switch for maven. Otherwise I'll just add some command line scripting to parse the version number.
                          – mkko
                          Aug 24 '10 at 5:47






                        • 70




                          I'm removing all logging (INFO,WARNING,etc) and 'Download' messages with mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version|grep -Ev '(^[|Downloadw+:)'
                          – Chadwick
                          Mar 29 '13 at 18:58






                        • 3




                          You might want to also add 2> /dev/null as otherwise you can get Picked up _JAVA_OPTIONS:
                          – Jakub Bochenski
                          Nov 2 '15 at 21:06








                        • 6




                          wouldn't expect anything less verbose from maven
                          – Andy
                          Jan 13 '16 at 11:33






                        • 20




                          Here's an approach I favor: printf 'VERSION=${project.version}n0n' | mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate | grep '^VERSION'
                          – ash
                          Jul 1 '16 at 17:47















                        up vote
                        172
                        down vote



                        accepted










                        The Maven Help Plugin is somehow already proposing something for this:






                        • help:evaluate evaluates Maven expressions given by the user in an interactive mode.




                        Here is how you would invoke it on the command line to get the ${project.version}:



                        mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate 
                        -Dexpression=project.version





                        share|improve this answer

















                        • 7




                          Okay thanks. I think this is the closest I'll get to a clean solution. Any ideas how could I surpress the maven output and filter out those [INFO] messages? I didn't find a switch for maven. Otherwise I'll just add some command line scripting to parse the version number.
                          – mkko
                          Aug 24 '10 at 5:47






                        • 70




                          I'm removing all logging (INFO,WARNING,etc) and 'Download' messages with mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version|grep -Ev '(^[|Downloadw+:)'
                          – Chadwick
                          Mar 29 '13 at 18:58






                        • 3




                          You might want to also add 2> /dev/null as otherwise you can get Picked up _JAVA_OPTIONS:
                          – Jakub Bochenski
                          Nov 2 '15 at 21:06








                        • 6




                          wouldn't expect anything less verbose from maven
                          – Andy
                          Jan 13 '16 at 11:33






                        • 20




                          Here's an approach I favor: printf 'VERSION=${project.version}n0n' | mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate | grep '^VERSION'
                          – ash
                          Jul 1 '16 at 17:47













                        up vote
                        172
                        down vote



                        accepted







                        up vote
                        172
                        down vote



                        accepted






                        The Maven Help Plugin is somehow already proposing something for this:






                        • help:evaluate evaluates Maven expressions given by the user in an interactive mode.




                        Here is how you would invoke it on the command line to get the ${project.version}:



                        mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate 
                        -Dexpression=project.version





                        share|improve this answer












                        The Maven Help Plugin is somehow already proposing something for this:






                        • help:evaluate evaluates Maven expressions given by the user in an interactive mode.




                        Here is how you would invoke it on the command line to get the ${project.version}:



                        mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate 
                        -Dexpression=project.version






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Aug 23 '10 at 7:06









                        Pascal Thivent

                        477k1109401057




                        477k1109401057








                        • 7




                          Okay thanks. I think this is the closest I'll get to a clean solution. Any ideas how could I surpress the maven output and filter out those [INFO] messages? I didn't find a switch for maven. Otherwise I'll just add some command line scripting to parse the version number.
                          – mkko
                          Aug 24 '10 at 5:47






                        • 70




                          I'm removing all logging (INFO,WARNING,etc) and 'Download' messages with mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version|grep -Ev '(^[|Downloadw+:)'
                          – Chadwick
                          Mar 29 '13 at 18:58






                        • 3




                          You might want to also add 2> /dev/null as otherwise you can get Picked up _JAVA_OPTIONS:
                          – Jakub Bochenski
                          Nov 2 '15 at 21:06








                        • 6




                          wouldn't expect anything less verbose from maven
                          – Andy
                          Jan 13 '16 at 11:33






                        • 20




                          Here's an approach I favor: printf 'VERSION=${project.version}n0n' | mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate | grep '^VERSION'
                          – ash
                          Jul 1 '16 at 17:47














                        • 7




                          Okay thanks. I think this is the closest I'll get to a clean solution. Any ideas how could I surpress the maven output and filter out those [INFO] messages? I didn't find a switch for maven. Otherwise I'll just add some command line scripting to parse the version number.
                          – mkko
                          Aug 24 '10 at 5:47






                        • 70




                          I'm removing all logging (INFO,WARNING,etc) and 'Download' messages with mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version|grep -Ev '(^[|Downloadw+:)'
                          – Chadwick
                          Mar 29 '13 at 18:58






                        • 3




                          You might want to also add 2> /dev/null as otherwise you can get Picked up _JAVA_OPTIONS:
                          – Jakub Bochenski
                          Nov 2 '15 at 21:06








                        • 6




                          wouldn't expect anything less verbose from maven
                          – Andy
                          Jan 13 '16 at 11:33






                        • 20




                          Here's an approach I favor: printf 'VERSION=${project.version}n0n' | mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate | grep '^VERSION'
                          – ash
                          Jul 1 '16 at 17:47








                        7




                        7




                        Okay thanks. I think this is the closest I'll get to a clean solution. Any ideas how could I surpress the maven output and filter out those [INFO] messages? I didn't find a switch for maven. Otherwise I'll just add some command line scripting to parse the version number.
                        – mkko
                        Aug 24 '10 at 5:47




                        Okay thanks. I think this is the closest I'll get to a clean solution. Any ideas how could I surpress the maven output and filter out those [INFO] messages? I didn't find a switch for maven. Otherwise I'll just add some command line scripting to parse the version number.
                        – mkko
                        Aug 24 '10 at 5:47




                        70




                        70




                        I'm removing all logging (INFO,WARNING,etc) and 'Download' messages with mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version|grep -Ev '(^[|Downloadw+:)'
                        – Chadwick
                        Mar 29 '13 at 18:58




                        I'm removing all logging (INFO,WARNING,etc) and 'Download' messages with mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version|grep -Ev '(^[|Downloadw+:)'
                        – Chadwick
                        Mar 29 '13 at 18:58




                        3




                        3




                        You might want to also add 2> /dev/null as otherwise you can get Picked up _JAVA_OPTIONS:
                        – Jakub Bochenski
                        Nov 2 '15 at 21:06






                        You might want to also add 2> /dev/null as otherwise you can get Picked up _JAVA_OPTIONS:
                        – Jakub Bochenski
                        Nov 2 '15 at 21:06






                        6




                        6




                        wouldn't expect anything less verbose from maven
                        – Andy
                        Jan 13 '16 at 11:33




                        wouldn't expect anything less verbose from maven
                        – Andy
                        Jan 13 '16 at 11:33




                        20




                        20




                        Here's an approach I favor: printf 'VERSION=${project.version}n0n' | mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate | grep '^VERSION'
                        – ash
                        Jul 1 '16 at 17:47




                        Here's an approach I favor: printf 'VERSION=${project.version}n0n' | mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate | grep '^VERSION'
                        – ash
                        Jul 1 '16 at 17:47












                        up vote
                        137
                        down vote













                        Tom's solution with the Exec Maven Plugin is much better, but still more complicated than it needs to be. For me it's as simple as:



                        MVN_VERSION=$(mvn -q 
                        -Dexec.executable=echo
                        -Dexec.args='${project.version}'
                        --non-recursive
                        exec:exec)





                        share|improve this answer



















                        • 24




                          I think this is the easiest way since it does not imply use of grep or similar things. Quick note: you may make it a bit shorter: mvn -q -Dexec.executable="echo" -Dexec.args='${project.version}' --non-recursive exec:exec
                          – scrutari
                          Sep 13 '16 at 16:57






                        • 1




                          This solution worked well for me as I wanted to print groupId, artifactId and version.. -Dexec.args='${project.groupId}:${project.artifactId}:${project.version}'.
                          – James H.
                          Jan 5 '17 at 16:03












                        • [ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.3.2:exec (default-cli) on project audit-events-processor-parent: Command execution failed. Cannot run program "maven" (in directory "/tmp"): error=2, No such file or directory shrug yet another answer that doesn't work for me, oh well
                          – cbmanica
                          Jan 31 '17 at 20:03










                        • Thank you this is the best solution I have seen so far, since it does not require to apply fragile grep/sed commands or similar
                          – Mike76
                          Aug 16 '17 at 7:55










                        • What is set -o errexit
                          – theonlygusti
                          Nov 6 at 15:05















                        up vote
                        137
                        down vote













                        Tom's solution with the Exec Maven Plugin is much better, but still more complicated than it needs to be. For me it's as simple as:



                        MVN_VERSION=$(mvn -q 
                        -Dexec.executable=echo
                        -Dexec.args='${project.version}'
                        --non-recursive
                        exec:exec)





                        share|improve this answer



















                        • 24




                          I think this is the easiest way since it does not imply use of grep or similar things. Quick note: you may make it a bit shorter: mvn -q -Dexec.executable="echo" -Dexec.args='${project.version}' --non-recursive exec:exec
                          – scrutari
                          Sep 13 '16 at 16:57






                        • 1




                          This solution worked well for me as I wanted to print groupId, artifactId and version.. -Dexec.args='${project.groupId}:${project.artifactId}:${project.version}'.
                          – James H.
                          Jan 5 '17 at 16:03












                        • [ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.3.2:exec (default-cli) on project audit-events-processor-parent: Command execution failed. Cannot run program "maven" (in directory "/tmp"): error=2, No such file or directory shrug yet another answer that doesn't work for me, oh well
                          – cbmanica
                          Jan 31 '17 at 20:03










                        • Thank you this is the best solution I have seen so far, since it does not require to apply fragile grep/sed commands or similar
                          – Mike76
                          Aug 16 '17 at 7:55










                        • What is set -o errexit
                          – theonlygusti
                          Nov 6 at 15:05













                        up vote
                        137
                        down vote










                        up vote
                        137
                        down vote









                        Tom's solution with the Exec Maven Plugin is much better, but still more complicated than it needs to be. For me it's as simple as:



                        MVN_VERSION=$(mvn -q 
                        -Dexec.executable=echo
                        -Dexec.args='${project.version}'
                        --non-recursive
                        exec:exec)





                        share|improve this answer














                        Tom's solution with the Exec Maven Plugin is much better, but still more complicated than it needs to be. For me it's as simple as:



                        MVN_VERSION=$(mvn -q 
                        -Dexec.executable=echo
                        -Dexec.args='${project.version}'
                        --non-recursive
                        exec:exec)






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Nov 28 at 11:53









                        Piotr Findeisen

                        4,8561538




                        4,8561538










                        answered Oct 22 '14 at 18:00









                        rjrjr

                        2,38611615




                        2,38611615








                        • 24




                          I think this is the easiest way since it does not imply use of grep or similar things. Quick note: you may make it a bit shorter: mvn -q -Dexec.executable="echo" -Dexec.args='${project.version}' --non-recursive exec:exec
                          – scrutari
                          Sep 13 '16 at 16:57






                        • 1




                          This solution worked well for me as I wanted to print groupId, artifactId and version.. -Dexec.args='${project.groupId}:${project.artifactId}:${project.version}'.
                          – James H.
                          Jan 5 '17 at 16:03












                        • [ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.3.2:exec (default-cli) on project audit-events-processor-parent: Command execution failed. Cannot run program "maven" (in directory "/tmp"): error=2, No such file or directory shrug yet another answer that doesn't work for me, oh well
                          – cbmanica
                          Jan 31 '17 at 20:03










                        • Thank you this is the best solution I have seen so far, since it does not require to apply fragile grep/sed commands or similar
                          – Mike76
                          Aug 16 '17 at 7:55










                        • What is set -o errexit
                          – theonlygusti
                          Nov 6 at 15:05














                        • 24




                          I think this is the easiest way since it does not imply use of grep or similar things. Quick note: you may make it a bit shorter: mvn -q -Dexec.executable="echo" -Dexec.args='${project.version}' --non-recursive exec:exec
                          – scrutari
                          Sep 13 '16 at 16:57






                        • 1




                          This solution worked well for me as I wanted to print groupId, artifactId and version.. -Dexec.args='${project.groupId}:${project.artifactId}:${project.version}'.
                          – James H.
                          Jan 5 '17 at 16:03












                        • [ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.3.2:exec (default-cli) on project audit-events-processor-parent: Command execution failed. Cannot run program "maven" (in directory "/tmp"): error=2, No such file or directory shrug yet another answer that doesn't work for me, oh well
                          – cbmanica
                          Jan 31 '17 at 20:03










                        • Thank you this is the best solution I have seen so far, since it does not require to apply fragile grep/sed commands or similar
                          – Mike76
                          Aug 16 '17 at 7:55










                        • What is set -o errexit
                          – theonlygusti
                          Nov 6 at 15:05








                        24




                        24




                        I think this is the easiest way since it does not imply use of grep or similar things. Quick note: you may make it a bit shorter: mvn -q -Dexec.executable="echo" -Dexec.args='${project.version}' --non-recursive exec:exec
                        – scrutari
                        Sep 13 '16 at 16:57




                        I think this is the easiest way since it does not imply use of grep or similar things. Quick note: you may make it a bit shorter: mvn -q -Dexec.executable="echo" -Dexec.args='${project.version}' --non-recursive exec:exec
                        – scrutari
                        Sep 13 '16 at 16:57




                        1




                        1




                        This solution worked well for me as I wanted to print groupId, artifactId and version.. -Dexec.args='${project.groupId}:${project.artifactId}:${project.version}'.
                        – James H.
                        Jan 5 '17 at 16:03






                        This solution worked well for me as I wanted to print groupId, artifactId and version.. -Dexec.args='${project.groupId}:${project.artifactId}:${project.version}'.
                        – James H.
                        Jan 5 '17 at 16:03














                        [ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.3.2:exec (default-cli) on project audit-events-processor-parent: Command execution failed. Cannot run program "maven" (in directory "/tmp"): error=2, No such file or directory shrug yet another answer that doesn't work for me, oh well
                        – cbmanica
                        Jan 31 '17 at 20:03




                        [ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.3.2:exec (default-cli) on project audit-events-processor-parent: Command execution failed. Cannot run program "maven" (in directory "/tmp"): error=2, No such file or directory shrug yet another answer that doesn't work for me, oh well
                        – cbmanica
                        Jan 31 '17 at 20:03












                        Thank you this is the best solution I have seen so far, since it does not require to apply fragile grep/sed commands or similar
                        – Mike76
                        Aug 16 '17 at 7:55




                        Thank you this is the best solution I have seen so far, since it does not require to apply fragile grep/sed commands or similar
                        – Mike76
                        Aug 16 '17 at 7:55












                        What is set -o errexit
                        – theonlygusti
                        Nov 6 at 15:05




                        What is set -o errexit
                        – theonlygusti
                        Nov 6 at 15:05










                        up vote
                        45
                        down vote













                        mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | grep -v '['





                        share|improve this answer























                        • make that mvn -o for faster execution
                          – Nathan Bubna
                          Nov 30 '12 at 22:50






                        • 1




                          Does not work to suppress "Downloaded:" lines.
                          – ceving
                          Feb 28 '13 at 10:34






                        • 1




                          See my answer (quickshiftin) below which filters 'Downloaded:' lines too.
                          – quickshiftin
                          Oct 8 '13 at 20:42






                        • 9




                          If you use -o as recommended by @NathanBubna then that will put maven in 'offline' mode. If you don't already have the maven help plugin and dependent jars downloaded then the build will fail. Got burned by that for a bit, hope it helps somebody else.
                          – phillipuniverse
                          Feb 8 '14 at 22:52










                        • You might want to also add 2> /dev/null as otherwise you can get Picked up _JAVA_OPTIONS:
                          – Jakub Bochenski
                          Nov 2 '15 at 21:06















                        up vote
                        45
                        down vote













                        mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | grep -v '['





                        share|improve this answer























                        • make that mvn -o for faster execution
                          – Nathan Bubna
                          Nov 30 '12 at 22:50






                        • 1




                          Does not work to suppress "Downloaded:" lines.
                          – ceving
                          Feb 28 '13 at 10:34






                        • 1




                          See my answer (quickshiftin) below which filters 'Downloaded:' lines too.
                          – quickshiftin
                          Oct 8 '13 at 20:42






                        • 9




                          If you use -o as recommended by @NathanBubna then that will put maven in 'offline' mode. If you don't already have the maven help plugin and dependent jars downloaded then the build will fail. Got burned by that for a bit, hope it helps somebody else.
                          – phillipuniverse
                          Feb 8 '14 at 22:52










                        • You might want to also add 2> /dev/null as otherwise you can get Picked up _JAVA_OPTIONS:
                          – Jakub Bochenski
                          Nov 2 '15 at 21:06













                        up vote
                        45
                        down vote










                        up vote
                        45
                        down vote









                        mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | grep -v '['





                        share|improve this answer














                        mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | grep -v '['






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Jul 8 '13 at 13:14









                        palacsint

                        21.7k96394




                        21.7k96394










                        answered Oct 4 '12 at 14:23









                        user1712021

                        47143




                        47143












                        • make that mvn -o for faster execution
                          – Nathan Bubna
                          Nov 30 '12 at 22:50






                        • 1




                          Does not work to suppress "Downloaded:" lines.
                          – ceving
                          Feb 28 '13 at 10:34






                        • 1




                          See my answer (quickshiftin) below which filters 'Downloaded:' lines too.
                          – quickshiftin
                          Oct 8 '13 at 20:42






                        • 9




                          If you use -o as recommended by @NathanBubna then that will put maven in 'offline' mode. If you don't already have the maven help plugin and dependent jars downloaded then the build will fail. Got burned by that for a bit, hope it helps somebody else.
                          – phillipuniverse
                          Feb 8 '14 at 22:52










                        • You might want to also add 2> /dev/null as otherwise you can get Picked up _JAVA_OPTIONS:
                          – Jakub Bochenski
                          Nov 2 '15 at 21:06


















                        • make that mvn -o for faster execution
                          – Nathan Bubna
                          Nov 30 '12 at 22:50






                        • 1




                          Does not work to suppress "Downloaded:" lines.
                          – ceving
                          Feb 28 '13 at 10:34






                        • 1




                          See my answer (quickshiftin) below which filters 'Downloaded:' lines too.
                          – quickshiftin
                          Oct 8 '13 at 20:42






                        • 9




                          If you use -o as recommended by @NathanBubna then that will put maven in 'offline' mode. If you don't already have the maven help plugin and dependent jars downloaded then the build will fail. Got burned by that for a bit, hope it helps somebody else.
                          – phillipuniverse
                          Feb 8 '14 at 22:52










                        • You might want to also add 2> /dev/null as otherwise you can get Picked up _JAVA_OPTIONS:
                          – Jakub Bochenski
                          Nov 2 '15 at 21:06
















                        make that mvn -o for faster execution
                        – Nathan Bubna
                        Nov 30 '12 at 22:50




                        make that mvn -o for faster execution
                        – Nathan Bubna
                        Nov 30 '12 at 22:50




                        1




                        1




                        Does not work to suppress "Downloaded:" lines.
                        – ceving
                        Feb 28 '13 at 10:34




                        Does not work to suppress "Downloaded:" lines.
                        – ceving
                        Feb 28 '13 at 10:34




                        1




                        1




                        See my answer (quickshiftin) below which filters 'Downloaded:' lines too.
                        – quickshiftin
                        Oct 8 '13 at 20:42




                        See my answer (quickshiftin) below which filters 'Downloaded:' lines too.
                        – quickshiftin
                        Oct 8 '13 at 20:42




                        9




                        9




                        If you use -o as recommended by @NathanBubna then that will put maven in 'offline' mode. If you don't already have the maven help plugin and dependent jars downloaded then the build will fail. Got burned by that for a bit, hope it helps somebody else.
                        – phillipuniverse
                        Feb 8 '14 at 22:52




                        If you use -o as recommended by @NathanBubna then that will put maven in 'offline' mode. If you don't already have the maven help plugin and dependent jars downloaded then the build will fail. Got burned by that for a bit, hope it helps somebody else.
                        – phillipuniverse
                        Feb 8 '14 at 22:52












                        You might want to also add 2> /dev/null as otherwise you can get Picked up _JAVA_OPTIONS:
                        – Jakub Bochenski
                        Nov 2 '15 at 21:06




                        You might want to also add 2> /dev/null as otherwise you can get Picked up _JAVA_OPTIONS:
                        – Jakub Bochenski
                        Nov 2 '15 at 21:06










                        up vote
                        28
                        down vote













                        The top answer is pretty garbage in my opinion, you have to use a bunch of grep to hack out the maven console output. Why not use the right tool for the job? Using xpath syntax is the best approach to retrieving the version number, since it is the intended method of accessing a XML data structure. The expression below is traversing the pom using the "local name" of the elements, in other words ignoring namespace declarations which may or may not be present in the xml.



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





                        share|improve this answer























                        • @bobmarksie I second that; using maven and scrubbing the output with regexp's is painful.
                          – mhvelplund
                          Jun 13 at 6:52








                        • 2




                          What about a project that inherits from a parent pom.
                          – Wes
                          Jun 15 at 11:53






                        • 1




                          "Why not use the right tool for the job?" IE !Maven?
                          – jeremyjjbrown
                          Jun 18 at 15:47

















                        up vote
                        28
                        down vote













                        The top answer is pretty garbage in my opinion, you have to use a bunch of grep to hack out the maven console output. Why not use the right tool for the job? Using xpath syntax is the best approach to retrieving the version number, since it is the intended method of accessing a XML data structure. The expression below is traversing the pom using the "local name" of the elements, in other words ignoring namespace declarations which may or may not be present in the xml.



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





                        share|improve this answer























                        • @bobmarksie I second that; using maven and scrubbing the output with regexp's is painful.
                          – mhvelplund
                          Jun 13 at 6:52








                        • 2




                          What about a project that inherits from a parent pom.
                          – Wes
                          Jun 15 at 11:53






                        • 1




                          "Why not use the right tool for the job?" IE !Maven?
                          – jeremyjjbrown
                          Jun 18 at 15:47















                        up vote
                        28
                        down vote










                        up vote
                        28
                        down vote









                        The top answer is pretty garbage in my opinion, you have to use a bunch of grep to hack out the maven console output. Why not use the right tool for the job? Using xpath syntax is the best approach to retrieving the version number, since it is the intended method of accessing a XML data structure. The expression below is traversing the pom using the "local name" of the elements, in other words ignoring namespace declarations which may or may not be present in the xml.



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





                        share|improve this answer














                        The top answer is pretty garbage in my opinion, you have to use a bunch of grep to hack out the maven console output. Why not use the right tool for the job? Using xpath syntax is the best approach to retrieving the version number, since it is the intended method of accessing a XML data structure. The expression below is traversing the pom using the "local name" of the elements, in other words ignoring namespace declarations which may or may not be present in the xml.



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






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited May 8 '16 at 15:52

























                        answered May 6 '16 at 19:20









                        lukeforehand

                        34439




                        34439












                        • @bobmarksie I second that; using maven and scrubbing the output with regexp's is painful.
                          – mhvelplund
                          Jun 13 at 6:52








                        • 2




                          What about a project that inherits from a parent pom.
                          – Wes
                          Jun 15 at 11:53






                        • 1




                          "Why not use the right tool for the job?" IE !Maven?
                          – jeremyjjbrown
                          Jun 18 at 15:47




















                        • @bobmarksie I second that; using maven and scrubbing the output with regexp's is painful.
                          – mhvelplund
                          Jun 13 at 6:52








                        • 2




                          What about a project that inherits from a parent pom.
                          – Wes
                          Jun 15 at 11:53






                        • 1




                          "Why not use the right tool for the job?" IE !Maven?
                          – jeremyjjbrown
                          Jun 18 at 15:47


















                        @bobmarksie I second that; using maven and scrubbing the output with regexp's is painful.
                        – mhvelplund
                        Jun 13 at 6:52






                        @bobmarksie I second that; using maven and scrubbing the output with regexp's is painful.
                        – mhvelplund
                        Jun 13 at 6:52






                        2




                        2




                        What about a project that inherits from a parent pom.
                        – Wes
                        Jun 15 at 11:53




                        What about a project that inherits from a parent pom.
                        – Wes
                        Jun 15 at 11:53




                        1




                        1




                        "Why not use the right tool for the job?" IE !Maven?
                        – jeremyjjbrown
                        Jun 18 at 15:47






                        "Why not use the right tool for the job?" IE !Maven?
                        – jeremyjjbrown
                        Jun 18 at 15:47












                        up vote
                        23
                        down vote













                        This will avoid the need for grepping off log entries from the output:



                        mvn -Dexec.executable='echo' -Dexec.args='${project.version}' --non-recursive exec:exec -q





                        share|improve this answer























                        • While this code may answer the question, providing additional context regarding why and/or how it answers the question would significantly improve its long-term value. Please edit your answer to add some explanation.
                          – Toby Speight
                          Apr 12 '16 at 13:02






                        • 1




                          I think for this approach it is necessary to put the "-q" after mvn.
                          – Rudge
                          Aug 7 '17 at 19:47










                        • This is better than the accepted solution; no need to filter out maven noisy output.
                          – Willian Mitsuda
                          Jun 20 at 3:17















                        up vote
                        23
                        down vote













                        This will avoid the need for grepping off log entries from the output:



                        mvn -Dexec.executable='echo' -Dexec.args='${project.version}' --non-recursive exec:exec -q





                        share|improve this answer























                        • While this code may answer the question, providing additional context regarding why and/or how it answers the question would significantly improve its long-term value. Please edit your answer to add some explanation.
                          – Toby Speight
                          Apr 12 '16 at 13:02






                        • 1




                          I think for this approach it is necessary to put the "-q" after mvn.
                          – Rudge
                          Aug 7 '17 at 19:47










                        • This is better than the accepted solution; no need to filter out maven noisy output.
                          – Willian Mitsuda
                          Jun 20 at 3:17













                        up vote
                        23
                        down vote










                        up vote
                        23
                        down vote









                        This will avoid the need for grepping off log entries from the output:



                        mvn -Dexec.executable='echo' -Dexec.args='${project.version}' --non-recursive exec:exec -q





                        share|improve this answer














                        This will avoid the need for grepping off log entries from the output:



                        mvn -Dexec.executable='echo' -Dexec.args='${project.version}' --non-recursive exec:exec -q






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Sep 29 '17 at 15:10









                        approxiblue

                        5,686123849




                        5,686123849










                        answered Apr 12 '16 at 12:08









                        Jose Alban

                        3,4792315




                        3,4792315












                        • While this code may answer the question, providing additional context regarding why and/or how it answers the question would significantly improve its long-term value. Please edit your answer to add some explanation.
                          – Toby Speight
                          Apr 12 '16 at 13:02






                        • 1




                          I think for this approach it is necessary to put the "-q" after mvn.
                          – Rudge
                          Aug 7 '17 at 19:47










                        • This is better than the accepted solution; no need to filter out maven noisy output.
                          – Willian Mitsuda
                          Jun 20 at 3:17


















                        • While this code may answer the question, providing additional context regarding why and/or how it answers the question would significantly improve its long-term value. Please edit your answer to add some explanation.
                          – Toby Speight
                          Apr 12 '16 at 13:02






                        • 1




                          I think for this approach it is necessary to put the "-q" after mvn.
                          – Rudge
                          Aug 7 '17 at 19:47










                        • This is better than the accepted solution; no need to filter out maven noisy output.
                          – Willian Mitsuda
                          Jun 20 at 3:17
















                        While this code may answer the question, providing additional context regarding why and/or how it answers the question would significantly improve its long-term value. Please edit your answer to add some explanation.
                        – Toby Speight
                        Apr 12 '16 at 13:02




                        While this code may answer the question, providing additional context regarding why and/or how it answers the question would significantly improve its long-term value. Please edit your answer to add some explanation.
                        – Toby Speight
                        Apr 12 '16 at 13:02




                        1




                        1




                        I think for this approach it is necessary to put the "-q" after mvn.
                        – Rudge
                        Aug 7 '17 at 19:47




                        I think for this approach it is necessary to put the "-q" after mvn.
                        – Rudge
                        Aug 7 '17 at 19:47












                        This is better than the accepted solution; no need to filter out maven noisy output.
                        – Willian Mitsuda
                        Jun 20 at 3:17




                        This is better than the accepted solution; no need to filter out maven noisy output.
                        – Willian Mitsuda
                        Jun 20 at 3:17










                        up vote
                        12
                        down vote













                        I have been doing some research and found the following:




                        1. Maven has been blamed for not being easily integrable within system operation scripts since it does not follow some good practices regarding CLI tools. (ref: https://youtu.be/1ILEw6Qca3U?t=372)



                        2. Inspired on the previous assertion, I decided to give a look on maven's source code as well as on maven-help-plugin. It seems that they have fixed a little bit the maven's -q switch (I am using version 3.5.3), so now if you pass it, you won't get all the annoying non-sense logging stuff that prevents maven from being used within automated scripts. So you should be able to use something like this:



                          mvn help:evaluate -Dexpression=project.version -q


                          The problem is that this command prints nothing because by default the help plugin outputs through the logger which has been silenced by the -q switch. (latest available version of the plugin is 3.1.0 released on June, 3rd 2018)




                        3. Karl Heinz Marbaise (https://github.com/khmarbaise) fixed it by adding an optional parameter that allows you to call it in the following way:



                          mvn help:evaluate -Dexpression=project.version -q -DforceStdout


                          The commit description is available at: (https://github.com/apache/maven-help-plugin/commit/316656983d780c04031bbadd97d4ab245c84d014)








                        share|improve this answer



















                        • 1




                          This would be my preferred solution and is also mentioned on the official help pages. However, it doesn't work for me. I am on MacOS with Maven v3.5.3. When I don't use the -q switch it prints out the version correctly (in between the log lines). Any ideas?
                          – gucce
                          Jul 12 at 12:23












                        • Hi @gucce, What is the exact command line you are using? I am also on a Mac OSX with Maven 3.5.3. Are you using the latest version of the plugin?
                          – montoyaedu
                          Jul 16 at 12:04






                        • 1




                          This should be the accepted answer. It works in Windows and Linux (I didn't try Max OSX), and it's simple. Thank you.
                          – trash80
                          Jul 24 at 18:10






                        • 1




                          @montoyaedu Sorry for not responding. In the meantime I have updated to maven 3.5.4 which works fine with the -q switch.
                          – gucce
                          Oct 10 at 8:58










                        • @gucce I can confirm that maven version used affects this: with 3.5.2 (package version on debian 9) the output with -q -DforceStdout was empty, even ensuring version 3.1.0 of the plugin was used with pluginManagement) ; i configured maven wrapper with version 3.5.4 of maven, and it worked correctly
                          – Gorkk
                          Oct 26 at 16:29















                        up vote
                        12
                        down vote













                        I have been doing some research and found the following:




                        1. Maven has been blamed for not being easily integrable within system operation scripts since it does not follow some good practices regarding CLI tools. (ref: https://youtu.be/1ILEw6Qca3U?t=372)



                        2. Inspired on the previous assertion, I decided to give a look on maven's source code as well as on maven-help-plugin. It seems that they have fixed a little bit the maven's -q switch (I am using version 3.5.3), so now if you pass it, you won't get all the annoying non-sense logging stuff that prevents maven from being used within automated scripts. So you should be able to use something like this:



                          mvn help:evaluate -Dexpression=project.version -q


                          The problem is that this command prints nothing because by default the help plugin outputs through the logger which has been silenced by the -q switch. (latest available version of the plugin is 3.1.0 released on June, 3rd 2018)




                        3. Karl Heinz Marbaise (https://github.com/khmarbaise) fixed it by adding an optional parameter that allows you to call it in the following way:



                          mvn help:evaluate -Dexpression=project.version -q -DforceStdout


                          The commit description is available at: (https://github.com/apache/maven-help-plugin/commit/316656983d780c04031bbadd97d4ab245c84d014)








                        share|improve this answer



















                        • 1




                          This would be my preferred solution and is also mentioned on the official help pages. However, it doesn't work for me. I am on MacOS with Maven v3.5.3. When I don't use the -q switch it prints out the version correctly (in between the log lines). Any ideas?
                          – gucce
                          Jul 12 at 12:23












                        • Hi @gucce, What is the exact command line you are using? I am also on a Mac OSX with Maven 3.5.3. Are you using the latest version of the plugin?
                          – montoyaedu
                          Jul 16 at 12:04






                        • 1




                          This should be the accepted answer. It works in Windows and Linux (I didn't try Max OSX), and it's simple. Thank you.
                          – trash80
                          Jul 24 at 18:10






                        • 1




                          @montoyaedu Sorry for not responding. In the meantime I have updated to maven 3.5.4 which works fine with the -q switch.
                          – gucce
                          Oct 10 at 8:58










                        • @gucce I can confirm that maven version used affects this: with 3.5.2 (package version on debian 9) the output with -q -DforceStdout was empty, even ensuring version 3.1.0 of the plugin was used with pluginManagement) ; i configured maven wrapper with version 3.5.4 of maven, and it worked correctly
                          – Gorkk
                          Oct 26 at 16:29













                        up vote
                        12
                        down vote










                        up vote
                        12
                        down vote









                        I have been doing some research and found the following:




                        1. Maven has been blamed for not being easily integrable within system operation scripts since it does not follow some good practices regarding CLI tools. (ref: https://youtu.be/1ILEw6Qca3U?t=372)



                        2. Inspired on the previous assertion, I decided to give a look on maven's source code as well as on maven-help-plugin. It seems that they have fixed a little bit the maven's -q switch (I am using version 3.5.3), so now if you pass it, you won't get all the annoying non-sense logging stuff that prevents maven from being used within automated scripts. So you should be able to use something like this:



                          mvn help:evaluate -Dexpression=project.version -q


                          The problem is that this command prints nothing because by default the help plugin outputs through the logger which has been silenced by the -q switch. (latest available version of the plugin is 3.1.0 released on June, 3rd 2018)




                        3. Karl Heinz Marbaise (https://github.com/khmarbaise) fixed it by adding an optional parameter that allows you to call it in the following way:



                          mvn help:evaluate -Dexpression=project.version -q -DforceStdout


                          The commit description is available at: (https://github.com/apache/maven-help-plugin/commit/316656983d780c04031bbadd97d4ab245c84d014)








                        share|improve this answer














                        I have been doing some research and found the following:




                        1. Maven has been blamed for not being easily integrable within system operation scripts since it does not follow some good practices regarding CLI tools. (ref: https://youtu.be/1ILEw6Qca3U?t=372)



                        2. Inspired on the previous assertion, I decided to give a look on maven's source code as well as on maven-help-plugin. It seems that they have fixed a little bit the maven's -q switch (I am using version 3.5.3), so now if you pass it, you won't get all the annoying non-sense logging stuff that prevents maven from being used within automated scripts. So you should be able to use something like this:



                          mvn help:evaluate -Dexpression=project.version -q


                          The problem is that this command prints nothing because by default the help plugin outputs through the logger which has been silenced by the -q switch. (latest available version of the plugin is 3.1.0 released on June, 3rd 2018)




                        3. Karl Heinz Marbaise (https://github.com/khmarbaise) fixed it by adding an optional parameter that allows you to call it in the following way:



                          mvn help:evaluate -Dexpression=project.version -q -DforceStdout


                          The commit description is available at: (https://github.com/apache/maven-help-plugin/commit/316656983d780c04031bbadd97d4ab245c84d014)









                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Jun 20 at 9:23

























                        answered May 30 at 21:45









                        montoyaedu

                        14618




                        14618








                        • 1




                          This would be my preferred solution and is also mentioned on the official help pages. However, it doesn't work for me. I am on MacOS with Maven v3.5.3. When I don't use the -q switch it prints out the version correctly (in between the log lines). Any ideas?
                          – gucce
                          Jul 12 at 12:23












                        • Hi @gucce, What is the exact command line you are using? I am also on a Mac OSX with Maven 3.5.3. Are you using the latest version of the plugin?
                          – montoyaedu
                          Jul 16 at 12:04






                        • 1




                          This should be the accepted answer. It works in Windows and Linux (I didn't try Max OSX), and it's simple. Thank you.
                          – trash80
                          Jul 24 at 18:10






                        • 1




                          @montoyaedu Sorry for not responding. In the meantime I have updated to maven 3.5.4 which works fine with the -q switch.
                          – gucce
                          Oct 10 at 8:58










                        • @gucce I can confirm that maven version used affects this: with 3.5.2 (package version on debian 9) the output with -q -DforceStdout was empty, even ensuring version 3.1.0 of the plugin was used with pluginManagement) ; i configured maven wrapper with version 3.5.4 of maven, and it worked correctly
                          – Gorkk
                          Oct 26 at 16:29














                        • 1




                          This would be my preferred solution and is also mentioned on the official help pages. However, it doesn't work for me. I am on MacOS with Maven v3.5.3. When I don't use the -q switch it prints out the version correctly (in between the log lines). Any ideas?
                          – gucce
                          Jul 12 at 12:23












                        • Hi @gucce, What is the exact command line you are using? I am also on a Mac OSX with Maven 3.5.3. Are you using the latest version of the plugin?
                          – montoyaedu
                          Jul 16 at 12:04






                        • 1




                          This should be the accepted answer. It works in Windows and Linux (I didn't try Max OSX), and it's simple. Thank you.
                          – trash80
                          Jul 24 at 18:10






                        • 1




                          @montoyaedu Sorry for not responding. In the meantime I have updated to maven 3.5.4 which works fine with the -q switch.
                          – gucce
                          Oct 10 at 8:58










                        • @gucce I can confirm that maven version used affects this: with 3.5.2 (package version on debian 9) the output with -q -DforceStdout was empty, even ensuring version 3.1.0 of the plugin was used with pluginManagement) ; i configured maven wrapper with version 3.5.4 of maven, and it worked correctly
                          – Gorkk
                          Oct 26 at 16:29








                        1




                        1




                        This would be my preferred solution and is also mentioned on the official help pages. However, it doesn't work for me. I am on MacOS with Maven v3.5.3. When I don't use the -q switch it prints out the version correctly (in between the log lines). Any ideas?
                        – gucce
                        Jul 12 at 12:23






                        This would be my preferred solution and is also mentioned on the official help pages. However, it doesn't work for me. I am on MacOS with Maven v3.5.3. When I don't use the -q switch it prints out the version correctly (in between the log lines). Any ideas?
                        – gucce
                        Jul 12 at 12:23














                        Hi @gucce, What is the exact command line you are using? I am also on a Mac OSX with Maven 3.5.3. Are you using the latest version of the plugin?
                        – montoyaedu
                        Jul 16 at 12:04




                        Hi @gucce, What is the exact command line you are using? I am also on a Mac OSX with Maven 3.5.3. Are you using the latest version of the plugin?
                        – montoyaedu
                        Jul 16 at 12:04




                        1




                        1




                        This should be the accepted answer. It works in Windows and Linux (I didn't try Max OSX), and it's simple. Thank you.
                        – trash80
                        Jul 24 at 18:10




                        This should be the accepted answer. It works in Windows and Linux (I didn't try Max OSX), and it's simple. Thank you.
                        – trash80
                        Jul 24 at 18:10




                        1




                        1




                        @montoyaedu Sorry for not responding. In the meantime I have updated to maven 3.5.4 which works fine with the -q switch.
                        – gucce
                        Oct 10 at 8:58




                        @montoyaedu Sorry for not responding. In the meantime I have updated to maven 3.5.4 which works fine with the -q switch.
                        – gucce
                        Oct 10 at 8:58












                        @gucce I can confirm that maven version used affects this: with 3.5.2 (package version on debian 9) the output with -q -DforceStdout was empty, even ensuring version 3.1.0 of the plugin was used with pluginManagement) ; i configured maven wrapper with version 3.5.4 of maven, and it worked correctly
                        – Gorkk
                        Oct 26 at 16:29




                        @gucce I can confirm that maven version used affects this: with 3.5.2 (package version on debian 9) the output with -q -DforceStdout was empty, even ensuring version 3.1.0 of the plugin was used with pluginManagement) ; i configured maven wrapper with version 3.5.4 of maven, and it worked correctly
                        – Gorkk
                        Oct 26 at 16:29










                        up vote
                        11
                        down vote













                        python -c "import xml.etree.ElementTree as ET; 
                        print(ET.parse(open('pom.xml')).getroot().find(
                        '{http://maven.apache.org/POM/4.0.0}version').text)"


                        As long as you have python 2.5 or greater, this should work. If you have a lower version than that, install python-lxml and change the import to lxml.etree. This method is quick and doesn't require downloading any extra plugins. It also works on malformed pom.xml files that don't validate with xmllint, like the ones I need to parse. Tested on Mac and Linux.






                        share|improve this answer

























                          up vote
                          11
                          down vote













                          python -c "import xml.etree.ElementTree as ET; 
                          print(ET.parse(open('pom.xml')).getroot().find(
                          '{http://maven.apache.org/POM/4.0.0}version').text)"


                          As long as you have python 2.5 or greater, this should work. If you have a lower version than that, install python-lxml and change the import to lxml.etree. This method is quick and doesn't require downloading any extra plugins. It also works on malformed pom.xml files that don't validate with xmllint, like the ones I need to parse. Tested on Mac and Linux.






                          share|improve this answer























                            up vote
                            11
                            down vote










                            up vote
                            11
                            down vote









                            python -c "import xml.etree.ElementTree as ET; 
                            print(ET.parse(open('pom.xml')).getroot().find(
                            '{http://maven.apache.org/POM/4.0.0}version').text)"


                            As long as you have python 2.5 or greater, this should work. If you have a lower version than that, install python-lxml and change the import to lxml.etree. This method is quick and doesn't require downloading any extra plugins. It also works on malformed pom.xml files that don't validate with xmllint, like the ones I need to parse. Tested on Mac and Linux.






                            share|improve this answer












                            python -c "import xml.etree.ElementTree as ET; 
                            print(ET.parse(open('pom.xml')).getroot().find(
                            '{http://maven.apache.org/POM/4.0.0}version').text)"


                            As long as you have python 2.5 or greater, this should work. If you have a lower version than that, install python-lxml and change the import to lxml.etree. This method is quick and doesn't require downloading any extra plugins. It also works on malformed pom.xml files that don't validate with xmllint, like the ones I need to parse. Tested on Mac and Linux.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 20 '15 at 23:43









                            pdr

                            15112




                            15112






















                                up vote
                                8
                                down vote













                                I kept running into side cases when using some of the other answers here, so here's yet another alternative.



                                version=$(printf 'VERt${project.version}' | mvn help:evaluate | grep '^VER' | cut -f2)





                                share|improve this answer

















                                • 1




                                  printf 'VERt${project.version}' | mvn help:evaluate 2> /dev/null | grep '^VER' | cut -f2
                                  – yegeniy
                                  Oct 24 '16 at 15:37















                                up vote
                                8
                                down vote













                                I kept running into side cases when using some of the other answers here, so here's yet another alternative.



                                version=$(printf 'VERt${project.version}' | mvn help:evaluate | grep '^VER' | cut -f2)





                                share|improve this answer

















                                • 1




                                  printf 'VERt${project.version}' | mvn help:evaluate 2> /dev/null | grep '^VER' | cut -f2
                                  – yegeniy
                                  Oct 24 '16 at 15:37













                                up vote
                                8
                                down vote










                                up vote
                                8
                                down vote









                                I kept running into side cases when using some of the other answers here, so here's yet another alternative.



                                version=$(printf 'VERt${project.version}' | mvn help:evaluate | grep '^VER' | cut -f2)





                                share|improve this answer












                                I kept running into side cases when using some of the other answers here, so here's yet another alternative.



                                version=$(printf 'VERt${project.version}' | mvn help:evaluate | grep '^VER' | cut -f2)






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Jul 6 '16 at 12:21









                                slow

                                41059




                                41059








                                • 1




                                  printf 'VERt${project.version}' | mvn help:evaluate 2> /dev/null | grep '^VER' | cut -f2
                                  – yegeniy
                                  Oct 24 '16 at 15:37














                                • 1




                                  printf 'VERt${project.version}' | mvn help:evaluate 2> /dev/null | grep '^VER' | cut -f2
                                  – yegeniy
                                  Oct 24 '16 at 15:37








                                1




                                1




                                printf 'VERt${project.version}' | mvn help:evaluate 2> /dev/null | grep '^VER' | cut -f2
                                – yegeniy
                                Oct 24 '16 at 15:37




                                printf 'VERt${project.version}' | mvn help:evaluate 2> /dev/null | grep '^VER' | cut -f2
                                – yegeniy
                                Oct 24 '16 at 15:37










                                up vote
                                7
                                down vote













                                If you don't mind to write the version into a temporary file, there is another solution (without grep/sed) that works well for me. (EDIT: see rjrjr's answer for a much simpler solution without any temporary file hassle)



                                I use the Exec Maven Plugin along with the echo binary. In contrast to the Maven Help Plugin, the Exec Plugin allows output redirection into a file, which can be used to bypass grep/sed, and makes it even possible to parse strange things like multiline version strings (with CDATA block in version tag), at least to a certain extent.



                                #!/usr/bin/env sh

                                MVN_VERSION=""
                                VERSION_FILE=$( mktemp mvn_project_version_XXXXX )
                                trap "rm -f -- "$VERSION_FILE"" INT EXIT

                                mvn -Dexec.executable="echo"
                                -Dexec.args='${project.version}'
                                -Dexec.outputFile="$VERSION_FILE"
                                --non-recursive
                                --batch-mode
                                org.codehaus.mojo:exec-maven-plugin:1.3.1:exec > /dev/null 2>&1 ||
                                { echo "Maven invocation failed!" 1>&2; exit 1; }

                                # if you just care about the first line of the version, which will be
                                # sufficent for pretty much every use case I can imagine, you can use
                                # the read builtin
                                [ -s "$VERSION_FILE" ] && read -r MVN_VERSION < "$VERSION_FILE"

                                # Otherwise, you could use cat.
                                # Note that this still has issues when there are leading whitespaces
                                # in the multiline version string
                                #MVN_VERSION=$( cat "$VERSION_FILE" )

                                printf "Maven project version: %sn" "$MVN_VERSION"





                                share|improve this answer



















                                • 6




                                  This is a much better solution than the helper plugin, and you don't need all that i/o. Just add a -q flag and the only output will be the version. So: mvn -q -Dexec.executable="echo" -Dexec.args='${project.version}' --non-recursive org.codehaus.mojo:exec-maven-plugin:1.3.1:exec
                                  – rjrjr
                                  Oct 22 '14 at 17:51












                                • rjrjr: Great! The only thing I need to mention here is error handling: If mvn fails, you get an invalid version string. So some validation is required, like checking mvn return code or the string itself.
                                  – Tom
                                  Oct 23 '14 at 6:19















                                up vote
                                7
                                down vote













                                If you don't mind to write the version into a temporary file, there is another solution (without grep/sed) that works well for me. (EDIT: see rjrjr's answer for a much simpler solution without any temporary file hassle)



                                I use the Exec Maven Plugin along with the echo binary. In contrast to the Maven Help Plugin, the Exec Plugin allows output redirection into a file, which can be used to bypass grep/sed, and makes it even possible to parse strange things like multiline version strings (with CDATA block in version tag), at least to a certain extent.



                                #!/usr/bin/env sh

                                MVN_VERSION=""
                                VERSION_FILE=$( mktemp mvn_project_version_XXXXX )
                                trap "rm -f -- "$VERSION_FILE"" INT EXIT

                                mvn -Dexec.executable="echo"
                                -Dexec.args='${project.version}'
                                -Dexec.outputFile="$VERSION_FILE"
                                --non-recursive
                                --batch-mode
                                org.codehaus.mojo:exec-maven-plugin:1.3.1:exec > /dev/null 2>&1 ||
                                { echo "Maven invocation failed!" 1>&2; exit 1; }

                                # if you just care about the first line of the version, which will be
                                # sufficent for pretty much every use case I can imagine, you can use
                                # the read builtin
                                [ -s "$VERSION_FILE" ] && read -r MVN_VERSION < "$VERSION_FILE"

                                # Otherwise, you could use cat.
                                # Note that this still has issues when there are leading whitespaces
                                # in the multiline version string
                                #MVN_VERSION=$( cat "$VERSION_FILE" )

                                printf "Maven project version: %sn" "$MVN_VERSION"





                                share|improve this answer



















                                • 6




                                  This is a much better solution than the helper plugin, and you don't need all that i/o. Just add a -q flag and the only output will be the version. So: mvn -q -Dexec.executable="echo" -Dexec.args='${project.version}' --non-recursive org.codehaus.mojo:exec-maven-plugin:1.3.1:exec
                                  – rjrjr
                                  Oct 22 '14 at 17:51












                                • rjrjr: Great! The only thing I need to mention here is error handling: If mvn fails, you get an invalid version string. So some validation is required, like checking mvn return code or the string itself.
                                  – Tom
                                  Oct 23 '14 at 6:19













                                up vote
                                7
                                down vote










                                up vote
                                7
                                down vote









                                If you don't mind to write the version into a temporary file, there is another solution (without grep/sed) that works well for me. (EDIT: see rjrjr's answer for a much simpler solution without any temporary file hassle)



                                I use the Exec Maven Plugin along with the echo binary. In contrast to the Maven Help Plugin, the Exec Plugin allows output redirection into a file, which can be used to bypass grep/sed, and makes it even possible to parse strange things like multiline version strings (with CDATA block in version tag), at least to a certain extent.



                                #!/usr/bin/env sh

                                MVN_VERSION=""
                                VERSION_FILE=$( mktemp mvn_project_version_XXXXX )
                                trap "rm -f -- "$VERSION_FILE"" INT EXIT

                                mvn -Dexec.executable="echo"
                                -Dexec.args='${project.version}'
                                -Dexec.outputFile="$VERSION_FILE"
                                --non-recursive
                                --batch-mode
                                org.codehaus.mojo:exec-maven-plugin:1.3.1:exec > /dev/null 2>&1 ||
                                { echo "Maven invocation failed!" 1>&2; exit 1; }

                                # if you just care about the first line of the version, which will be
                                # sufficent for pretty much every use case I can imagine, you can use
                                # the read builtin
                                [ -s "$VERSION_FILE" ] && read -r MVN_VERSION < "$VERSION_FILE"

                                # Otherwise, you could use cat.
                                # Note that this still has issues when there are leading whitespaces
                                # in the multiline version string
                                #MVN_VERSION=$( cat "$VERSION_FILE" )

                                printf "Maven project version: %sn" "$MVN_VERSION"





                                share|improve this answer














                                If you don't mind to write the version into a temporary file, there is another solution (without grep/sed) that works well for me. (EDIT: see rjrjr's answer for a much simpler solution without any temporary file hassle)



                                I use the Exec Maven Plugin along with the echo binary. In contrast to the Maven Help Plugin, the Exec Plugin allows output redirection into a file, which can be used to bypass grep/sed, and makes it even possible to parse strange things like multiline version strings (with CDATA block in version tag), at least to a certain extent.



                                #!/usr/bin/env sh

                                MVN_VERSION=""
                                VERSION_FILE=$( mktemp mvn_project_version_XXXXX )
                                trap "rm -f -- "$VERSION_FILE"" INT EXIT

                                mvn -Dexec.executable="echo"
                                -Dexec.args='${project.version}'
                                -Dexec.outputFile="$VERSION_FILE"
                                --non-recursive
                                --batch-mode
                                org.codehaus.mojo:exec-maven-plugin:1.3.1:exec > /dev/null 2>&1 ||
                                { echo "Maven invocation failed!" 1>&2; exit 1; }

                                # if you just care about the first line of the version, which will be
                                # sufficent for pretty much every use case I can imagine, you can use
                                # the read builtin
                                [ -s "$VERSION_FILE" ] && read -r MVN_VERSION < "$VERSION_FILE"

                                # Otherwise, you could use cat.
                                # Note that this still has issues when there are leading whitespaces
                                # in the multiline version string
                                #MVN_VERSION=$( cat "$VERSION_FILE" )

                                printf "Maven project version: %sn" "$MVN_VERSION"






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Oct 23 '14 at 6:13

























                                answered Jun 12 '14 at 12:15









                                Tom

                                40568




                                40568








                                • 6




                                  This is a much better solution than the helper plugin, and you don't need all that i/o. Just add a -q flag and the only output will be the version. So: mvn -q -Dexec.executable="echo" -Dexec.args='${project.version}' --non-recursive org.codehaus.mojo:exec-maven-plugin:1.3.1:exec
                                  – rjrjr
                                  Oct 22 '14 at 17:51












                                • rjrjr: Great! The only thing I need to mention here is error handling: If mvn fails, you get an invalid version string. So some validation is required, like checking mvn return code or the string itself.
                                  – Tom
                                  Oct 23 '14 at 6:19














                                • 6




                                  This is a much better solution than the helper plugin, and you don't need all that i/o. Just add a -q flag and the only output will be the version. So: mvn -q -Dexec.executable="echo" -Dexec.args='${project.version}' --non-recursive org.codehaus.mojo:exec-maven-plugin:1.3.1:exec
                                  – rjrjr
                                  Oct 22 '14 at 17:51












                                • rjrjr: Great! The only thing I need to mention here is error handling: If mvn fails, you get an invalid version string. So some validation is required, like checking mvn return code or the string itself.
                                  – Tom
                                  Oct 23 '14 at 6:19








                                6




                                6




                                This is a much better solution than the helper plugin, and you don't need all that i/o. Just add a -q flag and the only output will be the version. So: mvn -q -Dexec.executable="echo" -Dexec.args='${project.version}' --non-recursive org.codehaus.mojo:exec-maven-plugin:1.3.1:exec
                                – rjrjr
                                Oct 22 '14 at 17:51






                                This is a much better solution than the helper plugin, and you don't need all that i/o. Just add a -q flag and the only output will be the version. So: mvn -q -Dexec.executable="echo" -Dexec.args='${project.version}' --non-recursive org.codehaus.mojo:exec-maven-plugin:1.3.1:exec
                                – rjrjr
                                Oct 22 '14 at 17:51














                                rjrjr: Great! The only thing I need to mention here is error handling: If mvn fails, you get an invalid version string. So some validation is required, like checking mvn return code or the string itself.
                                – Tom
                                Oct 23 '14 at 6:19




                                rjrjr: Great! The only thing I need to mention here is error handling: If mvn fails, you get an invalid version string. So some validation is required, like checking mvn return code or the string itself.
                                – Tom
                                Oct 23 '14 at 6:19










                                up vote
                                5
                                down vote













                                I noticed some spurious Downloaded: lines coming in the output that were breaking my original assignment. Here's the filter I've settled on; hope it helps!



                                version=$(mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | egrep -v '^[|Downloading:' | tr -d ' n')


                                EDIT



                                Not 100% sure why, but when running this through a post-build script in Jenkins, the output was coming out as [INFO]version, e.g. [INFO]0.3.2.



                                I dumped the output to a file and ran it through my first filter directly from BASH, it works fine.., so again, unsure what's going on in Jenkins land.



                                To get it 100% in Jenkins, I've added a follow-up sed filter; here's my latest



                                version=$(mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | egrep -v '^[|Downloading:' | tr -d ' n' | sed -E 's/[.*]//g')


                                EDIT



                                One last note here.. I found out tr was still resulting in things like /r/n0.3.2 (again only when running via Jenkins). Switched to awk and the problem has gone away! My final working result



                                mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version 
                                | egrep -v '^[|Downloading:' | sed 's/[^0-9.]//g' | awk 1 ORS=''





                                share|improve this answer



























                                  up vote
                                  5
                                  down vote













                                  I noticed some spurious Downloaded: lines coming in the output that were breaking my original assignment. Here's the filter I've settled on; hope it helps!



                                  version=$(mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | egrep -v '^[|Downloading:' | tr -d ' n')


                                  EDIT



                                  Not 100% sure why, but when running this through a post-build script in Jenkins, the output was coming out as [INFO]version, e.g. [INFO]0.3.2.



                                  I dumped the output to a file and ran it through my first filter directly from BASH, it works fine.., so again, unsure what's going on in Jenkins land.



                                  To get it 100% in Jenkins, I've added a follow-up sed filter; here's my latest



                                  version=$(mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | egrep -v '^[|Downloading:' | tr -d ' n' | sed -E 's/[.*]//g')


                                  EDIT



                                  One last note here.. I found out tr was still resulting in things like /r/n0.3.2 (again only when running via Jenkins). Switched to awk and the problem has gone away! My final working result



                                  mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version 
                                  | egrep -v '^[|Downloading:' | sed 's/[^0-9.]//g' | awk 1 ORS=''





                                  share|improve this answer

























                                    up vote
                                    5
                                    down vote










                                    up vote
                                    5
                                    down vote









                                    I noticed some spurious Downloaded: lines coming in the output that were breaking my original assignment. Here's the filter I've settled on; hope it helps!



                                    version=$(mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | egrep -v '^[|Downloading:' | tr -d ' n')


                                    EDIT



                                    Not 100% sure why, but when running this through a post-build script in Jenkins, the output was coming out as [INFO]version, e.g. [INFO]0.3.2.



                                    I dumped the output to a file and ran it through my first filter directly from BASH, it works fine.., so again, unsure what's going on in Jenkins land.



                                    To get it 100% in Jenkins, I've added a follow-up sed filter; here's my latest



                                    version=$(mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | egrep -v '^[|Downloading:' | tr -d ' n' | sed -E 's/[.*]//g')


                                    EDIT



                                    One last note here.. I found out tr was still resulting in things like /r/n0.3.2 (again only when running via Jenkins). Switched to awk and the problem has gone away! My final working result



                                    mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version 
                                    | egrep -v '^[|Downloading:' | sed 's/[^0-9.]//g' | awk 1 ORS=''





                                    share|improve this answer














                                    I noticed some spurious Downloaded: lines coming in the output that were breaking my original assignment. Here's the filter I've settled on; hope it helps!



                                    version=$(mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | egrep -v '^[|Downloading:' | tr -d ' n')


                                    EDIT



                                    Not 100% sure why, but when running this through a post-build script in Jenkins, the output was coming out as [INFO]version, e.g. [INFO]0.3.2.



                                    I dumped the output to a file and ran it through my first filter directly from BASH, it works fine.., so again, unsure what's going on in Jenkins land.



                                    To get it 100% in Jenkins, I've added a follow-up sed filter; here's my latest



                                    version=$(mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | egrep -v '^[|Downloading:' | tr -d ' n' | sed -E 's/[.*]//g')


                                    EDIT



                                    One last note here.. I found out tr was still resulting in things like /r/n0.3.2 (again only when running via Jenkins). Switched to awk and the problem has gone away! My final working result



                                    mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version 
                                    | egrep -v '^[|Downloading:' | sed 's/[^0-9.]//g' | awk 1 ORS=''






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Oct 28 '13 at 22:11

























                                    answered Oct 8 '13 at 20:41









                                    quickshiftin

                                    36k74768




                                    36k74768






















                                        up vote
                                        5
                                        down vote













                                        Just for the record, it's possible to configure Maven's Simple SLF4J logging directly in the command line to output only what we need by configuring:





                                        • org.slf4j.simpleLogger.defaultLogLevel=WARN and

                                        • org.slf4j.simpleLogger.log.org.apache.maven.plugins.help=INFO


                                        as documented at http://www.slf4j.org/api/org/slf4j/impl/SimpleLogger.html



                                        MAVEN_OPTS="
                                        -Dorg.slf4j.simpleLogger.defaultLogLevel=WARN
                                        -Dorg.slf4j.simpleLogger.log.org.apache.maven.plugins.help=INFO"
                                        mvn help:evaluate -o -Dexpression=project.version


                                        As a result, one can run simply tail -1 and get:



                                        $ MAVEN_OPTS="
                                        -Dorg.slf4j.simpleLogger.defaultLogLevel=WARN
                                        -Dorg.slf4j.simpleLogger.log.org.apache.maven.plugins.help=INFO"
                                        mvn help:evaluate -o -Dexpression=project.version | tail -1

                                        1.0.0-SNAPSHOT


                                        Note that this is a one-liner. MAVEN_OPTS are being rewritten only for this particular mvn execution.






                                        share|improve this answer

























                                          up vote
                                          5
                                          down vote













                                          Just for the record, it's possible to configure Maven's Simple SLF4J logging directly in the command line to output only what we need by configuring:





                                          • org.slf4j.simpleLogger.defaultLogLevel=WARN and

                                          • org.slf4j.simpleLogger.log.org.apache.maven.plugins.help=INFO


                                          as documented at http://www.slf4j.org/api/org/slf4j/impl/SimpleLogger.html



                                          MAVEN_OPTS="
                                          -Dorg.slf4j.simpleLogger.defaultLogLevel=WARN
                                          -Dorg.slf4j.simpleLogger.log.org.apache.maven.plugins.help=INFO"
                                          mvn help:evaluate -o -Dexpression=project.version


                                          As a result, one can run simply tail -1 and get:



                                          $ MAVEN_OPTS="
                                          -Dorg.slf4j.simpleLogger.defaultLogLevel=WARN
                                          -Dorg.slf4j.simpleLogger.log.org.apache.maven.plugins.help=INFO"
                                          mvn help:evaluate -o -Dexpression=project.version | tail -1

                                          1.0.0-SNAPSHOT


                                          Note that this is a one-liner. MAVEN_OPTS are being rewritten only for this particular mvn execution.






                                          share|improve this answer























                                            up vote
                                            5
                                            down vote










                                            up vote
                                            5
                                            down vote









                                            Just for the record, it's possible to configure Maven's Simple SLF4J logging directly in the command line to output only what we need by configuring:





                                            • org.slf4j.simpleLogger.defaultLogLevel=WARN and

                                            • org.slf4j.simpleLogger.log.org.apache.maven.plugins.help=INFO


                                            as documented at http://www.slf4j.org/api/org/slf4j/impl/SimpleLogger.html



                                            MAVEN_OPTS="
                                            -Dorg.slf4j.simpleLogger.defaultLogLevel=WARN
                                            -Dorg.slf4j.simpleLogger.log.org.apache.maven.plugins.help=INFO"
                                            mvn help:evaluate -o -Dexpression=project.version


                                            As a result, one can run simply tail -1 and get:



                                            $ MAVEN_OPTS="
                                            -Dorg.slf4j.simpleLogger.defaultLogLevel=WARN
                                            -Dorg.slf4j.simpleLogger.log.org.apache.maven.plugins.help=INFO"
                                            mvn help:evaluate -o -Dexpression=project.version | tail -1

                                            1.0.0-SNAPSHOT


                                            Note that this is a one-liner. MAVEN_OPTS are being rewritten only for this particular mvn execution.






                                            share|improve this answer












                                            Just for the record, it's possible to configure Maven's Simple SLF4J logging directly in the command line to output only what we need by configuring:





                                            • org.slf4j.simpleLogger.defaultLogLevel=WARN and

                                            • org.slf4j.simpleLogger.log.org.apache.maven.plugins.help=INFO


                                            as documented at http://www.slf4j.org/api/org/slf4j/impl/SimpleLogger.html



                                            MAVEN_OPTS="
                                            -Dorg.slf4j.simpleLogger.defaultLogLevel=WARN
                                            -Dorg.slf4j.simpleLogger.log.org.apache.maven.plugins.help=INFO"
                                            mvn help:evaluate -o -Dexpression=project.version


                                            As a result, one can run simply tail -1 and get:



                                            $ MAVEN_OPTS="
                                            -Dorg.slf4j.simpleLogger.defaultLogLevel=WARN
                                            -Dorg.slf4j.simpleLogger.log.org.apache.maven.plugins.help=INFO"
                                            mvn help:evaluate -o -Dexpression=project.version | tail -1

                                            1.0.0-SNAPSHOT


                                            Note that this is a one-liner. MAVEN_OPTS are being rewritten only for this particular mvn execution.







                                            share|improve this answer












                                            share|improve this answer



                                            share|improve this answer










                                            answered Aug 9 '16 at 17:14









                                            Stepan Vavra

                                            2,53211833




                                            2,53211833






















                                                up vote
                                                4
                                                down vote













                                                A simple maven only solution



                                                mvn -q -N org.codehaus.mojo:exec-maven-plugin:1.3.1:exec 
                                                -Dexec.executable='echo'
                                                -Dexec.args='${project.version}'


                                                And for bonus points parsed part of a version



                                                mvn -q -N org.codehaus.mojo:build-helper-maven-plugin:3.0.0:parse-version 
                                                org.codehaus.mojo:exec-maven-plugin:1.3.1:exec
                                                -Dexec.executable='echo'
                                                -Dexec.args='${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.incrementalVersion}'





                                                share|improve this answer





















                                                • 'echo' doesn't work in Windows.
                                                  – trash80
                                                  Jul 24 at 18:02















                                                up vote
                                                4
                                                down vote













                                                A simple maven only solution



                                                mvn -q -N org.codehaus.mojo:exec-maven-plugin:1.3.1:exec 
                                                -Dexec.executable='echo'
                                                -Dexec.args='${project.version}'


                                                And for bonus points parsed part of a version



                                                mvn -q -N org.codehaus.mojo:build-helper-maven-plugin:3.0.0:parse-version 
                                                org.codehaus.mojo:exec-maven-plugin:1.3.1:exec
                                                -Dexec.executable='echo'
                                                -Dexec.args='${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.incrementalVersion}'





                                                share|improve this answer





















                                                • 'echo' doesn't work in Windows.
                                                  – trash80
                                                  Jul 24 at 18:02













                                                up vote
                                                4
                                                down vote










                                                up vote
                                                4
                                                down vote









                                                A simple maven only solution



                                                mvn -q -N org.codehaus.mojo:exec-maven-plugin:1.3.1:exec 
                                                -Dexec.executable='echo'
                                                -Dexec.args='${project.version}'


                                                And for bonus points parsed part of a version



                                                mvn -q -N org.codehaus.mojo:build-helper-maven-plugin:3.0.0:parse-version 
                                                org.codehaus.mojo:exec-maven-plugin:1.3.1:exec
                                                -Dexec.executable='echo'
                                                -Dexec.args='${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.incrementalVersion}'





                                                share|improve this answer












                                                A simple maven only solution



                                                mvn -q -N org.codehaus.mojo:exec-maven-plugin:1.3.1:exec 
                                                -Dexec.executable='echo'
                                                -Dexec.args='${project.version}'


                                                And for bonus points parsed part of a version



                                                mvn -q -N org.codehaus.mojo:build-helper-maven-plugin:3.0.0:parse-version 
                                                org.codehaus.mojo:exec-maven-plugin:1.3.1:exec
                                                -Dexec.executable='echo'
                                                -Dexec.args='${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.incrementalVersion}'






                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Jul 13 '17 at 9:12









                                                Jakob O.

                                                19414




                                                19414












                                                • 'echo' doesn't work in Windows.
                                                  – trash80
                                                  Jul 24 at 18:02


















                                                • 'echo' doesn't work in Windows.
                                                  – trash80
                                                  Jul 24 at 18:02
















                                                'echo' doesn't work in Windows.
                                                – trash80
                                                Jul 24 at 18:02




                                                'echo' doesn't work in Windows.
                                                – trash80
                                                Jul 24 at 18:02










                                                up vote
                                                4
                                                down vote













                                                I've recently developed the Release Candidate Maven plugin that solves this exact problem so that you don't have to resort to any hacky shell scripts and parsing the output of the maven-help-plugin.



                                                For example, to print the version of your Maven project to a terminal, run:



                                                mvn com.smartcodeltd:release-candidate-maven-plugin:LATEST:version


                                                which gives output similar to maven-help-plugin:



                                                [INFO] Detected version: '1.0.0-SNAPSHOT'
                                                1.0.0-SNAPSHOT
                                                [INFO] ------------------------------------------------------------------------
                                                [INFO] BUILD SUCCESS
                                                [INFO] ------------------------------------------------------------------------


                                                However, you can also specify an arbitrary output format (so that the version could be picked up from the log by a CI server such as TeamCity):



                                                mvn com.smartcodeltd:release-candidate-maven-plugin:LATEST:version 
                                                -DoutputTemplate="##teamcity[setParameter name='env.PROJECT_VERSION' value='{{ version }}']"


                                                Which results in:



                                                [INFO] Detected version: '1.0.0-SNAPSHOT'
                                                ##teamcity[setParameter name='env.PROJECT_VERSION' value='1.0.0-SNAPSHOT']
                                                [INFO] ------------------------------------------------------------------------
                                                [INFO] BUILD SUCCESS
                                                [INFO] ------------------------------------------------------------------------


                                                To save the output to a file (so that a CI server such as Jenkins could use it):



                                                mvn com.smartcodeltd:release-candidate-maven-plugin:LATEST:version 
                                                -DoutputTemplate="PROJECT_VERSION={{ version }}"
                                                -DoutputUri="file://${project.basedir}/version.properties"


                                                The resulting version.properties file will look as follows:



                                                PROJECT_VERSION=1.0.0-SNAPSHOT




                                                On top of all the above, Release Candidate also allows you to set the version of your project (which is something you'd probably do on your CI server) based on the API version you've defined in your POM.



                                                If you'd like to see an example of Release Candidate being used as part of the Maven lifecycle, have a look at the pom.xml of my other open-source project - Build Monitor for Jenkins.






                                                share|improve this answer



























                                                  up vote
                                                  4
                                                  down vote













                                                  I've recently developed the Release Candidate Maven plugin that solves this exact problem so that you don't have to resort to any hacky shell scripts and parsing the output of the maven-help-plugin.



                                                  For example, to print the version of your Maven project to a terminal, run:



                                                  mvn com.smartcodeltd:release-candidate-maven-plugin:LATEST:version


                                                  which gives output similar to maven-help-plugin:



                                                  [INFO] Detected version: '1.0.0-SNAPSHOT'
                                                  1.0.0-SNAPSHOT
                                                  [INFO] ------------------------------------------------------------------------
                                                  [INFO] BUILD SUCCESS
                                                  [INFO] ------------------------------------------------------------------------


                                                  However, you can also specify an arbitrary output format (so that the version could be picked up from the log by a CI server such as TeamCity):



                                                  mvn com.smartcodeltd:release-candidate-maven-plugin:LATEST:version 
                                                  -DoutputTemplate="##teamcity[setParameter name='env.PROJECT_VERSION' value='{{ version }}']"


                                                  Which results in:



                                                  [INFO] Detected version: '1.0.0-SNAPSHOT'
                                                  ##teamcity[setParameter name='env.PROJECT_VERSION' value='1.0.0-SNAPSHOT']
                                                  [INFO] ------------------------------------------------------------------------
                                                  [INFO] BUILD SUCCESS
                                                  [INFO] ------------------------------------------------------------------------


                                                  To save the output to a file (so that a CI server such as Jenkins could use it):



                                                  mvn com.smartcodeltd:release-candidate-maven-plugin:LATEST:version 
                                                  -DoutputTemplate="PROJECT_VERSION={{ version }}"
                                                  -DoutputUri="file://${project.basedir}/version.properties"


                                                  The resulting version.properties file will look as follows:



                                                  PROJECT_VERSION=1.0.0-SNAPSHOT




                                                  On top of all the above, Release Candidate also allows you to set the version of your project (which is something you'd probably do on your CI server) based on the API version you've defined in your POM.



                                                  If you'd like to see an example of Release Candidate being used as part of the Maven lifecycle, have a look at the pom.xml of my other open-source project - Build Monitor for Jenkins.






                                                  share|improve this answer

























                                                    up vote
                                                    4
                                                    down vote










                                                    up vote
                                                    4
                                                    down vote









                                                    I've recently developed the Release Candidate Maven plugin that solves this exact problem so that you don't have to resort to any hacky shell scripts and parsing the output of the maven-help-plugin.



                                                    For example, to print the version of your Maven project to a terminal, run:



                                                    mvn com.smartcodeltd:release-candidate-maven-plugin:LATEST:version


                                                    which gives output similar to maven-help-plugin:



                                                    [INFO] Detected version: '1.0.0-SNAPSHOT'
                                                    1.0.0-SNAPSHOT
                                                    [INFO] ------------------------------------------------------------------------
                                                    [INFO] BUILD SUCCESS
                                                    [INFO] ------------------------------------------------------------------------


                                                    However, you can also specify an arbitrary output format (so that the version could be picked up from the log by a CI server such as TeamCity):



                                                    mvn com.smartcodeltd:release-candidate-maven-plugin:LATEST:version 
                                                    -DoutputTemplate="##teamcity[setParameter name='env.PROJECT_VERSION' value='{{ version }}']"


                                                    Which results in:



                                                    [INFO] Detected version: '1.0.0-SNAPSHOT'
                                                    ##teamcity[setParameter name='env.PROJECT_VERSION' value='1.0.0-SNAPSHOT']
                                                    [INFO] ------------------------------------------------------------------------
                                                    [INFO] BUILD SUCCESS
                                                    [INFO] ------------------------------------------------------------------------


                                                    To save the output to a file (so that a CI server such as Jenkins could use it):



                                                    mvn com.smartcodeltd:release-candidate-maven-plugin:LATEST:version 
                                                    -DoutputTemplate="PROJECT_VERSION={{ version }}"
                                                    -DoutputUri="file://${project.basedir}/version.properties"


                                                    The resulting version.properties file will look as follows:



                                                    PROJECT_VERSION=1.0.0-SNAPSHOT




                                                    On top of all the above, Release Candidate also allows you to set the version of your project (which is something you'd probably do on your CI server) based on the API version you've defined in your POM.



                                                    If you'd like to see an example of Release Candidate being used as part of the Maven lifecycle, have a look at the pom.xml of my other open-source project - Build Monitor for Jenkins.






                                                    share|improve this answer














                                                    I've recently developed the Release Candidate Maven plugin that solves this exact problem so that you don't have to resort to any hacky shell scripts and parsing the output of the maven-help-plugin.



                                                    For example, to print the version of your Maven project to a terminal, run:



                                                    mvn com.smartcodeltd:release-candidate-maven-plugin:LATEST:version


                                                    which gives output similar to maven-help-plugin:



                                                    [INFO] Detected version: '1.0.0-SNAPSHOT'
                                                    1.0.0-SNAPSHOT
                                                    [INFO] ------------------------------------------------------------------------
                                                    [INFO] BUILD SUCCESS
                                                    [INFO] ------------------------------------------------------------------------


                                                    However, you can also specify an arbitrary output format (so that the version could be picked up from the log by a CI server such as TeamCity):



                                                    mvn com.smartcodeltd:release-candidate-maven-plugin:LATEST:version 
                                                    -DoutputTemplate="##teamcity[setParameter name='env.PROJECT_VERSION' value='{{ version }}']"


                                                    Which results in:



                                                    [INFO] Detected version: '1.0.0-SNAPSHOT'
                                                    ##teamcity[setParameter name='env.PROJECT_VERSION' value='1.0.0-SNAPSHOT']
                                                    [INFO] ------------------------------------------------------------------------
                                                    [INFO] BUILD SUCCESS
                                                    [INFO] ------------------------------------------------------------------------


                                                    To save the output to a file (so that a CI server such as Jenkins could use it):



                                                    mvn com.smartcodeltd:release-candidate-maven-plugin:LATEST:version 
                                                    -DoutputTemplate="PROJECT_VERSION={{ version }}"
                                                    -DoutputUri="file://${project.basedir}/version.properties"


                                                    The resulting version.properties file will look as follows:



                                                    PROJECT_VERSION=1.0.0-SNAPSHOT




                                                    On top of all the above, Release Candidate also allows you to set the version of your project (which is something you'd probably do on your CI server) based on the API version you've defined in your POM.



                                                    If you'd like to see an example of Release Candidate being used as part of the Maven lifecycle, have a look at the pom.xml of my other open-source project - Build Monitor for Jenkins.







                                                    share|improve this answer














                                                    share|improve this answer



                                                    share|improve this answer








                                                    edited Sep 29 '17 at 18:45









                                                    approxiblue

                                                    5,686123849




                                                    5,686123849










                                                    answered Nov 22 '15 at 18:02









                                                    Jan Molak

                                                    2,86012328




                                                    2,86012328






















                                                        up vote
                                                        3
                                                        down vote













                                                        The easy to understand all-in-one solution that outputs the maven project version, and suppresses extraneous output from [INFO] and Download messages:



                                                        mvn -o org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | grep -v '['


                                                        Same thing, but split onto two lines:



                                                        mvn -o org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate 
                                                        -Dexpression=project.version | grep -v '['


                                                        Outputs: 4.3-SNAPSHOT



                                                        So, using your project.version in a simple bash script:



                                                        projectVersion=`mvn -o org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | grep -v '['`
                                                        cd "target/"$projectVersion"-build"


                                                        Other solutions on this page didn't seem to combine all the tricks into one.






                                                        share|improve this answer

























                                                          up vote
                                                          3
                                                          down vote













                                                          The easy to understand all-in-one solution that outputs the maven project version, and suppresses extraneous output from [INFO] and Download messages:



                                                          mvn -o org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | grep -v '['


                                                          Same thing, but split onto two lines:



                                                          mvn -o org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate 
                                                          -Dexpression=project.version | grep -v '['


                                                          Outputs: 4.3-SNAPSHOT



                                                          So, using your project.version in a simple bash script:



                                                          projectVersion=`mvn -o org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | grep -v '['`
                                                          cd "target/"$projectVersion"-build"


                                                          Other solutions on this page didn't seem to combine all the tricks into one.






                                                          share|improve this answer























                                                            up vote
                                                            3
                                                            down vote










                                                            up vote
                                                            3
                                                            down vote









                                                            The easy to understand all-in-one solution that outputs the maven project version, and suppresses extraneous output from [INFO] and Download messages:



                                                            mvn -o org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | grep -v '['


                                                            Same thing, but split onto two lines:



                                                            mvn -o org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate 
                                                            -Dexpression=project.version | grep -v '['


                                                            Outputs: 4.3-SNAPSHOT



                                                            So, using your project.version in a simple bash script:



                                                            projectVersion=`mvn -o org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | grep -v '['`
                                                            cd "target/"$projectVersion"-build"


                                                            Other solutions on this page didn't seem to combine all the tricks into one.






                                                            share|improve this answer












                                                            The easy to understand all-in-one solution that outputs the maven project version, and suppresses extraneous output from [INFO] and Download messages:



                                                            mvn -o org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | grep -v '['


                                                            Same thing, but split onto two lines:



                                                            mvn -o org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate 
                                                            -Dexpression=project.version | grep -v '['


                                                            Outputs: 4.3-SNAPSHOT



                                                            So, using your project.version in a simple bash script:



                                                            projectVersion=`mvn -o org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | grep -v '['`
                                                            cd "target/"$projectVersion"-build"


                                                            Other solutions on this page didn't seem to combine all the tricks into one.







                                                            share|improve this answer












                                                            share|improve this answer



                                                            share|improve this answer










                                                            answered Aug 30 '14 at 5:18









                                                            Peter Dietz

                                                            1,47411421




                                                            1,47411421






















                                                                up vote
                                                                2
                                                                down vote













                                                                Should be easier since this bug is fixed in maven-help-plugin 3.0.0: MPH-99 Evaluate has no output in quiet mode.






                                                                share|improve this answer























                                                                • Sadly 3.0.0 is not published
                                                                  – Víctor Romero
                                                                  Feb 15 at 0:03















                                                                up vote
                                                                2
                                                                down vote













                                                                Should be easier since this bug is fixed in maven-help-plugin 3.0.0: MPH-99 Evaluate has no output in quiet mode.






                                                                share|improve this answer























                                                                • Sadly 3.0.0 is not published
                                                                  – Víctor Romero
                                                                  Feb 15 at 0:03













                                                                up vote
                                                                2
                                                                down vote










                                                                up vote
                                                                2
                                                                down vote









                                                                Should be easier since this bug is fixed in maven-help-plugin 3.0.0: MPH-99 Evaluate has no output in quiet mode.






                                                                share|improve this answer














                                                                Should be easier since this bug is fixed in maven-help-plugin 3.0.0: MPH-99 Evaluate has no output in quiet mode.







                                                                share|improve this answer














                                                                share|improve this answer



                                                                share|improve this answer








                                                                edited Sep 29 '17 at 18:47









                                                                approxiblue

                                                                5,686123849




                                                                5,686123849










                                                                answered Jan 30 '15 at 10:35









                                                                mturra

                                                                15028




                                                                15028












                                                                • Sadly 3.0.0 is not published
                                                                  – Víctor Romero
                                                                  Feb 15 at 0:03


















                                                                • Sadly 3.0.0 is not published
                                                                  – Víctor Romero
                                                                  Feb 15 at 0:03
















                                                                Sadly 3.0.0 is not published
                                                                – Víctor Romero
                                                                Feb 15 at 0:03




                                                                Sadly 3.0.0 is not published
                                                                – Víctor Romero
                                                                Feb 15 at 0:03










                                                                up vote
                                                                1
                                                                down vote













                                                                Exec plugin works without any output parsing because output can be redirected into file and injected back into the job environment via EnvInject plugin:



                                                                enter image description here






                                                                share|improve this answer

























                                                                  up vote
                                                                  1
                                                                  down vote













                                                                  Exec plugin works without any output parsing because output can be redirected into file and injected back into the job environment via EnvInject plugin:



                                                                  enter image description here






                                                                  share|improve this answer























                                                                    up vote
                                                                    1
                                                                    down vote










                                                                    up vote
                                                                    1
                                                                    down vote









                                                                    Exec plugin works without any output parsing because output can be redirected into file and injected back into the job environment via EnvInject plugin:



                                                                    enter image description here






                                                                    share|improve this answer












                                                                    Exec plugin works without any output parsing because output can be redirected into file and injected back into the job environment via EnvInject plugin:



                                                                    enter image description here







                                                                    share|improve this answer












                                                                    share|improve this answer



                                                                    share|improve this answer










                                                                    answered Mar 9 '16 at 8:37









                                                                    Ondřej Kmoch

                                                                    663




                                                                    663






















                                                                        up vote
                                                                        1
                                                                        down vote













                                                                        I found right balance for me. After mvn package maven-archiver plugin creates target/maven-archiver/pom.properties with contents like this



                                                                        version=0.0.1-SNAPSHOT
                                                                        groupId=somegroup
                                                                        artifactId=someArtifact


                                                                        and I am using bash just to execute it



                                                                        . ./target/maven-archiver/pom.properties


                                                                        then



                                                                        echo $version
                                                                        0.0.1-SNAPSHOT


                                                                        Of course this is not safe at all to execute this file, but execution can easily be converted into perl or bash script to read and set environment variable from that file.






                                                                        share|improve this answer

























                                                                          up vote
                                                                          1
                                                                          down vote













                                                                          I found right balance for me. After mvn package maven-archiver plugin creates target/maven-archiver/pom.properties with contents like this



                                                                          version=0.0.1-SNAPSHOT
                                                                          groupId=somegroup
                                                                          artifactId=someArtifact


                                                                          and I am using bash just to execute it



                                                                          . ./target/maven-archiver/pom.properties


                                                                          then



                                                                          echo $version
                                                                          0.0.1-SNAPSHOT


                                                                          Of course this is not safe at all to execute this file, but execution can easily be converted into perl or bash script to read and set environment variable from that file.






                                                                          share|improve this answer























                                                                            up vote
                                                                            1
                                                                            down vote










                                                                            up vote
                                                                            1
                                                                            down vote









                                                                            I found right balance for me. After mvn package maven-archiver plugin creates target/maven-archiver/pom.properties with contents like this



                                                                            version=0.0.1-SNAPSHOT
                                                                            groupId=somegroup
                                                                            artifactId=someArtifact


                                                                            and I am using bash just to execute it



                                                                            . ./target/maven-archiver/pom.properties


                                                                            then



                                                                            echo $version
                                                                            0.0.1-SNAPSHOT


                                                                            Of course this is not safe at all to execute this file, but execution can easily be converted into perl or bash script to read and set environment variable from that file.






                                                                            share|improve this answer












                                                                            I found right balance for me. After mvn package maven-archiver plugin creates target/maven-archiver/pom.properties with contents like this



                                                                            version=0.0.1-SNAPSHOT
                                                                            groupId=somegroup
                                                                            artifactId=someArtifact


                                                                            and I am using bash just to execute it



                                                                            . ./target/maven-archiver/pom.properties


                                                                            then



                                                                            echo $version
                                                                            0.0.1-SNAPSHOT


                                                                            Of course this is not safe at all to execute this file, but execution can easily be converted into perl or bash script to read and set environment variable from that file.







                                                                            share|improve this answer












                                                                            share|improve this answer



                                                                            share|improve this answer










                                                                            answered Nov 11 at 12:22









                                                                            Vladimir I

                                                                            112




                                                                            112






















                                                                                up vote
                                                                                0
                                                                                down vote













                                                                                This worked for me, offline and without depending on mvn:



                                                                                VERSION=$(grep --max-count=1 '<version>' <your_path>/pom.xml | awk -F '>' '{ print $2 }' | awk -F '<' '{ print $1 }')
                                                                                echo $VERSION





                                                                                share|improve this answer

























                                                                                  up vote
                                                                                  0
                                                                                  down vote













                                                                                  This worked for me, offline and without depending on mvn:



                                                                                  VERSION=$(grep --max-count=1 '<version>' <your_path>/pom.xml | awk -F '>' '{ print $2 }' | awk -F '<' '{ print $1 }')
                                                                                  echo $VERSION





                                                                                  share|improve this answer























                                                                                    up vote
                                                                                    0
                                                                                    down vote










                                                                                    up vote
                                                                                    0
                                                                                    down vote









                                                                                    This worked for me, offline and without depending on mvn:



                                                                                    VERSION=$(grep --max-count=1 '<version>' <your_path>/pom.xml | awk -F '>' '{ print $2 }' | awk -F '<' '{ print $1 }')
                                                                                    echo $VERSION





                                                                                    share|improve this answer












                                                                                    This worked for me, offline and without depending on mvn:



                                                                                    VERSION=$(grep --max-count=1 '<version>' <your_path>/pom.xml | awk -F '>' '{ print $2 }' | awk -F '<' '{ print $1 }')
                                                                                    echo $VERSION






                                                                                    share|improve this answer












                                                                                    share|improve this answer



                                                                                    share|improve this answer










                                                                                    answered Nov 24 '15 at 9:19









                                                                                    George

                                                                                    4,43553757




                                                                                    4,43553757






















                                                                                        up vote
                                                                                        0
                                                                                        down vote













                                                                                        Either you have mvn give you the answer (as most answers suggest), or you extract the answer from the pom.xml. The only drawback of the second approach is that you can very easily extract the value of the <version/> tag, but it will be meaningful only if it's literal, that is, not a Maven property. I chose this approach anyway because:





                                                                                        • mvn is way to verbose and I simply don't like filtering its output.

                                                                                        • Starting mvn is very slow compared to reading the pom.xml.

                                                                                        • I always use literal values in <version/>.


                                                                                        mvn-version is a zsh shell script that uses xmlstarlet to read the pom.xml and print the version of the project (if it exists) or the version of the parent project (if it exists):



                                                                                        $ mvn-version .
                                                                                        1.0.0-SNAPSHOT


                                                                                        The advantage is that it's way quicker than running mvn:



                                                                                        $ time mvn-version .
                                                                                        1.1.0-SNAPSHOT
                                                                                        mvn-version . 0.01s user 0.01s system 75% cpu 0.019 total

                                                                                        $ time mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate
                                                                                        > -Dexpression=project.version
                                                                                        mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate 4.17s user 0.21s system 240% cpu 1.823 total


                                                                                        The difference on my machine is greater than two orders of magnitude.






                                                                                        share|improve this answer

























                                                                                          up vote
                                                                                          0
                                                                                          down vote













                                                                                          Either you have mvn give you the answer (as most answers suggest), or you extract the answer from the pom.xml. The only drawback of the second approach is that you can very easily extract the value of the <version/> tag, but it will be meaningful only if it's literal, that is, not a Maven property. I chose this approach anyway because:





                                                                                          • mvn is way to verbose and I simply don't like filtering its output.

                                                                                          • Starting mvn is very slow compared to reading the pom.xml.

                                                                                          • I always use literal values in <version/>.


                                                                                          mvn-version is a zsh shell script that uses xmlstarlet to read the pom.xml and print the version of the project (if it exists) or the version of the parent project (if it exists):



                                                                                          $ mvn-version .
                                                                                          1.0.0-SNAPSHOT


                                                                                          The advantage is that it's way quicker than running mvn:



                                                                                          $ time mvn-version .
                                                                                          1.1.0-SNAPSHOT
                                                                                          mvn-version . 0.01s user 0.01s system 75% cpu 0.019 total

                                                                                          $ time mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate
                                                                                          > -Dexpression=project.version
                                                                                          mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate 4.17s user 0.21s system 240% cpu 1.823 total


                                                                                          The difference on my machine is greater than two orders of magnitude.






                                                                                          share|improve this answer























                                                                                            up vote
                                                                                            0
                                                                                            down vote










                                                                                            up vote
                                                                                            0
                                                                                            down vote









                                                                                            Either you have mvn give you the answer (as most answers suggest), or you extract the answer from the pom.xml. The only drawback of the second approach is that you can very easily extract the value of the <version/> tag, but it will be meaningful only if it's literal, that is, not a Maven property. I chose this approach anyway because:





                                                                                            • mvn is way to verbose and I simply don't like filtering its output.

                                                                                            • Starting mvn is very slow compared to reading the pom.xml.

                                                                                            • I always use literal values in <version/>.


                                                                                            mvn-version is a zsh shell script that uses xmlstarlet to read the pom.xml and print the version of the project (if it exists) or the version of the parent project (if it exists):



                                                                                            $ mvn-version .
                                                                                            1.0.0-SNAPSHOT


                                                                                            The advantage is that it's way quicker than running mvn:



                                                                                            $ time mvn-version .
                                                                                            1.1.0-SNAPSHOT
                                                                                            mvn-version . 0.01s user 0.01s system 75% cpu 0.019 total

                                                                                            $ time mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate
                                                                                            > -Dexpression=project.version
                                                                                            mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate 4.17s user 0.21s system 240% cpu 1.823 total


                                                                                            The difference on my machine is greater than two orders of magnitude.






                                                                                            share|improve this answer












                                                                                            Either you have mvn give you the answer (as most answers suggest), or you extract the answer from the pom.xml. The only drawback of the second approach is that you can very easily extract the value of the <version/> tag, but it will be meaningful only if it's literal, that is, not a Maven property. I chose this approach anyway because:





                                                                                            • mvn is way to verbose and I simply don't like filtering its output.

                                                                                            • Starting mvn is very slow compared to reading the pom.xml.

                                                                                            • I always use literal values in <version/>.


                                                                                            mvn-version is a zsh shell script that uses xmlstarlet to read the pom.xml and print the version of the project (if it exists) or the version of the parent project (if it exists):



                                                                                            $ mvn-version .
                                                                                            1.0.0-SNAPSHOT


                                                                                            The advantage is that it's way quicker than running mvn:



                                                                                            $ time mvn-version .
                                                                                            1.1.0-SNAPSHOT
                                                                                            mvn-version . 0.01s user 0.01s system 75% cpu 0.019 total

                                                                                            $ time mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate
                                                                                            > -Dexpression=project.version
                                                                                            mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate 4.17s user 0.21s system 240% cpu 1.823 total


                                                                                            The difference on my machine is greater than two orders of magnitude.







                                                                                            share|improve this answer












                                                                                            share|improve this answer



                                                                                            share|improve this answer










                                                                                            answered Nov 28 '16 at 23:15









                                                                                            Enrico M. Crisostomo

                                                                                            1,02111024




                                                                                            1,02111024






















                                                                                                up vote
                                                                                                -2
                                                                                                down vote













                                                                                                mvn help:evaluate -Dexpression=project.version | sed -e 1h -e '2,3{H;g}' -e '/[INFO] BUILD SUCCESS/ q' -e '1,2d' -e '{N;D}' | sed -e '1q'


                                                                                                I'm just adding small sed filter improvement I have recently implemented to extract project.version from maven output.






                                                                                                share|improve this answer



















                                                                                                • 2




                                                                                                  Does not work here.
                                                                                                  – ceving
                                                                                                  Feb 28 '13 at 10:30










                                                                                                • Does not work for me either
                                                                                                  – Joseph Earl
                                                                                                  May 30 '13 at 15:20










                                                                                                • For me it works, i have just corrected the regex.
                                                                                                  – Gábor Lipták
                                                                                                  Jun 19 '13 at 13:21















                                                                                                up vote
                                                                                                -2
                                                                                                down vote













                                                                                                mvn help:evaluate -Dexpression=project.version | sed -e 1h -e '2,3{H;g}' -e '/[INFO] BUILD SUCCESS/ q' -e '1,2d' -e '{N;D}' | sed -e '1q'


                                                                                                I'm just adding small sed filter improvement I have recently implemented to extract project.version from maven output.






                                                                                                share|improve this answer



















                                                                                                • 2




                                                                                                  Does not work here.
                                                                                                  – ceving
                                                                                                  Feb 28 '13 at 10:30










                                                                                                • Does not work for me either
                                                                                                  – Joseph Earl
                                                                                                  May 30 '13 at 15:20










                                                                                                • For me it works, i have just corrected the regex.
                                                                                                  – Gábor Lipták
                                                                                                  Jun 19 '13 at 13:21













                                                                                                up vote
                                                                                                -2
                                                                                                down vote










                                                                                                up vote
                                                                                                -2
                                                                                                down vote









                                                                                                mvn help:evaluate -Dexpression=project.version | sed -e 1h -e '2,3{H;g}' -e '/[INFO] BUILD SUCCESS/ q' -e '1,2d' -e '{N;D}' | sed -e '1q'


                                                                                                I'm just adding small sed filter improvement I have recently implemented to extract project.version from maven output.






                                                                                                share|improve this answer














                                                                                                mvn help:evaluate -Dexpression=project.version | sed -e 1h -e '2,3{H;g}' -e '/[INFO] BUILD SUCCESS/ q' -e '1,2d' -e '{N;D}' | sed -e '1q'


                                                                                                I'm just adding small sed filter improvement I have recently implemented to extract project.version from maven output.







                                                                                                share|improve this answer














                                                                                                share|improve this answer



                                                                                                share|improve this answer








                                                                                                edited Jun 19 '13 at 13:20









                                                                                                Gábor Lipták

                                                                                                7,03624394




                                                                                                7,03624394










                                                                                                answered Jul 30 '12 at 17:47









                                                                                                websigni

                                                                                                25




                                                                                                25








                                                                                                • 2




                                                                                                  Does not work here.
                                                                                                  – ceving
                                                                                                  Feb 28 '13 at 10:30










                                                                                                • Does not work for me either
                                                                                                  – Joseph Earl
                                                                                                  May 30 '13 at 15:20










                                                                                                • For me it works, i have just corrected the regex.
                                                                                                  – Gábor Lipták
                                                                                                  Jun 19 '13 at 13:21














                                                                                                • 2




                                                                                                  Does not work here.
                                                                                                  – ceving
                                                                                                  Feb 28 '13 at 10:30










                                                                                                • Does not work for me either
                                                                                                  – Joseph Earl
                                                                                                  May 30 '13 at 15:20










                                                                                                • For me it works, i have just corrected the regex.
                                                                                                  – Gábor Lipták
                                                                                                  Jun 19 '13 at 13:21








                                                                                                2




                                                                                                2




                                                                                                Does not work here.
                                                                                                – ceving
                                                                                                Feb 28 '13 at 10:30




                                                                                                Does not work here.
                                                                                                – ceving
                                                                                                Feb 28 '13 at 10:30












                                                                                                Does not work for me either
                                                                                                – Joseph Earl
                                                                                                May 30 '13 at 15:20




                                                                                                Does not work for me either
                                                                                                – Joseph Earl
                                                                                                May 30 '13 at 15:20












                                                                                                For me it works, i have just corrected the regex.
                                                                                                – Gábor Lipták
                                                                                                Jun 19 '13 at 13:21




                                                                                                For me it works, i have just corrected the regex.
                                                                                                – Gábor Lipták
                                                                                                Jun 19 '13 at 13:21










                                                                                                up vote
                                                                                                -2
                                                                                                down vote













                                                                                                VERSION=$(head -50 pom.xml | awk -F'>' '/SNAPSHOT/ {print $2}' | awk -F'<' '{print $1}')


                                                                                                This is what I used to get the version number, thought there would have been a better maven way to do so






                                                                                                share|improve this answer

























                                                                                                  up vote
                                                                                                  -2
                                                                                                  down vote













                                                                                                  VERSION=$(head -50 pom.xml | awk -F'>' '/SNAPSHOT/ {print $2}' | awk -F'<' '{print $1}')


                                                                                                  This is what I used to get the version number, thought there would have been a better maven way to do so






                                                                                                  share|improve this answer























                                                                                                    up vote
                                                                                                    -2
                                                                                                    down vote










                                                                                                    up vote
                                                                                                    -2
                                                                                                    down vote









                                                                                                    VERSION=$(head -50 pom.xml | awk -F'>' '/SNAPSHOT/ {print $2}' | awk -F'<' '{print $1}')


                                                                                                    This is what I used to get the version number, thought there would have been a better maven way to do so






                                                                                                    share|improve this answer












                                                                                                    VERSION=$(head -50 pom.xml | awk -F'>' '/SNAPSHOT/ {print $2}' | awk -F'<' '{print $1}')


                                                                                                    This is what I used to get the version number, thought there would have been a better maven way to do so







                                                                                                    share|improve this answer












                                                                                                    share|improve this answer



                                                                                                    share|improve this answer










                                                                                                    answered Jul 7 '15 at 21:43









                                                                                                    Tristik

                                                                                                    173




                                                                                                    173






















                                                                                                        up vote
                                                                                                        -4
                                                                                                        down vote













                                                                                                        Maven footer is pretty standard:



                                                                                                        [INFO] ------------------------------------------------------------------------
                                                                                                        [INFO] BUILD SUCCESS
                                                                                                        [INFO] ------------------------------------------------------------------------
                                                                                                        [INFO] Total time: 1.609s
                                                                                                        [INFO] Finished at: Wed May 21 18:02:38 MSK 2014
                                                                                                        [INFO] Final Memory: 17M/736M
                                                                                                        [INFO] ------------------------------------------------------------------------


                                                                                                        So you can use the following code:



                                                                                                        > version=$(mvn help:evaluate -Dexpression=project.version | tail -8 | head -1)
                                                                                                        > echo $version





                                                                                                        share|improve this answer

























                                                                                                          up vote
                                                                                                          -4
                                                                                                          down vote













                                                                                                          Maven footer is pretty standard:



                                                                                                          [INFO] ------------------------------------------------------------------------
                                                                                                          [INFO] BUILD SUCCESS
                                                                                                          [INFO] ------------------------------------------------------------------------
                                                                                                          [INFO] Total time: 1.609s
                                                                                                          [INFO] Finished at: Wed May 21 18:02:38 MSK 2014
                                                                                                          [INFO] Final Memory: 17M/736M
                                                                                                          [INFO] ------------------------------------------------------------------------


                                                                                                          So you can use the following code:



                                                                                                          > version=$(mvn help:evaluate -Dexpression=project.version | tail -8 | head -1)
                                                                                                          > echo $version





                                                                                                          share|improve this answer























                                                                                                            up vote
                                                                                                            -4
                                                                                                            down vote










                                                                                                            up vote
                                                                                                            -4
                                                                                                            down vote









                                                                                                            Maven footer is pretty standard:



                                                                                                            [INFO] ------------------------------------------------------------------------
                                                                                                            [INFO] BUILD SUCCESS
                                                                                                            [INFO] ------------------------------------------------------------------------
                                                                                                            [INFO] Total time: 1.609s
                                                                                                            [INFO] Finished at: Wed May 21 18:02:38 MSK 2014
                                                                                                            [INFO] Final Memory: 17M/736M
                                                                                                            [INFO] ------------------------------------------------------------------------


                                                                                                            So you can use the following code:



                                                                                                            > version=$(mvn help:evaluate -Dexpression=project.version | tail -8 | head -1)
                                                                                                            > echo $version





                                                                                                            share|improve this answer












                                                                                                            Maven footer is pretty standard:



                                                                                                            [INFO] ------------------------------------------------------------------------
                                                                                                            [INFO] BUILD SUCCESS
                                                                                                            [INFO] ------------------------------------------------------------------------
                                                                                                            [INFO] Total time: 1.609s
                                                                                                            [INFO] Finished at: Wed May 21 18:02:38 MSK 2014
                                                                                                            [INFO] Final Memory: 17M/736M
                                                                                                            [INFO] ------------------------------------------------------------------------


                                                                                                            So you can use the following code:



                                                                                                            > version=$(mvn help:evaluate -Dexpression=project.version | tail -8 | head -1)
                                                                                                            > echo $version






                                                                                                            share|improve this answer












                                                                                                            share|improve this answer



                                                                                                            share|improve this answer










                                                                                                            answered May 21 '14 at 17:34









                                                                                                            ursa

                                                                                                            3,0081927




                                                                                                            3,0081927






























                                                                                                                draft saved

                                                                                                                draft discarded




















































                                                                                                                Thanks for contributing an answer to Stack Overflow!


                                                                                                                • Please be sure to answer the question. Provide details and share your research!

                                                                                                                But avoid



                                                                                                                • Asking for help, clarification, or responding to other answers.

                                                                                                                • Making statements based on opinion; back them up with references or personal experience.


                                                                                                                To learn more, see our tips on writing great answers.





                                                                                                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                                                                                Please pay close attention to the following guidance:


                                                                                                                • Please be sure to answer the question. Provide details and share your research!

                                                                                                                But avoid



                                                                                                                • Asking for help, clarification, or responding to other answers.

                                                                                                                • Making statements based on opinion; back them up with references or personal experience.


                                                                                                                To learn more, see our tips on writing great answers.




                                                                                                                draft saved


                                                                                                                draft discarded














                                                                                                                StackExchange.ready(
                                                                                                                function () {
                                                                                                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3545292%2fhow-to-get-maven-project-version-to-the-bash-command-line%23new-answer', 'question_page');
                                                                                                                }
                                                                                                                );

                                                                                                                Post as a guest















                                                                                                                Required, but never shown





















































                                                                                                                Required, but never shown














                                                                                                                Required, but never shown












                                                                                                                Required, but never shown







                                                                                                                Required, but never shown

































                                                                                                                Required, but never shown














                                                                                                                Required, but never shown












                                                                                                                Required, but never shown







                                                                                                                Required, but never shown