VersionInfoBundle is a simple Dropwizard bundle which exposes a
version string as a Jersey resource under /version.
-
Create a version.properties file that contains a
productVersionkey in the <project_dir>/src/main/resources/. It should look something like this:productVersion: <version>If the properties file cannot be read (for example, because of an incorrect path or an IO error), the bundle will throw a
RuntimeExceptionon construction. If the properties file can be read, but theproductVersionkey cannot be found, then the version will be "unknown". -
Add the
com.palantir.versioninfo:dropwizard-version-info:<VERSION>dependency to your project's build.gradle file. The most recent version number can be found in the [Relase Notes] (https://github.com/palantir/dropwizard-version-info/releases). The dependencies section should look something like this:dependencies { // ... unrelated dependencies omitted ... compile "com.palantir.versioninfo:dropwizard-version-info:<VERSION>" } -
Add the bundle in your Dropwizard application's
initializemethod (or create one if it doesn't already exist). It should look something like this:@Override public void initialize(Bootstrap<MyConfiguration> bootstrap) { bootstrap.addBundle(new VersionInfoBundle()); } -
Run your server, then access
<API_ROOT>/version. The<API_ROOT>is defined by therootPathandapplicationContextPathin your Dropwizard server configuration. -
If your application uses Feign to build clients, make sure your service interface extends VersionInfoService interface. The dependencies section for your
-apiproject should look like this:dependencies { // ... unrelated dependencies omitted ... compile "com.palantir.versioninfo:dropwizard-version-info-api:<VERSION>" }Your service interface should look like this:
public interface ExampleService extends VersionInfoService { @GET @Path("hello") @Produces(MediaType.TEXT_PLAIN) String hello(); }
To load the version.properties file from somewhere else in the classpath, pass the path to it as an argument to the bundle's constructor:
@Override
public void initialize(Bootstrap<MyConfiguration> bootstrap) {
bootstrap.addBundle(new VersionInfoBundle("/properties/info.properties"));
}
To generate the product version from Git, use the Git-Version Gradle Plugin.
You can use the write-version.gradle script to write the version from git into the version.properties file:
-
In your
build.gradle, include thewrite-version.gradledependency:apply from: "${rootDir}/write-version.gradle" -
Set your compile task to depend on the generation of the version file:
compileJava.dependsOn writeVersion -
Set your
.gitignoreto ignore changes to theversion.propertiesfile:*version.properties