foreword
When we want to know which version of the deployed project has a problem? When we want to know if the version running online is the version we expected? When we want to associate the deployed version with the code? If you use git for version management, you can use the git-commit-id-maven-plugin plugin to achieve the above functions.
The git-commit-id-maven-plugin plugin will generate a git.properties file based on the version number of the current branch. The content of git.properties is as follows
git.branch=master
git.build.host=xxx
git.build.time=2022-03-01T20\:33\:43+0800
git.build.user.email=aaa@qq.com
git.build.user.name=aaa
git.build.version=1.0-SNAPSHOT
git.closest.tag.commit.count=
git.closest.tag.name=
git.commit.id=6dab4430864e3e4a9fc1ba6f6b93f278100d4e2e
git.commit.id.abbrev=6dab443
git.commit.id.describe=6dab443-dirty
git.commit.id.describe-short=6dab443-dirty
git.commit.message.full=Add README.md
git.commit.message.short=Add README.md
git.commit.time=2022-03-01T16\:24\:48+0800
git.commit.user.email=aa@example
git.commit.user.name=aa
git.dirty=true
git.remote.origin.url=http://hello
git.tags=
git.total.commit.count=1
how to use
This article takes the springboot project as an example. The git-commit-id-maven-plugin plugin has been embedded in the parent pom of the springboot project to manage dependencies. as follows
<pluginManagement>
<plugins>
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>revision</goal>
</goals>
</execution>
</executions>
<configuration>
<verbose>true</verbose>
<dateFormat>yyyy-MM-dd'T'HH:mm:ssZ</dateFormat>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
</configuration>
</plugin>
</plugins>
</pluginManagement>
So we can do the following configuration in our project
1. Explicitly introduce the git-commit-id-plugin plugin in our project
<build>
<plugins>
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
</plugin>
</plugins>
</build>
2. Display git information by integrating with actuator
a. Introduce actuator GAV into the project
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
b. Browser access http://ip : port/actuator/info
If the above information is not enough, we can display the information through custom endpoints or write a controller ourselves
Detailed information can be displayed through org.springframework.boot.info.GitProperties
This example uses a custom endpoint as an example. Examples are as follows
@Endpoint(id = "git")
@Component
public class GitEndpoint {
@Autowired(required = false)
private GitProperties gitProperties;
@ReadOperation
public Object info() throws IOException {
if(ObjectUtils.isEmpty(gitProperties)){
return new HashMap<>();
}
return gitProperties;
}
}
Expose custom endpoints in application.yml
management:
endpoints:
web:
exposure:
include: 'git'
Browser access http://ip : port/actuator/git
If you still feel that the above information is not detailed enough, you can display it by parsing the git.properties file. Example
@Endpoint(id = "gitDetail")
@Slf4j
@Component
public class GitDetailEndPoint {
@ReadOperation
public Object detail() throws IOException {
Properties props = null;
try {
props = PropertiesLoaderUtils.loadAllProperties("git.properties");
return props;
} catch (IOException e) {
log.error("git.properties not found");
} finally {
}
return new HashMap<>();
}
}
Expose custom endpoints in application.yml
management:
endpoints:
web:
exposure:
include: 'gitDetail'
Browser access http://ip:port/actuator/gitDetail
Summarize
git-commit-id-maven-plugin is quite useful for verifying the project version in distributed or microservice projects. It is recommended that you have the opportunity to use it
demo link
https://github.com/lyb-geek/springboot-learning/tree/master/springboot-git-commit
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。