Injecting CommitId Into a Spring Bean (#599)

* initial

* working

* added injection test
This commit is contained in:
pared 2016-08-14 15:54:46 +02:00 committed by Grzegorz Piwowarek
parent d81365a7ac
commit 1d511d4336
7 changed files with 115 additions and 1 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "testgitrepo"]
path = testgitrepo
url = /home/prd/Development/projects/idea/tutorials/spring-boot/src/main/resources/testgitrepo/

View File

@ -112,7 +112,24 @@
<artifactId>maven-war-plugin</artifactId> <artifactId>maven-war-plugin</artifactId>
</plugin> </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> </build>

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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}");
}
}

1
testgitrepo Submodule

@ -0,0 +1 @@
Subproject commit b3e635b96d114e25ca1da696acfd881b5fac28a7