Exposing the version of your Maven built web application
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.
To expose the version of your maven built spring web application, you can use a REST controller like that shown below. Note, you won't see a valid version while running in the IDE - it'll only work when running the Maven built WAR.
If you want to add the Jersey dependencies to your spring application try using the following dependency declaration.
This file contains 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; | |
} |
This file contains 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; | |
} | |
} |
This file contains 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
<dependency> | |
<groupId>com.sun.jersey.contribs</groupId> | |
<artifactId>jersey-spring</artifactId> | |
<version>1.8</version> | |
<exclusions> | |
<exclusion> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring</artifactId> | |
</exclusion> | |
<exclusion> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-core</artifactId> | |
</exclusion> | |
<exclusion> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-web</artifactId> | |
</exclusion> | |
<exclusion> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-beans</artifactId> | |
</exclusion> | |
<exclusion> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-context</artifactId> | |
</exclusion> | |
<exclusion> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-aop</artifactId> | |
</exclusion> | |
</exclusions> | |
</dependency> |