下面列出了怎么用org.springframework.boot.info.GitProperties的API类实例代码及写法,或者点击链接到github查看源代码。
private String generatePrefix(GitProperties gitProperties, BuildProperties buildProperties) {
String shortCommitId = null;
if (Objects.nonNull(gitProperties)) {
shortCommitId = gitProperties.getShortCommitId();
}
Instant time = null;
String version = null;
if (Objects.nonNull(buildProperties)) {
time = buildProperties.getTime();
version = buildProperties.getVersion();
}
Object p = ObjectUtils.firstNonNull(shortCommitId, time, version, RandomStringUtils.randomAlphanumeric(12));
if (p instanceof Instant) {
return DateTimeFormatter.ISO_INSTANT.format((Instant) p);
}
return p.toString();
}
private VersionInfo(final VersionInfoProperties versionInfoProperties, final GitProperties gitProperties) {
if (gitProperties != null) {
this.commitId = gitProperties.getCommitId();
this.commitIdAbbrev = gitProperties.getShortCommitId();
this.branch = gitProperties.getBranch();
this.commitTime = gitProperties.get(COMMIT_TIME);
this.userName = gitProperties.get(USER_NAME);
this.userEmail = gitProperties.get(USER_EMAIL);
this.messageShort = gitProperties.get(MESSAGE_SHORT);
this.messageFull = gitProperties.get(MESSAGE_FULL);
} else {
this.commitId = versionInfoProperties.getCommitId();
this.commitIdAbbrev= versionInfoProperties.getCommitIdAbbrev();
this.commitTime = versionInfoProperties.getCommitTime();
this.userName = versionInfoProperties.getUserName();
this.userEmail = versionInfoProperties.getUserEmail();
this.messageShort = versionInfoProperties.getMessageShort();
this.messageFull = versionInfoProperties.getMessageFull();
this.branch = versionInfoProperties.getBranch();
}
this.version = Objects.toString(versionInfoProperties.getVersion(), this.commitId);
this.url = versionInfoProperties.getUrlTemplate().replace("{commit}", commitId).replace("{version}", version);
}
private void addStaticProperties() {
add("Build Time", buildProperties.getTime());
add("Build Version", buildProperties.getVersion());
add("Java Version", buildProperties.get("java.source"));
if (gitProperties.isPresent()) {
GitProperties gitProps = gitProperties.get();
add("Commit ID", gitProps.getCommitId());
add("Branch", gitProps.getBranch());
add("Commit Message", gitProps.get("commit.message.full"));
add("Commit Time", gitProps.getCommitTime());
}
addSpringProfiles();
addEnvProperty("JDBC Driver Class","spring.datasource.hikari.driver-class-name");
addEnvProperty("JDBC-URL","spring.datasource.hikari.jdbc-url");
addEnvProperty("Image Location","fredbet.image-location");
addEnvProperty("Image Size","fredbet.image-size");
addEnvProperty("Thumbnail Size","fredbet.thumbnail-size");
addEnvProperty("AWS S3 Bucket Name","fredbet.aws-s3bucket-name");
addEnvProperty("AWS Region","fredbet.aws-region");
}
public GitProperties getGit() {
Properties e = entries.entrySet()
.stream()
.filter(entry -> entry.toString().startsWith("git"))
.collect(Collectors.toMap(
entry -> entry.getKey().toString().replace("git", ""),
Map.Entry::getValue, (v1, v2) -> {
throw new RuntimeException();
},
Properties::new));
return new GitProperties(e);
}
@Autowired
public SystemInfoService(BuildProperties buildProperties, Environment environment, Optional<GitProperties> gitProperties) {
this.buildProperties = buildProperties;
this.environment = environment;
this.gitProperties = gitProperties;
addStaticProperties();
}
private GitProperties gitProperties(final String commitId) {
return new GitProperties(new Properties() {{
put("commit.id", commitId);
}});
}
@Test
public void generatePrefixFromShortCommitId() {
Properties gitProperties = new Properties();
gitProperties.put("commit.id.abbrev", "1234");
PrefixedKeyGenerator prefixedKeyGenerator = new PrefixedKeyGenerator(new GitProperties(gitProperties), null);
assertThat(prefixedKeyGenerator.getPrefix()).isEqualTo("1234");
}
@Test
public void generatePrefixFromCommitId() {
Properties gitProperties = new Properties();
gitProperties.put("commit.id", "1234567");
PrefixedKeyGenerator prefixedKeyGenerator = new PrefixedKeyGenerator(new GitProperties(gitProperties), null);
assertThat(prefixedKeyGenerator.getPrefix()).isEqualTo("1234567");
}
public PrefixedKeyGenerator(GitProperties gitProperties, BuildProperties buildProperties) {
this.prefix = generatePrefix(gitProperties, buildProperties);
}
/**
* Creates VersionInfo from Spring Boot {@link GitProperties}. Missing Information ({@link #version} and
* {@link #url})is filled from VersionInfoProperties.
*
* @param versionInfoProperties Edison VersionInfoProperties used for version and url
* @param gitProperties Spring Boot GitProperties for all the other properties.
* @return VersionInfo
*/
public static VersionInfo versionInfo(final VersionInfoProperties versionInfoProperties,
final GitProperties gitProperties) {
return new VersionInfo(versionInfoProperties, gitProperties);
}