title: How to set project version in maven at build time?
body: <p>I am trying to implement semantic versioning in our project. I tested maven semver plugin but that didn't help me so please don't ask me why. I finally ended up using maven groovy. It works like a charm, however, when I install or deploy the maven project the version in repository is the variable name. </p>
  
  <p>This is despite the fact that all the artefacts and jar files are packaged with correct version.</p>
  
  <p>So please look at my pom.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                        http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
  
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;com.mytest.test&lt;/groupId&gt;
      &lt;artifactId&gt;test-tag&lt;/artifactId&gt;
      &lt;version&gt;${revision}&lt;/version&gt; 
  
  
      &lt;description&gt;Test&lt;/description&gt;
  
      &lt;properties&gt;
          &lt;ChangeType&gt;TO_BE_SET&lt;/ChangeType&gt;
      &lt;/properties&gt;
  
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.codehaus.gmaven&lt;/groupId&gt;
                  &lt;artifactId&gt;gmaven-plugin&lt;/artifactId&gt;
                  &lt;version&gt;1.5&lt;/version&gt;
                  &lt;executions&gt;
                      &lt;execution&gt;
                          &lt;phase&gt;validate&lt;/phase&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;execute&lt;/goal&gt;
                          &lt;/goals&gt;
                          &lt;configuration&gt;
                              &lt;providerSelection&gt;2.0&lt;/providerSelection&gt;
                              &lt;properties&gt;
                                  &lt;script&gt;git describe --abbrev=0 --tags&lt;/script&gt;
                              &lt;/properties&gt;
                              &lt;source&gt;
                                  def tagIt = 'git tag -a vXXXX -m "Auto tagged"'
                                  def changeType = project.properties.ChangeType
                                  def command = project.properties.script
                                  def process = command.execute()
                                  process.waitFor()
                                  def describe = process.in.text.trim()
                                  println "Setting revision to: " + describe
  
                                  if(!describe.startsWith("v")) {
                                      describe = "1.0.1"
                                  } else {
                                      describe = describe.substring(1)
                                  }
  
                                      project.properties.setProperty('revision', describe)
  
  
                              &lt;/source&gt;
                          &lt;/configuration&gt;
                      &lt;/execution&gt;
                  &lt;/executions&gt;
              &lt;/plugin&gt;
  
  
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                  &lt;executions&gt;
                      &lt;execution&gt;
                          &lt;id&gt;default-compile&lt;/id&gt;
                          &lt;phase&gt;none&lt;/phase&gt;
                      &lt;/execution&gt;
                      &lt;execution&gt;
                          &lt;id&gt;default-testCompile&lt;/id&gt;
                          &lt;phase&gt;none&lt;/phase&gt;
                      &lt;/execution&gt;
                  &lt;/executions&gt;
              &lt;/plugin&gt;
  
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-resources-plugin&lt;/artifactId&gt;
                  &lt;executions&gt;
                      &lt;execution&gt;
                          &lt;id&gt;default-testResources&lt;/id&gt;
                          &lt;phase&gt;none&lt;/phase&gt;
                      &lt;/execution&gt;
                  &lt;/executions&gt;
              &lt;/plugin&gt;
  
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
                  &lt;executions&gt;
                      &lt;execution&gt;
                          &lt;id&gt;default-jar&lt;/id&gt;
                          &lt;phase&gt;package&lt;/phase&gt;
                      &lt;/execution&gt;
                  &lt;/executions&gt;
              &lt;/plugin&gt;
  
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
                  &lt;executions&gt;
                      &lt;execution&gt;
                          &lt;id&gt;default-test&lt;/id&gt;
                          &lt;phase&gt;none&lt;/phase&gt;
                      &lt;/execution&gt;
                  &lt;/executions&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  
  &lt;/project&gt;
  </code></pre>
  
  <p>the version is ${revision} a variable name that is being set in groovy script. What groovy code does is getting the last tag from GIT and and then set it to the property 'revision'.</p>
  
  <p>The final jar file has the correct version extracted but when installed into repository, the folder name and jar name are like:</p>
  
  <blockquote>
    <p>m2\repository\com\mytest\test\test-tag\${revision}\test-tag-${revision}.jar</p>
  </blockquote>
  
  <p>I tried to default 'revision' to a value using:</p>
  
  <pre><code>&lt;properties&gt;
    &lt;revision&gt;1.0.1&lt;/revision&gt;
  &lt;/properties&gt;
  </code></pre>
  
  <p>but then groovy code setting the value has no effect.</p>
  
  <p>I also tried different phase for the maven groovy plugin, no luck. Have I missed anything? Can anyone please help me on this?</p>
  
  <p>I'd like to mention that as vatbub and StefanHeimberg mentioned I can use versions:set to set the version but this requires me to do an extra commit to GIT which I am trying to avoid, wondering if I can achieve this by writing a maven plugin instead? </p>