Created
May 25, 2013 11:42
-
-
Save prule/5648809 to your computer and use it in GitHub Desktop.
A REST controller which can be used to expose the version of a WAR application built with Maven. Useful when used with vsConsole to monitor the versions of you applications deployed to various environments. See http://vs-console.appspot.com/
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
| import org.springframework.context.annotation.Scope; | |
| import org.springframework.stereotype.Component; | |
| import javax.servlet.ServletContext; | |
| import javax.ws.rs.GET; | |
| import javax.ws.rs.Path; | |
| import javax.ws.rs.Produces; | |
| import javax.ws.rs.core.Context; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.text.MessageFormat; | |
| import java.util.Properties; | |
| @Component | |
| @Scope("request") | |
| @Path("version") | |
| public class VersionREST { | |
| private static final String VERSION_KEY = "version"; | |
| private static final String POM_PROPS_PATH_MF = "/META-INF/maven/{0}/{1}/pom.properties"; | |
| @Context | |
| private ServletContext context; | |
| @GET | |
| @Produces("text/plain") | |
| public String version() { | |
| return getMavenWarVersion(context, "<YOUR GROUP ID>", "<YOUR ARTIFACT ID"); | |
| } | |
| public String getMavenWarVersion(ServletContext servletContext, String groupId, String artifactId) { | |
| String resourceName = MessageFormat.format(POM_PROPS_PATH_MF, new Object[]{groupId, artifactId}); | |
| Properties properties = loadProperties(servletContext.getResourceAsStream(resourceName)); | |
| if (properties != null) { | |
| return properties.getProperty(VERSION_KEY); | |
| } | |
| return "unknown"; | |
| } | |
| private 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