출처1 : https://stackoverflow.com/questions/45738975/spring-boot-is-there-a-way-to-show-the-pom-version-number-in-some-endpoint-easi

출처2 : https://docs.spring.io/spring-boot/docs/current/maven-plugin/build-info-mojo.html

출처3 : https://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/build-info.html


스프링 부트로 작업을 한 war 파일을 배포할 때 버전관리를 하고 싶었다. 

처음에는 application.properties 파일에 적어보았는데, 

버전 관리 뿐 아니라도 종종 바뀌는 터라 왠지 여기가 아닌 것 같았고

이미 버전을 넣는 곳이 pom.xml 에 있었기 때문에 pom.xml 을 사용하고 싶었다.


개발환경은 다음과 같다.

spring boot 2.x

maven 3.5.x


찾다보니 스트림으로 읽어들이는 방법이 보였는데 일단 난 동작을 잘 안하더라.

왠지 path 같은거 맞추면 될 것 같기도 했지만 굳이. 라는 생각이 들던차에 BuildProperties 라는 클래스를 찾았다.

단순히 Autowired로 연결하여 사용할 수 있다길래 일단 테스트를 해보니, build info가 없어서 안된다더라?


그래서 찾다보니 나의 경우에는 build-info를 생성하는 곳이 없어서 그런 것 같았다. 

다음과 같이 설정하면 빌드시마다 ./META-INF/build-info.properties 라는 파일이 생성된다.

pom.xml

<project>

  ...

  <build>

    ...

    <plugins>

      ...

      <plugin>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-maven-plugin</artifactId>

        <version>2.0.6.RELEASE</version>

        <executions>

          <execution>

            <goals>

              <goal>build-info</goal>

            </goals>

            <configuration>

              <additionalProperties>

                <encoding.source>UTF-8</encoding.source>

                <encoding.reporting>UTF-8</encoding.reporting>

                <java.source>${maven.compiler.source}</java.source>

                <java.target>${maven.compiler.target}</java.target>

              </additionalProperties>

            </configuration>

          </execution>

        </executions>

        ...

      </plugin>

      ...

    </plugins>

    ...

  </build>

  ...

</project>


이제 build-info를 읽고 싶은 곳에서 다음과 같이 읽어 들일 수 있다.

@Autowired

BuildProperties buildProperties;

 

@ResponseBody

@RequestMapping(value = { "version" }, method = RequestMethod.GET, produces = "application/json")

public Map<String, String> getPcOffVersion() {

    Map<String, String> data = new HashMap<String, String>();

    data.put("Version", buildProperties.getVersion());

   

    LocalDateTime lt = LocalDateTime.ofInstant(buildProperties.getTime(), ZoneOffset.systemDefault());

    data.put("BuildTime", lt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

 

    return data;

}










Posted by motolies
,