diff --git a/pom.xml b/pom.xml index d28e325..13e4407 100644 --- a/pom.xml +++ b/pom.xml @@ -16,7 +16,10 @@ 17 + git_info + git.properties + org.springframework.boot @@ -107,6 +110,36 @@ credit-tracker + + pl.project13.maven + git-commit-id-plugin + 4.9.10 + + + get-the-git-infos + + revision + + compile + + + + false + ${project.basedir}/${git.info.relative.dir} + true + + ${project.build.outputDirectory}/${git.info.filename} + + ${git.info.prefix} + + ${git.info.prefix}.commit.id + ${git.info.prefix}.branch + ${git.info.prefix}.build.version + ${git.info.prefix}.build.time + ${git.info.prefix}.closest.tag.name + + + org.apache.maven.plugins maven-compiler-plugin diff --git a/src/main/java/ru/nbch/credit_tracker/config/settings/version/VersionInfoLogger.java b/src/main/java/ru/nbch/credit_tracker/config/settings/version/VersionInfoLogger.java new file mode 100644 index 0000000..cd6a5ae --- /dev/null +++ b/src/main/java/ru/nbch/credit_tracker/config/settings/version/VersionInfoLogger.java @@ -0,0 +1,32 @@ +package ru.nbch.credit_tracker.config.settings.version; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.context.event.ApplicationReadyEvent; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.event.EventListener; +import org.springframework.core.io.ClassPathResource; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +@Slf4j +@Configuration +public class VersionInfoLogger { + + @EventListener(ApplicationReadyEvent.class) + public void logGitInfo() { + try { + Properties properties = new Properties(); + ClassPathResource resource = new ClassPathResource("git.properties"); + try (InputStream inputStream = resource.getInputStream()) { + properties.load(inputStream); + String version = VersionInfoProvider.obtainVersionInfo(properties, "git_info"); + log.info("Application version: {}", version); + } + } catch (IOException e) { + log.warn("Failed to load git.properties", e); + } + } + +} diff --git a/src/main/java/ru/nbch/credit_tracker/config/settings/version/VersionInfoProvider.java b/src/main/java/ru/nbch/credit_tracker/config/settings/version/VersionInfoProvider.java new file mode 100644 index 0000000..ba53090 --- /dev/null +++ b/src/main/java/ru/nbch/credit_tracker/config/settings/version/VersionInfoProvider.java @@ -0,0 +1,58 @@ +package ru.nbch.credit_tracker.config.settings.version; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.util.StringUtils; + +import java.util.Properties; +import java.util.function.Function; + +@Slf4j +public class VersionInfoProvider { + + public static String obtainVersionInfo(Properties properties, String prefix) { + VersionInfoProvider gitVersionInfo = VersionInfoProvider.build(properties, prefix); + if (gitVersionInfo != null) return gitVersionInfo.toString(); + log.warn("no build info is available by git-commit-id-plugin..."); + return "[UNKNOWN VERSION]"; + } + + public static VersionInfoProvider build(Properties properties, String prefix) { + if (properties == null || prefix == null) return null; + VersionInfoProvider provider = new VersionInfoProvider(properties, prefix); + if (!StringUtils.hasText(provider.gitRevision)) return null; + return provider; + } + + + //from git-commit-id-plugin + private String gitRevision; + private String gitBranch; + private String mavenVersion; + private String buildTime; + private String tag; + + private VersionInfoProvider(Properties properties, String prefix) { + Function pref = s -> prefix + "." + s; + if (properties != null) { + gitRevision = properties.getProperty(pref.apply("commit.id")); + gitBranch = properties.getProperty(pref.apply("branch")); + mavenVersion = properties.getProperty(pref.apply("build.version")); //project version from maven + buildTime = properties.getProperty(pref.apply("build.time")); + tag = properties.getProperty(pref.apply("closest.tag.name")); + } + } + + @Override + public String toString() { + if (gitRevision != null) { + return String.format("v. %s-%s-%s-%s", //-%s-%s + mavenVersion, + gitBranch, + gitRevision.substring(0, Math.min(gitRevision.length(), 8)), + buildTime //, tag, gitRevision + ); + } else { + return "UNKNOWN_VERSION"; + } + } +}