0
votes

Today i got a requirement, where i have to create a common module to expose the version of the application without changing anything in parent application

Common module groupId : com.mhn.version, artifactId: version-endpoint packaging: jar VersionController.java - where i will expose a REST Service "/version" as GET method which returns the details

In any spring boot application if i add this jar (module) as a dependency, then without changing anything in parent application it should fetch the application artifactId and version. Here in this case 1.0.1-SNAPSHOT

For example if i add this as dependency in spring-boot-sample-1.0.1-SNAPSHOT.war application as mentioned in pom.xml below

<groupdId>com.parent.app</groupId>
<artifactId>spring-boot-sample</artifactId>
<version>1.0.1-SNAPSHOT</version>
<packaging>war</packaging>

  <dependencies>
    <dependency>
      <groupId>com.mhn.version</groupId>
      <artifactId>version-endpoint</artifactId>
      <version>1.0</version>
    </dependency>
</dependencies>

Then spring-boot-sample-1.0.1-SNAPSHOT has to expose a service "/version". and by hitting that endpoint it should return maven project.artifactId and project.version details

In this example

{
"artifactId" : "spring-boot-sample"
"version" : "1.0.1-SNAPSHOT"
}

Guide me, if we have any third party jars, if not guide me on how to do this.

Make this as a note, we are not going to do any changes in parent application

1
Hi, welcome to Stack Overflow! Your question's title is not clear. Please leave out things like "I have a requirement" - we are here to help with specific and clearly-stated technical questions. You seem to be asking "How do I read Maven artifact information from within a Spring Boot application and present it via a RESTful web service?"Gabriel Bauman

1 Answers

0
votes

Spring Boot actuators will do this for you with some project configuration as shown in the documentation. Modifying the build configuration to look something like this:

<groupdId>com.parent.app</groupId>
<artifactId>spring-boot-sample</artifactId>
<version>1.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>
</dependencies>

<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <version>2.2.5.RELEASE</version>
      <executions>
        <execution>
          <goals>
            <goal>build-info</goal>
          </goals>
        </execution>
      </executions>
      ...
    </plugin>
  </plugins>
</build>

Will enable a response from the /actuator/info endpoint that might look like this:

{
    "build": {
        "artifact": "spring-boot-sample",
        "group": "com.parent.app",
        "name": "spring-boot-sample",
        "time": "2020-03-06T16:29:01.200Z",
        "version": "1.0.1-SNAPSHOT"
    }
}

If, for some reason, you can't use Boot actuators, you could read the content written by spring-boot-maven-plugin's build-info goal in your own library code by accessing the file as a resource with classpath:META-INF/build-info.properties.