Skip to content

Instantly share code, notes, and snippets.

@prule
Created May 14, 2013 00:36
Show Gist options
  • Select an option

  • Save prule/5572715 to your computer and use it in GitHub Desktop.

Select an option

Save prule/5572715 to your computer and use it in GitHub Desktop.
Sometimes its useful to expose the version of your web application - I find it useful so vsConsole can display the version of the deployed application for each environment on the dashboard. If you build your app with Maven, you can use this code to get the version, and simply wire up a controller/rest endpoint to return it as text/plain.
private static final String VERSION_KEY = "version";
private static final String POM_PROPS_PATH_MF = "/META-INF/maven/{0}/{1}/pom.properties";
public static String getMavenWarVersion(ServletContext context, String groupId, String artifactId) {
String resourceName = MessageFormat.format(POM_PROPS_PATH_MF, new Object[]{groupId, artifactId});
Properties properties = loadProperties(context.getResourceAsStream(resourceName));
if (properties != null) {
return properties.getProperty(VERSION_KEY);
}
return "unknown";
}
private static Properties loadProperties(InputStream is) {
if (is != null) {
Properties properties = new Properties();
try {
properties.load(is);
} catch (IOException ex) {
return null;
}
return properties;
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment