Created
May 14, 2013 00:36
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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