Add Maven Build Information to Properties File

This example shows you how to add your Maven Build Information like version number, build time, etc.. to a properties file. We use the resource filtering from the maven-resources-plugin. We get the information like version number from the maven pom.xml file. We assign the maven.build.timestamp into a new property because there is a bug in maven which prevents from getting this property directly. Finally we use a custom date formatter to print out the required date format.

Add Maven Build Information To Properties File

We copy all the build information defined in our pom.xml file using the maven-resouces-plugin.

<?xml version="1.0" encoding="UTF-8"?>
<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">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.memorynotfound.maven</groupId>
    <artifactId>version-number-build-time-maven</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>MAVEN - ${project.artifactId}</name>
    <url>http://memorynotfound.com</url>
    <packaging>war</packaging>

    <properties>
        <timestamp>${maven.build.timestamp}</timestamp>
        <maven.build.timestamp.format>dd-MM-yyyy HH:mm

</maven.build.timestamp.format>
    </properties>

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

</project>

Properties File

Add the build.properties file to your classpath.

build.version=${pom.version}
build.date=${timestamp}
build.url=${pom.url}
build.name=${pom.name}

Build with maven

mvn clean package

Filtered Properties File

The generated/filtered properties file by Maven.

build.version=1.0.0-SNAPSHOT
build.date=2015-07-03 14:11
build.url=http://memorynotfound.com
build.name=MAVEN - version-number-build-time-maven

References

Download

You may also like...