Skip to content

how to get Maven to build a properties file with the git commit version, branch, and build timestamp

Eric Jahn edited this page Feb 12, 2018 · 9 revisions

Maven part: [in the maven part, if you set to true, it won't build if there are uncommitted files]

HSLynk src/main/resources/ true maven-compiler-plugin 3.6.1 -Xlint:unchecked 1.7 1.7 org.apache.maven.plugins maven-war-plugin 3.1.0 org.apache.maven.plugins maven-dependency-plugin 3.0.1 org.codehaus.mojo buildnumber-maven-plugin 1.4 validate create false false 8 ${scmBranch}

and this is version.properties which lives in src/main/resources:

version=${project.version} name=${project.name} revision=${buildNumber} timestamp=${timestamp} branch=${scmBranch}

And some logging:

` public static void logVersion() { Properties properties = new Properties(); InputStream in = null; try { in = LoadPropertiesUtility.class.getClassLoader().getResourceAsStream("version.properties");

        if(in == null){
            LOGGER.log(Level.FINE, "Unknown Code Revision");
        }

        properties.load(in);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String timestamp = properties.getProperty("timestamp");
        String buildTimestamp = sdf.format(new Date(Long.parseLong(timestamp)));
        String version = properties.getProperty("name") + "-" + properties.getProperty("version");
        String revision = properties.getProperty("revision");
        String scmBranch = properties.getProperty("branch");

        LOGGER.log(Level.FINE, "{0} commit version {1}, built on {2} from branch {3}",
                   new Object[] { version, revision, buildTimestamp, scmBranch });
    } catch (IOException ex) {
        LOGGER.log(Level.FINE, ex.getMessage());
    } finally{
        if(in != null){
            try {
                in.close();
            } catch (IOException e) {
                LOGGER.log(Level.FINE, e.getMessage());
            }
        }
    }

`

Clone this wiki locally