This post is about how to solve a problem that bugged me for while.
When developing Portlets for Liferay one always wonders what exact version is actually running on the various servers of the development chain (local/test/integration/production) and what sources it was compiled from.
The first solution (when still using CVS/SVN):
When using CVS or SVN this can easily be solved by using keyword expansion and have either one of these systems update the MANIFEST.MF file containing the keyword placeholders.
For SVN adding keywords to the MANIFEST.MF file and simply adding a couple of lines to the build.xml file that change the MANIFEST.MF file (so it gets marked for check-in every time), the requested information can be included pretty automatically.
In the MANIFEST.MF file one needs to include the following placeholders:
Finally in Eclipse one needs to add the keywords to the MANIFEST.MF file (this feature is hidden in the MANIFEST.MF context menu under TEAM|Set Property…. In the resulting dialog choose ‘svn:keywords’ and enter ‘Revision Date Author‘ on a single line.
CVS always expands the keywords if present so only the Ant script is needed.
Getting hold of this information and making use of it is similar to the Mercurial solution presented next.
The second solution (after switching to Mercurial):
This method adds Mercurial information to the MANIFEST.MF unfortunately this is immediately outdated the moment one does a commit/push (because then a new revision is created).
BUT when the portlet is build for deployment the information in the MANIFEST.MF is updated to the correct values prior to compiling. The deployment build itself does not commit any files so no new revision is created. So when deployed one can easily find out what exact source code is used and compiled.
The first part is to prep the build.xml file so it retrieves and writes/updates this information into the MANIFEST/MF file.
A plain Liferay generated build.xml file looks like:
By inserting/include the following xml just after the import task we retrieve the wanted information and write/update it into the MANIFEST.MF file. This file and the docroot/META-INF directory where it should reside are also created if missing.
The manifest.xml should contain the following Ant script:
- The above script expects hg Mercurial executable to be on the path under Linux and to be installed in “c:\program files\Mercurial” when running under Windows.
- The Short-Node consists of the first 12 characters from the full Mercurial ”’Node”’ hash. This corresponds to the change set value that Source forge displays when browsing the repository.
- The Revision is the local revision within a project repository (in contrast to the system wide values of node and short-node). It corresponds to the change set number that Source forge displays when browsing the repository.
The resulting MANIFEST.MF looks like:
The lines between Created-By: and Class-Path: are originating from added the Ant build.xml script.
Finally how to retrieve this information in out Java code:
We need to add some utility methods to our code:
and call use code for example in the constructor of our Java backing bean like:
Final Note: A better type of output would be to use _log.debug() instead of _log.info() so the Tomcat log files are not polluted to much.