Created
September 4, 2012 02:15
-
-
Save mcantrell/3615775 to your computer and use it in GitHub Desktop.
Custom JSTL function to retrieve application version from Maven build metadata
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
<?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> |
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
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" | |
} | |
} | |
} |
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
<%@ 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