Displaying the Build Date

Displaying the build date

Jeff Atwood had a few things to say about this issue in Determining Build Date the hard way.

The most reliable method turns out to be retrieving the linker timestamp from the PE header embedded in the executable file -- some C# code (by Joe Spivey) for that from the comments to Jeff's article:

public static DateTime GetLinkerTime(this Assembly assembly, TimeZoneInfo target = null)
{
var filePath = assembly.Location;
const int c_PeHeaderOffset = 60;
const int c_LinkerTimestampOffset = 8;

var buffer = new byte[2048];

using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
stream.Read(buffer, 0, 2048);

var offset = BitConverter.ToInt32(buffer, c_PeHeaderOffset);
var secondsSince1970 = BitConverter.ToInt32(buffer, offset + c_LinkerTimestampOffset);
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

var linkTimeUtc = epoch.AddSeconds(secondsSince1970);

var tz = target ?? TimeZoneInfo.Local;
var localTime = TimeZoneInfo.ConvertTimeFromUtc(linkTimeUtc, tz);

return localTime;
}

Usage example:

var linkTimeLocal = Assembly.GetExecutingAssembly().GetLinkerTime();

Note: this method works for .NET Core 1.0, but stopped working after .NET Core 1.1 - it gives random years in the 1900-2020 range.

Display build date in razor file (Blazor)

For debugging purposes, you could use the AssemblyTitle (or any other attribute you like)

First you add it to your csproj file

<PropertyGroup>
<AssemblyTitle Condition="'$(Configuration)' == 'debug'">My Assembly $([System.DateTime]::Now)</AssemblyTitle>
</PropertyGroup>

Then in your Blazor code (MainLayout seems a good choice) you can extract the value and display it:

<div class="info-panel">@BuildInfo</div>
@code {
string BuildInfo;
#if DEBUG
protected override void OnInitialized()
{
Assembly curAssembly = typeof(Program).Assembly;
BuildInfo = $"{curAssembly.GetCustomAttributes(false).OfType<AssemblyTitleAttribute>().FirstOrDefault().Title}";
}
#endif
}

If you don't like the idea of using an existing attribute, you could create a custom attribute - but that seems over the top to me.

Displaying version and date of build in the xhtml page

One approach that will work: use Maven filtering to put a file in your WAR or JAR containing the required information. Then in your Java webapp, load that file's contents as a ClassPath resource InputStream.

Create a file (let's say "buildInfo.properties") under src/main/resources containing something like:

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

Note that due to an open defect, you need to define the timestamp property as follows in the <properties> block of your pom:

`<timestamp>${maven.build.timestamp}</timestamp>`

During your build, this file will be filtered with the value of project.version (which you define with <version> in your pom.xml, when you specify

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

In your Java code (JSF bean, whatever), have code like the following:

    InputStream in = getClass().getClassLoader().getResourceAsStream("buildInfo.properties");
if (in == null)
return;

Properties props = new Properties();
props.load(in);

String version = props.getProperty("build.version");
// etc.

If your framework supports loading properties as "Resource Bundles" from the classpath (i.e. like in Spring), no need for the preceding Java code that loads the properties file.

How to show build datetime on my react web app using create-react-app?

I'm a Create React App maintainer!

Starting in Create React App 2 (react-scripts@^2.0) you can accomplish this via macros.

First, install preval.macro:

$ npm install --save preval.macro # if using npm
$ yarn add preval.macro # if using yarn

Next, in the file you want to render a build timestamp in, include preval.macro:

import preval from 'preval.macro'

Finally, you can create a constant and use it in your app like so:

const dateTimeStamp = preval`module.exports = new Date().toLocaleString();`

Here's a full example:

import React, { Component } from 'react'
import logo from './logo.svg'
import './App.css'
import preval from 'preval.macro'

class App extends Component {
render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Build Date: {preval`module.exports = new Date().toLocaleString();`}.
</p>
</header>
</div>
)
}
}

export default App

Display build timestamp in Spring Boot actuator info endpoint

You can define a timestamp Maven property in your pom.xml like so:

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

And then reference it using the @...@ convention like so:

info:
app:
timestamp: @timestamp@

How can I display the build time in my Android application?

Assuming that you're building with Gradle, you can add a BuildConfig field with the desired information in your app/build.gradle:

android {
defaultConfig {
def buildTime = new Date()
buildConfigField "String", "BUILD_TIME", "\"${buildTime.format('yyyy-MM-dd HH:mm:ss')}\""
... other stuff ...
}
...other stuff ...
}

And then in your Kotlin/Java code:

myTextView.text = BuildConfig.BUILD_TIME

Another alternative is to replace the buildConfigField line in the above example with:

resValue "string", "build_time", "${buildTime.format('yyyy-MM-dd HH:mm:ss')}"

Which creates a string resource that you can use in your layout XML file, e.g.:

android:text="@string/build_time"


Related Topics



Leave a reply



Submit