Said Savci
Said Savci

Reputation: 858

Constructing a version number in a Jenkins, Spring Boot and Gradle environment

I have Spring Boot web application and want to display a version number in my application. The version number should be something like v1.2.3#BUILD_NUMBER, where BUILD_NUMBER is the build number from Jenkins. How can I achieve this?

Upvotes: 1

Views: 1656

Answers (1)

Bjørn Vester
Bjørn Vester

Reputation: 7608

First, let's make sure Gradle knows about the build number. Jenkins makes it available with an environment variable. We can put it in the normal version field in the Gradle project:

version = "1.0-" + (System.getenv("BUILD_NUMBER") ?: "SNAPSHOT")

From here we have to put it into a file that your application can read. There are two good ways to do this, and I usually use both at the same time.

First, we update the standard MANIFEST.MF file in the final jar:

bootJar {
    manifest {
        attributes(
                "Implementation-Title": project.name,
                "Implementation-Version": project.version
        )
    }
}

By default, Spring Boot will print a banner on start-up and it reads those attributes and prints them out. You can customize the banner, but until you do it should look something like this:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.5.RELEASE)

2020-11-06 16:00:56.394  INFO 3144 --- [main] com.example.App : Starting App v1.0-SNAPSHOT on PF18RJAD with PID 3144 (C:\Work\Temp\buildinfo-example\app\build\libs\app-1.0-SNAPSHOT.jar started by bmv in C:\Work\Temp\buildinfo-example\app)
2020-11-06 16:00:56.398  INFO 3144 --- [main] com.example.App : No active profile set, falling back to default profiles: default
2020-11-06 16:00:57.181  INFO 3144 --- [main] com.example.App : Started App in 1.292 seconds (JVM running for 1.908)

Notice the line "Starting App v1.0-SNAPSHOT", where "SNAPSHOT" will be replaced with the environment variable if present.

But note that because MANIFEST.INF is only created as part of the jar, it will not show in tests or by running the bootRun task. You have to run the final "boot jar" (e.g. java -jar build/libs/app-1.0-SNAPSHOT.jar).

Secondly, you can use the BuildInfo task. This enables Actuator to read more details about the build, and makes it easier for you to programmatically query them:

springBoot {
  buildInfo()
}

This will generate a file META-INF/build-info.properties in your application with build information like version, artifact name or custom values. You can query them by injecting a BuildProperties instance:

@Autowired
BuildProperties buildProperties;

Use it to get information about the build:

public void greet() {
    System.out.println("Hello version: " + buildProperties.getVersion());
}

It should output:

Hello version: 1.0-SNAPSHOT

If you use Actuator, it will also pick up information from the build properties file.

Upvotes: 1

Related Questions