poornerd

my thoughts on programming and other nerdy stuff

How to add auto-update the Version Number of a Play Framework 2.X Project

| 0 comments

I wanted to have Version numbers that get automatically updated when I want to release a new version, so I set about to find out how to do this with Play Framework.

I discovered that I could base it on the sbt-release plugin, but it was not so straight forward. Here is my strategy, so that in the end all I have to do is run “activator release“:

1. Add the Plugin

Add the plugin by adding this line to your project/plugins.sbt file:

addSbtPlugin("com.github.gseitz" % "sbt-release" % "1.0.2")

2. Update your build.sbt file:

Add this import near the top of the file:

import ReleaseTransformations._

Change the line with version to this:

version := (version in ThisBuild).value

Next optionally add this piece of code at the end and comment out the pipeline stages you do not want executed (Note: this is apparently the default pipeline):

releaseProcess := Seq[ReleaseStep](
  checkSnapshotDependencies,              // : ReleaseStep
  inquireVersions,                        // : ReleaseStep
  runTest,                                // : ReleaseStep
  setReleaseVersion,                      // : ReleaseStep
  commitReleaseVersion,                   // : ReleaseStep, performs the initial git checks
  tagRelease,                             // : ReleaseStep
  //publishArtifacts,                       // : ReleaseStep, checks whether `publishTo` is properly set up
  setNextVersion,                         // : ReleaseStep
  commitNextVersion                      // : ReleaseStep
  //pushChanges                             // : ReleaseStep, also checks that an upstream branch is properly configured
)

Note: I have commented out the automatic publish and git push

3. Get the Version Number in a controller and pass to a template

public static Result index() {
    String title = Application.class.getPackage().getImplementationTitle();  
    String version = Application.class.getPackage().getImplementationVersion();   
    return ok(index.render(version));
}

And display it in the template:

@(version: String)

...
Version: @version

4. Make sure everything is committed before you release

5. Execute the release

Once you execute the release, the new version will be stored in a file versions.sbt .


activator release

You can lookup more options and possibilities for the sbt-release plugin, including strategies for auto incrementing the version here: https://github.com/sbt/sbt-release

If you have read this far, you may as well follow me on Twitter:

Author: poornerd

Tech­nol­o­gist, Entre­pre­neur, Vision­ary, Pro­gram­mer :: Grad­u­ated from USC (Uni­ver­sity of South­ern Cal­i­for­nia) with a degree in Com­puter Sci­ence. After 10+ years of free­lance con­sult­ing and pro­gram­ming, he co-founded Site­Force AG eBusi­ness Solu­tions in 1999 in Munich (München), Ger­many.

Leave a Reply

Required fields are marked *.