From 841a759357d8f7a2a413079b40da58750d932e90 Mon Sep 17 00:00:00 2001 From: Ulli Hafner Date: Mon, 1 Apr 2024 17:19:52 +0200 Subject: [PATCH] Add methods to read Maven version and Git hash. --- pom.xml | 2 +- .../hm/hafner/grading/AutoGradingRunner.java | 62 +++++++++++++++++-- 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index c3df7c5..6cba760 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ edu.hm.hafner codingstyle-pom - 4.0.1 + 4.1.0 diff --git a/src/main/java/edu/hm/hafner/grading/AutoGradingRunner.java b/src/main/java/edu/hm/hafner/grading/AutoGradingRunner.java index d91898d..f91649e 100644 --- a/src/main/java/edu/hm/hafner/grading/AutoGradingRunner.java +++ b/src/main/java/edu/hm/hafner/grading/AutoGradingRunner.java @@ -4,6 +4,7 @@ import java.io.PrintStream; import java.nio.charset.StandardCharsets; import java.util.NoSuchElementException; +import java.util.Properties; import java.util.StringJoiner; import org.apache.commons.lang3.StringUtils; @@ -56,6 +57,10 @@ protected String getDisplayName() { return "Autograding"; } + private String getFullDisplayName(final FilteredLog log) { + return String.format("%s %s (#%s)", getDisplayName(), readVersion(log), readSha(log)); + } + /** * Runs the autograding. * @@ -67,7 +72,7 @@ public AggregatedScore run() { var logHandler = new LogHandler(outputStream, log); log.logInfo(SINGLE_LINE); - log.logInfo(center("Start")); + log.logInfo(center("Start", log)); log.logInfo(SINGLE_LINE); var configuration = getConfiguration(log); @@ -109,15 +114,64 @@ public AggregatedScore run() { } log.logInfo(SINGLE_LINE); - log.logInfo(center("End")); + log.logInfo(center("End", log)); log.logInfo(SINGLE_LINE); logHandler.print(); return score; } - private String center(final String message) { - return StringUtils.center(message + " " + getDisplayName(), 80); + /** + * Reads the Maven version information from the git.properties file. + * + * @param log + * the logger + * + * @return the version information + */ + protected String readVersion(final FilteredLog log) { + return readGitProperty("git.build.version", log); + } + + /** + * Reads the Git SHA from the git.properties file. + * + * @param log + * the logger + * + * @return the Git SHA + */ + protected String readSha(final FilteredLog log) { + return readGitProperty("git.commit.id.abbrev", log); + } + + protected String readGitProperty(final String key, final FilteredLog log) { + try (var propertiesFile = getClass().getResourceAsStream("/git.properties")) { + if (propertiesFile == null) { + log.logError("Version information file '/git.properties' not found in class path"); + + return StringUtils.EMPTY; + } + + try { + var gitProperties = new Properties(); + + gitProperties.load(propertiesFile); + + return gitProperties.getProperty(key); + } + catch (IOException exception) { + log.logError("Can't read version information in '/git.properties'."); + } + return StringUtils.EMPTY; + } + catch (IOException exception) { + return StringUtils.EMPTY; // ignore exception on close + } + } + + private String center(final String message, final FilteredLog log) { + return StringUtils.center(message + " " + getFullDisplayName(log), 80); } /**