Skip to content

Instantly share code, notes, and snippets.

@mcantrell
Created September 4, 2012 02:15
Show Gist options
  • Save mcantrell/3615775 to your computer and use it in GitHub Desktop.
Save mcantrell/3615775 to your computer and use it in GitHub Desktop.
Custom JSTL function to retrieve application version from Maven build metadata
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd">
<tlib-version>2.0</tlib-version>
<short-name>zfn</short-name>
<function>
<name>applicationVersion</name>
<function-class>org.devnull.zuul.web.config.JstlFunctions</function-class>
<function-signature>
java.lang.String getApplicationVersion(javax.servlet.ServletContext)
</function-signature>
</function>
</taglib>
package org.devnull.zuul.web.config
import org.springframework.web.context.support.ServletContextResource
import javax.servlet.ServletContext
class JstlFunctions {
static String getApplicationVersion(ServletContext context) {
def mavenProps = new ServletContextResource(context, "/META-INF/maven/org.devnull/zuul-web/pom.properties")
if (mavenProps.exists()) {
def properties = new Properties()
def stream = mavenProps.inputStream
properties.load(stream)
stream.close()
return properties.getProperty("version") ?: "unknown"
}
else {
return "development"
}
}
}
<%@ taglib prefix="zfn" uri="/WEB-INF/tags/functions.tld" %>
Version: ${zfn:applicationVersion(pageContext.servletContext)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment