Added git-commit-id plugin for app version logging
This commit is contained in:
parent
c5d0740abc
commit
656ca4172e
3 changed files with 123 additions and 0 deletions
33
pom.xml
33
pom.xml
|
|
@ -16,7 +16,10 @@
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<java.version>17</java.version>
|
<java.version>17</java.version>
|
||||||
|
<git.info.prefix>git_info</git.info.prefix>
|
||||||
|
<git.info.filename>git.properties</git.info.filename>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
|
@ -107,6 +110,36 @@
|
||||||
<build>
|
<build>
|
||||||
<finalName>credit-tracker</finalName>
|
<finalName>credit-tracker</finalName>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>pl.project13.maven</groupId>
|
||||||
|
<artifactId>git-commit-id-plugin</artifactId>
|
||||||
|
<version>4.9.10</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>get-the-git-infos</id>
|
||||||
|
<goals>
|
||||||
|
<goal>revision</goal>
|
||||||
|
</goals>
|
||||||
|
<phase>compile</phase>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
<configuration>
|
||||||
|
<failOnNoGitDirectory>false</failOnNoGitDirectory>
|
||||||
|
<dotGitDirectory>${project.basedir}/${git.info.relative.dir}</dotGitDirectory>
|
||||||
|
<generateGitPropertiesFile>true</generateGitPropertiesFile>
|
||||||
|
<generateGitPropertiesFilename>
|
||||||
|
${project.build.outputDirectory}/${git.info.filename}
|
||||||
|
</generateGitPropertiesFilename>
|
||||||
|
<prefix>${git.info.prefix}</prefix>
|
||||||
|
<includeOnlyProperties>
|
||||||
|
<includeOnlyProperty>${git.info.prefix}.commit.id</includeOnlyProperty>
|
||||||
|
<includeOnlyProperty>${git.info.prefix}.branch</includeOnlyProperty>
|
||||||
|
<includeOnlyProperty>${git.info.prefix}.build.version</includeOnlyProperty>
|
||||||
|
<includeOnlyProperty>${git.info.prefix}.build.time</includeOnlyProperty>
|
||||||
|
<includeOnlyProperty>${git.info.prefix}.closest.tag.name</includeOnlyProperty>
|
||||||
|
</includeOnlyProperties>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -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<String, String> 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";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue