From 1d511d4336f76a1cc8e239cd67586b06b63d977a Mon Sep 17 00:00:00 2001 From: pared Date: Sun, 14 Aug 2016 15:54:46 +0200 Subject: [PATCH] Injecting CommitId Into a Spring Bean (#599) * initial * working * added injection test --- .gitmodules | 3 ++ spring-boot/pom.xml | 19 ++++++++++- .../com/baeldung/git/CommitIdApplication.java | 14 ++++++++ .../baeldung/git/CommitInfoController.java | 22 +++++++++++++ .../java/com/baeldung/git/GitInfoDto.java | 25 +++++++++++++++ .../java/com/baeldung/git/CommitIdTest.java | 32 +++++++++++++++++++ testgitrepo | 1 + 7 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 .gitmodules create mode 100644 spring-boot/src/main/java/com/baeldung/git/CommitIdApplication.java create mode 100644 spring-boot/src/main/java/com/baeldung/git/CommitInfoController.java create mode 100644 spring-boot/src/main/java/com/baeldung/git/GitInfoDto.java create mode 100644 spring-boot/src/test/java/com/baeldung/git/CommitIdTest.java create mode 160000 testgitrepo diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000000..9c5cdb8f2d --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "testgitrepo"] + path = testgitrepo + url = /home/prd/Development/projects/idea/tutorials/spring-boot/src/main/resources/testgitrepo/ diff --git a/spring-boot/pom.xml b/spring-boot/pom.xml index 2c5304dbf3..66197feacd 100644 --- a/spring-boot/pom.xml +++ b/spring-boot/pom.xml @@ -112,7 +112,24 @@ maven-war-plugin - + + pl.project13.maven + git-commit-id-plugin + 2.2.1 + + + get-the-git-infos + + revision + + + + + true + + + + diff --git a/spring-boot/src/main/java/com/baeldung/git/CommitIdApplication.java b/spring-boot/src/main/java/com/baeldung/git/CommitIdApplication.java new file mode 100644 index 0000000000..1f774dbead --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/git/CommitIdApplication.java @@ -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); + } +} + diff --git a/spring-boot/src/main/java/com/baeldung/git/CommitInfoController.java b/spring-boot/src/main/java/com/baeldung/git/CommitInfoController.java new file mode 100644 index 0000000000..7829339cd6 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/git/CommitInfoController.java @@ -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); + } +} diff --git a/spring-boot/src/main/java/com/baeldung/git/GitInfoDto.java b/spring-boot/src/main/java/com/baeldung/git/GitInfoDto.java new file mode 100644 index 0000000000..b8b58f55e8 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/git/GitInfoDto.java @@ -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; + } +} diff --git a/spring-boot/src/test/java/com/baeldung/git/CommitIdTest.java b/spring-boot/src/test/java/com/baeldung/git/CommitIdTest.java new file mode 100644 index 0000000000..833cfc1c2a --- /dev/null +++ b/spring-boot/src/test/java/com/baeldung/git/CommitIdTest.java @@ -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}"); + } +} \ No newline at end of file diff --git a/testgitrepo b/testgitrepo new file mode 160000 index 0000000000..b3e635b96d --- /dev/null +++ b/testgitrepo @@ -0,0 +1 @@ +Subproject commit b3e635b96d114e25ca1da696acfd881b5fac28a7