logo

How to Export POM Project Version to Property File

Last Updated: 2021-11-19

Somehow I need to have a version defined in a property file, say a file at src/main/resources/config.prop

version=1.0.0

This should be the same version number defined in pom. However this file needs to be manually modified for every new release. Here's a solution to easily sync up the versions:

Step 1: add pom property to properties file

version=${project.version}

Step 2: add maven-resources-plugin to pom file

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.7</version>
    </plugin>
</plugins>

Step 3: setup section in pom

Set directory to src/main/resources. Set filtering to true so ${project.version} will be replaced by the actual project.version defined in pom

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

Step 4: compile

Compile the code

$ mvn install

find the "compiled" property file in target/classes/conf.prop, the ${project.version} should be replaced

version=1.0.0

Step 5: package the compiled file by assembly plugin

<fileSets>
    <fileSet>
        <directory>${project.basedir}/target/classes</directory>
        <outputDirectory>/</outputDirectory>
        <includes>
            <include>conf.prop</include>
        </includes>
    </fileSet>
</fileSets>