maven - read version number from property file

Read following answers:

  • User and project specific settings in Maven
  • How to read an external properties file in Maven
  • Reading properties file from Maven POM file
  • Read a file into a Maven property

or just:

<project>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>properties-maven-plugin</artifactId>
        <version>1.0</version>
        <executions>
          <execution>
            <phase>initialize</phase>
            <goals>
              <goal>read-project-properties</goal>
            </goals>
            <configuration>
              <files>
                <file>dev.properties</file> <!-- THIS!!! -->
              </files>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

UPDATE Working proof of concept is a subproject at http://sourceforge.net/u/gavenkoa/exp/ci/default/tree/maven/properties/pom.xml

Run build as mvn compile, check pom.xml and console output.


Just to answer my own question: it turns out that Maven needs the property to be set before anything can happen in the script. As such, it is set in stone and not modifiable from a file. I ended up writing an Ant task that modifies the pom.xml and changes the version in the file, before Maven script is triggered. Ugly and non-trivial, but it works.