Auto generating version & build information in Java

Until recently, I was relying on final Strings and longs to store version information in my programs. However, I soon faced the limitations of this approach such as forgetting to update version (or revision) information, conflicting maven and internal version information. So I switched to using java properties for handling version information and updating the properties at compile time using maven’s antrun plugins. This had its own short comings and resulted in complex pom.xml files.

I have to admit, I’m not a big fan of maven and its XML based structure: I don’t like it because its a gigantic beast. Every time I have to do something in maven, I find that I’m spending time researching online for the right plugin and looking up the documentation for the plugin of interest. As a developer, build management using maven should be the least of my concern {Not having to remember which maven plugin does what}. On the other hand, to be fair to maven, it has some cool features like dependency management, life cycle, convention based directory structure to name a few. But maven tried to do a lot of things, resulting in a complex product.

{At this point, I’m considering switching in Gradle. As I developer, I want to be spending time solving problems in the problem domain not trying to tame my build management system. But using Gradle requires grasp of Groovy (Yet Another Scripting Language – YASL) If only there was a build management system written in Python!!!!!! }

The solution which I’m going to discuss in this post uses Java annotations to generate versioning information at run-time using python scripts. Maven is used in a very limited way with this approach.

Steps

  1. Create Version annotation and a class which reads these annotations in your Java program
  2. Write a python script to write the Java annotation at runtime
  3. Structure your pom.xml file to include generated-sources folder and running our python script

1. Create Version annotation and a class which reads these annotations in your Java program

The first step is to create an annotation holder in your program which you’ll annotate at the runtime. Example here.

Then create a class which is going to read the annotation information. Example here.

2. Write a python script to write the Java annotation at runtime

The next step is to create a python script to generates package-info.java containing build time & date, version string, hostname, etc. The way this works is by creating a package information which is used to provide overall information about package contents. We will fill our annotations in this class. Example of a python script is here. Feel free to use it in your projects.

3. Structure your pom.xml file to include generated-sources folder and running our python script

You then need to tell your maven file to pick up the package-info.java which is auto-generated by the python script in the last step. The python script places the ‘package-info.java’ in “targer/generated-sources/java” folder. I used the build-helper-maven plugin to include a new source folder. Example here.

The last step is to tell maven to run the python script in generate-sources phases. I used the exec-maven plugin for this. Example here.

Checkout the complete project

I have uploaded a complete project on Github: https://github.com/umermansoor/Versionaire

To use the project, do the following:

$ git clone git@github.com:umermansoor/Versionaire.git

$ cd Versionaire

$ mvn package

$ java -cp ./target/versonaire-1.0-SNAPSHOT.jar com._10kloc.versionaire.App

Advertisements