Search code examples
javaandroideclipsegitauto-generate

Embed git-describe, date and time in a build during compilation in eclipse


I am using an external builder feature in eclipse to call a bash script that will autogenerate BuildInfo.java class for my project (I've looked here on stackoverflow for a way to embed git-describe and date into an eclipse (non-ant!) build, but couldn't find any), my the script looks like this:

#!/bin/bash

VERSION=$(git describe --tag)
DATE=$(date "+%Y-%m-%d")
TIME=$(date "+%H:%M")

echo "Version $VERSION, built on $DATE $TIME"

cat > src/com/bla/bla/BuildInfo.java <<DELIM
package com.bla.bla;

// Auto-generated, triggered by project build
public class BuildInfo {
    public static String version = new String("$VERSION");
    public static String date = new String("$DATE");
    public static String time = new String("$TIME");
}
DELIM

I configure the external builder to execute every build (even automatic ones) before the java/android builders. This scheme indeed works fine when I do full project clean, however when I update another file, even though Eclipse does execute my external builder, and BuildInfo class is indeed being regenerated, Eclipse still uses old cached version of the class for the final result, so I see old info, which can sometimes be worse than not having build info at all.

Do you have any idea how I can tell Eclipse this file is generated and it shouldn't cache it at all, but load it from the disc after my external builder is done and before java builder? Is there any other elegant solution to get that data into the build?

Thank you!


Solution

  • In Eclipse you can specify the resources that have changed after a custom built-tool ran.

    Go to your built-tool select Edit switch to the Refresh-Tab. Select Refresh resources upon completion, Specific resources… and add your auto-generated BuildInfo.java.