Injecting CommitId Into a Spring Bean (#599)
* initial * working * added injection test
This commit is contained in:
parent
d81365a7ac
commit
1d511d4336
|
@ -0,0 +1,3 @@
|
|||
[submodule "testgitrepo"]
|
||||
path = testgitrepo
|
||||
url = /home/prd/Development/projects/idea/tutorials/spring-boot/src/main/resources/testgitrepo/
|
|
@ -112,7 +112,24 @@
|
|||
<artifactId>maven-war-plugin</artifactId>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
<plugin>
|
||||
<groupId>pl.project13.maven</groupId>
|
||||
<artifactId>git-commit-id-plugin</artifactId>
|
||||
<version>2.2.1</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>get-the-git-infos</id>
|
||||
<goals>
|
||||
<goal>revision</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<verbose>true</verbose>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
|
||||
</build>
|
||||
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.git;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
@PropertySource("classpath:/git.properties")
|
||||
@SpringBootApplication(scanBasePackages = { "com.baeldung.git"})
|
||||
public class CommitIdApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(CommitIdApplication.class, args);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.git;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class CommitInfoController {
|
||||
@Value("${git.commit.message.short}")
|
||||
private String commitMessage;
|
||||
|
||||
@Value("${git.branch}")
|
||||
private String branch;
|
||||
|
||||
@Value("${git.commit.id}")
|
||||
private String commitId;
|
||||
|
||||
@RequestMapping("/commitId")
|
||||
public GitInfoDto getCommitId() {
|
||||
return new GitInfoDto(commitMessage, branch, commitId);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung.git;
|
||||
|
||||
public class GitInfoDto {
|
||||
private String commitMessage;
|
||||
private String branch;
|
||||
private String commitId;
|
||||
|
||||
public GitInfoDto(String commitMessage, String branch, String commitId) {
|
||||
this.commitMessage = commitMessage;
|
||||
this.branch = branch;
|
||||
this.commitId = commitId;
|
||||
}
|
||||
|
||||
public String getCommitMessage() {
|
||||
return commitMessage;
|
||||
}
|
||||
|
||||
public String getBranch() {
|
||||
return branch;
|
||||
}
|
||||
|
||||
public String getCommitId() {
|
||||
return commitId;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.baeldung.git;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = CommitIdApplication.class)
|
||||
public class CommitIdTest {
|
||||
|
||||
@Value("${git.commit.message.short}")
|
||||
private String commitMessage;
|
||||
|
||||
@Value("${git.branch}")
|
||||
private String branch;
|
||||
|
||||
@Value("${git.commit.id}")
|
||||
private String commitId;
|
||||
|
||||
@Test
|
||||
public void shouldInjectGitInfoProperties() throws Exception {
|
||||
assertThat(commitMessage).isNotNull();
|
||||
assertThat(commitMessage).isNotEqualTo("${git.commit.message.short}");
|
||||
assertThat(branch).isNotNull();
|
||||
assertThat(branch).isNotEqualTo("${git.branch}");
|
||||
assertThat(commitId).isNotNull();
|
||||
assertThat(commitId).isNotEqualTo("${git.commit.id}");
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
Subproject commit b3e635b96d114e25ca1da696acfd881b5fac28a7
|
Loading…
Reference in New Issue