Git Product home page Git Product logo

buildinfo-maven-example's Introduction

buildinfo-maven-example

This document and example code repository describes how to render a properties file containing build timestamp and more information using maven.

Howto

Template file

The buildinfo.properties file template is placed in the resources directory and can contain any Maven property.

Example src/main/resources/buildinfo.properties:

version=${project.version}
date=${maven.build.timestamp}

Filtering

Use maven-resources-plugin to filter the file and replace it with interpolated variables.

Example pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>${maven-resources-plugin.version}</version>
        </plugin>

        ...

    <plugins>

    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/buildinfo.properties</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>false</filtering>
            <excludes>
                <exclude>**/buildinfo.properties</exclude>
            </excludes>
        </resource>
    </resources>
<build>

The filtered file will be included in the compile result, instead of the template:

target/classes/buildinfo.properties:

version=0.0.1-SNAPSHOT
date=2020-09-04T08:11:02Z

NOTE: if maven.build.timestamp is not replaced with a timestamp, your maven-resources-plugin version might be outdated.

From there (either at runtime if packaged into a jar, or at test stage) it can be read like any resource. See example code below or in this repository's Main.java.

Packaging (optional)

The above will provide build information inside the packaged jar, primarily to be read by the application at runtime.

To have build information readily available in your distribution tarball (for example to inspect it via cat buildinfo.properties on the server), include it via src/assembly/distribution.xml:

<?xml version="1.0" encoding="UTF-8"?>
<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>distribution</id>
    <formats>
        <format>tar.gz</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <files>
        <file>
            <source>${project.build.directory}/classes/buildinfo.properties</source>
            <outputDirectory>.</outputDirectory>
        </file>
    </files>

    ...

</assembly>

This definition will store the buildinfo.properties file at toplevel in the distributable tarball.

Results

After these steps, the buildinfo.properties file can be found at the following locations:

  • target/classes/buildinfo.properties
  • buildinfo-maven-example-0.0.1-SNAPSHOT.jar/buildinfo.properties
  • buildinfo-maven-example-0.0.1-SNAPSHOT-distribution.tar.gz/buildinfo.properties

Accessing the properties at runtime

The file can be loaded like any resource, see example code in Main.java:

    Properties buildInfo = new Properties();

    try (InputStream inputStream = Main.class.getResourceAsStream("/buildinfo.properties")) {
        buildInfo.load(inputStream);
    }

    System.out.println("build date: " + buildInfo.getProperty("date"));
    System.out.println("build version: " + buildInfo.getProperty("version"));

Relevant dependencies:

Example code

This repository contains a fully functional example demonstrating rendering of a buildinfo file via maven.

Build instructions

mvn clean package

=> will render the buildinfo.properties file

Run instructions

java -jar target/buildinfo-maven-example-0.0.1-SNAPSHOT.jar

=> will output properties from buildinfo.properties

Further reading

Any maven property can be used for interpolation. For an overview, start here:

http://maven.apache.org/ref/3.6.3/maven-model-builder/#Model_Interpolation

There are plugins available that will expose the current git commit hash as a maven property.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.