- Overall
- Quick Go Through
- Add Dependencies
- Add Plugin
- Run maven command
- Expose using SpringBoot Actuator
- Expose using Custom EndPoint
- Expose using Custom RestControllerEndPoint
- From doc from Spring.io, it tells what plugin we should use to get git information for us, including Maven and Gradle.
- Also, from doc from Spring.io
- Plugin will generate git.properties file automatically.
- A GitProperties bean is autoconfigured if a git.properties file is available at the root of the classpath
- If a GitProperties bean is available, you can use the info endpoint to expose these properties.
- Plugin will generate git.properties file automatically.
- So, we will first expose git infos into /info endpoint provided by SpringBoot actuator.
- And if this isn't enough, we customize an /endpoint specific for showing git-infos.
- Secure our endpoint if it contains some info we wouldn't want anybody to see.
- Option 1: Secure your endpoint with Spring Security
- Only expose endpoint within specific environment
- Option 1: By setting properties with different values in different environment, and use @ConditionalOnProperty to decide.
Check Class GitCustomRestControllerEndPoint
- Option 1: By setting properties with different values in different environment, and use @ConditionalOnProperty to decide.
- Download and launch SpringBoot application with 8080 port.
- All endpoints we offer now: http://localhost:8080/actuator
- /info endpoint: http://localhost:8080/actuator/info
- Custom /git-info endpoint
- http://localhost:8080/actuator/git-info
- http://localhost:8080/actuator/git-info/true -> Which show full detail infos from git commit.
- Custom /defined-info endpoint
- http://localhost:8080/actuator/defined-info/git
- http://localhost:8080/actuator/defined-info/git/detail -> Which show full detail infos from git commit.
- Custom /custom-info endpoint
- http://localhost:8080/actuator/custom-info/git/detail -> Which show self-selected detail infos from git commit.
<!-- Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Starter for using Spring Boot's Actuator which provides production ready features to help us monitor and manage our application -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<build>
<plugins>
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
</plugin>
</plugins>
</build>
mvn clean compile
We should be able to see there is a 'git.properties' file under target folder like this.
And the content will be like this
⭐ We can decide what information to be included into git.properties by setting configuration of git plugin.
For more details of configuration, please refer to plugin_docs
Say we don't want infos that prefix with 'git.build', we set configuration as
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<configuration>
<excludeProperties>
<excludeProperty>git.build.*</excludeProperty>
</excludeProperties>
</configuration>
</plugin>
- Edit application.properties and add some configuration for actuator
# Disable all endpoints that SpringBoot Actuator provide by default
# For this project, we only enable /info endpoint
management.endpoints.enabled-by-default=false
management.endpoint.info.enabled=true
# Include only /info endpoint
management.endpoints.web.exposure.include=info
# full -> show all info about git commit
# simple -> show only 'branch, 'commit-id' and 'commit-time'
management.info.git.mode=full
# Enable git info in order to show in /actuator/info endpoint
management.info.git.enabled=true
- Now, start our SpringBoot application, we should be able to access to http://localhost:8080/actuator/info and saw git infos.
-
Add a class annotated with WebEndPoint and inject GitProperties like below
😋 @ReadOperation is for GET method for our custom endpoint. For further details, please reference to Spring-docs
@Component @WebEndpoint(id = "git-info") public class GitEndPoint { private final GitProperties gitProperties; @Autowired public GitEndPoint(@Autowired(required = false) GitProperties gitProperties) { this.gitProperties = gitProperties; } @ReadOperation public GitProperties getGitInfo() { if (ObjectUtils.isEmpty(gitProperties)) { return null; } return this.gitProperties; } }
-
Update our application.properties file, include also our custom endpoint
# Include only /info endpoint management.endpoints.web.exposure.include=info, git-info
-
Start our SpringBoot application again, we should be able to get response like below by accessing to http://localhost:8080/actuator/git-info
⚠️
If the info from GitProperties are not sufficient, we could directly return the whole content inside the Property file like below, or select needed info ourselves and return.
We should be able to access by http://localhost:8080/actuator/git-info/true/** * Return detail git info if ${showDetail} is true. * @return detail git information */ @ReadOperation public Object getGitInfo(@Selector boolean showDetail) { if (ObjectUtils.isEmpty(gitProperties) || !showDetail) { return null; } return getGitDetail(); } /** * Get all properties from git.properties file */ private Object getGitDetail() { Properties props; try { props = PropertiesLoaderUtils.loadAllProperties("git.properties"); } catch (IOException e) { throw new RuntimeException(e); } return props; }
Add a class annotate with @RestControllerEndPoint and define endpoints.
I personally prefer using RestControllerEndPoint because I can set up endpoints with all familiar annotation when we're developing apis, such as @GetMapping, @PostMapping, @RequestBody...etc.
Access this endpoint by:
- http://localhost:8080/actuator/defined-info/git
- http://localhost:8080/actuator/defined-info/git/detail -> Which show full detail infos from git commit.
Add a class annotate with @RestControllerEndPoint and inject all properties we want using @Value annotation.
⚠️
Remember to let SpringBoot know git.properties in order to inject value by @Value.
No matter we choose to achieve by using @PropertySource, config PropertySourcesPlaceholderConfigurer or any ways mentioned in this article.
Access this endpoint by:
- http://localhost:8080/actuator/custom-info/git/detail -> Which show self-selected detail infos from git commit.
- Tracking issue for personal use of chinese commit message.