title: How to get Maven project version to the bash command line
body: <p>Previous I issued a question on <a href="https://stackoverflow.com/questions/3519005/update-a-maven-project-version-from-script">how to change Maven project vesion from command line</a> which lead me to a new issue.</p>
  <p>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 <code>&lt;version&gt;</code> 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 <strong>without</strong> external tools for parsing XML or some very context-aware <code>sed</code> command.</p>
  <p>The most clean solution in my opinion 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.</p>
  <p>So, is there any easy way to get the value of <code>${project.version}</code> to the command line?</p>
  <h3>Solution</h3>
  <p>I had to <code>cd</code> to the directory manually but that can be done easily. In my bash script I have:</p>
  
  <pre class="lang-sh prettyprint-override"><code>version=`cd $project_loc &amp;&amp; mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | sed -n -e '/^\[.*\]/ !{ /^[0-9]/ { p; q } }'`
  </code></pre>
  <p>Which gives me the current version that I can then advance. Grepping might be simpler 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.</p>
  <pre class="lang-sh prettyprint-override"><code># 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 &quot;s/[0-9][0-9]*\([^0-9]*\)$/$next_num/&quot;
  }
  </code></pre>
  <p>And I use this by simply calling:</p>
  <pre class="lang-sh prettyprint-override"><code>new_version=$(advance_version $version)
  </code></pre>