๋ณธ ๊ธ€์— ๋Œ€ํ•œ ๊ด€๋ จ ์ฝ”๋“œ๋Š” https://github.com/kdevkr/spring-demo ์—์„œ ์ฐธ๊ณ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์Šคํ”„๋ง ๋ถ€ํŠธ ํ”„๋กœ์ ํŠธ์—์„œ ๋นŒ๋“œ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ทธ๋ž˜๋“ค์ด๋‚˜ ๋ฉ”์ด๋ธ ํ”Œ๋Ÿฌ๊ทธ์ธ์— ๋”ฐ๋ผ ์•„๋ž˜์™€ ๊ฐ™์ด buildInfo()๊ฐ€ ๋™์ž‘ํ•˜๋„๋ก ๊ตฌ์„ฑํ•ด์•ผํ•œ๋‹ค. ์ด๋ ‡๊ฒŒ ๊ตฌ์„ฑํ•˜๋ฉด ๋นŒ๋“œ ๋‹จ๊ณ„์—์„œ ํด๋ž˜์ŠคํŒจ์Šค์— META-INF/build.properties๋ผ๋Š” ํŒŒ์ผ์— ๋นŒ๋“œ ์ •๋ณด๊ฐ€ ํฌํ•จ๋œ๋‹ค.

build.gradle
springBoot { buildInfo() }

ํด๋ž˜์ŠคํŒจ์Šค์— ์ถ”๊ฐ€๋œ build-info.properties์˜ ๋‚ด์šฉ์€ ProjectInfoAutoConfiguration ํด๋ž˜์Šค์— ์˜ํ•ด์„œ BuildProperties ํด๋ž˜์Šค๊ฐ€ ๋นˆ์œผ๋กœ ๋“ฑ๋ก๋˜๊ฒŒ ๋œ๋‹ค. ๊ทธ๋ž˜์„œ ์šฐ๋ฆฌ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด BuildProperties๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋นŒ๋“œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” API๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

BuildController.java
@RestController public class BuildController { private final BuildProperties buildProperties; public BuildController(final BuildProperties buildProperties) { this.buildProperties = buildProperties; } @GetMapping("build-info") public BuildProperties buildInfo() { return buildProperties; } }

ํ•˜์ง€๋งŒ, ๊ทธ๋ž˜๋“ค ๋ช…๋ น์–ด์— ์˜ํ•ด ๋นŒ๋“œ๋œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ํŒŒ์ผ์œผ๋กœ ์‹คํ–‰๋œ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ธํ…”๋ฆฌ์ œ์ด์™€ ๊ฐ™์€ IDE๋กœ ๊ตฌ๋™ํ•˜๋Š” ๊ฒฝ์šฐ๋ผ๋ฉด BuildProperties๊ฐ€ ๋“ฑ๋ก๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ํด๋ž˜์ŠคํŒจ์Šค์— ์žˆ๋Š” ๋นŒ๋“œ ์ •๋ณด๋ฅผ ๋จผ์ € ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก @AutoConfiguration์„ ํ†ตํ•ด ์ž๋™ ๊ตฌ์„ฑ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๊ณ  ProjectInfoAutoConfiguration๊ฐ€ ๋™์ž‘ํ•œ ์ดํ›„์— ์ˆ˜ํ–‰ํ•˜๋„๋ก ํ•˜๋ฉด ๋œ๋‹ค.

BuildConfiguration.java
//@Import(ProjectInfoAutoConfiguration.class) @AutoConfiguration(after = ProjectInfoAutoConfiguration.class) @Configuration public class BuildConfiguration { @ConditionalOnMissingBean(BuildProperties.class) @Bean public BuildProperties buildProperties() { Properties properties = new Properties(); properties.setProperty("time", DateTimeFormatter.ISO_INSTANT.format(Instant.now())); properties.setProperty("artifact", "demo"); properties.setProperty("group", "kr.kdev"); properties.setProperty("name", "demo"); properties.setProperty("version", "local"); return new BuildProperties(properties); } }

์œ„ ์ฝ”๋“œ์—์„œ @AutoConfiguration๋Š” ์Šคํ”„๋ง ๋ถ€ํŠธ 2.7 ์—์„œ ์ถ”๊ฐ€๋œ ์–ด๋…ธํ…Œ์ด์…˜์ด๋ฏ€๋กœ ์ด์ „ ๋ฒ„์ „์ด๋ผ๋ฉด @Import๋ฅผ ํ†ตํ•ด ์ˆ˜๋™์œผ๋กœ ๋™์ž‘ํ•˜๋„๋ก ํ•ด์•ผํ•œ๋‹ค.

๊ทธ๋Ÿฌ๋ฉด ์ด์ œ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์—์„œ๋Š” ProjectInfoAutoConfiguration์— ์˜ํ•ด BuildProperties๊ฐ€ ๋“ฑ๋ก๋˜์ง€ ์•Š๋”๋ผ๋„ ์ฝ”๋“œ๋กœ ์ •์˜๋œ BuildProperties๊ฐ€ ๋“ฑ๋ก๋˜์–ด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋นŒ๋“œ๋œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ๋Š” ๋นŒ๋“œ ์ •๋ณด API๊ฐ€ ๋“ฑ๋ก๋˜๋ฏ€๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด ๋นŒ๋“œ ์ •๋ณด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

Windows Terminal
# java -jar '-Dspring.profiles.active=prod' .\demo-1.0.0.jar # /build-info {"name":"demo","version":"local","time":"2023-03-22T13:13:58.186Z","artifact":"demo","group":"kr.kdev"}