github
diff --git a/src/main/java/com/example/DeploymentTimestampListener.java b/src/main/java/com/example/DeploymentTimestampListener.java
new file mode 100644
index 0000000..6185ca2
--- /dev/null
+++ b/src/main/java/com/example/DeploymentTimestampListener.java
@@ -0,0 +1,22 @@
+package com.example;
+
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
+import javax.servlet.annotation.WebListener;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+
+@WebListener
+public class DeploymentTimestampListener implements ServletContextListener {
+
+ @Override
+ public void contextInitialized(ServletContextEvent sce) {
+ String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
+ sce.getServletContext().setAttribute("deploymentTimestamp", timestamp);
+ }
+
+ @Override
+ public void contextDestroyed(ServletContextEvent sce) {
+ // No need to handle
+ }
+}
diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml
index 6a0571e..a041d6c 100644
--- a/src/main/webapp/WEB-INF/web.xml
+++ b/src/main/webapp/WEB-INF/web.xml
@@ -4,4 +4,7 @@
Web Application for testing
+
+ com.example.DeploymentTimestampListener
+
\ No newline at end of file
diff --git a/src/main/webapp/index.jsp b/src/main/webapp/index.jsp
index 667b454..45a168a 100644
--- a/src/main/webapp/index.jsp
+++ b/src/main/webapp/index.jsp
@@ -1,5 +1,31 @@
+<%@ page import="java.io.InputStream" %>
+<%@ page import="javax.xml.parsers.DocumentBuilderFactory" %>
+<%@ page import="javax.xml.xpath.XPath" %>
+<%@ page import="javax.xml.xpath.XPathConstants" %>
+<%@ page import="javax.xml.xpath.XPathFactory" %>
+<%@ page import="org.w3c.dom.Document" %>
+<%@ page import="org.w3c.dom.Node" %>
+<%
+ String version = "N/A";
+ try (InputStream input = getServletContext().getResourceAsStream("/META-INF/maven/bcgov.example/java-maven-pipeline-example/pom.xml")) {
+ if (input != null) {
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ Document doc = factory.newDocumentBuilder().parse(input);
+ XPath xpath = XPathFactory.newInstance().newXPath();
+ Node versionNode = (Node) xpath.evaluate("/project/version", doc, XPathConstants.NODE);
+ if (versionNode != null) {
+ version = versionNode.getTextContent();
+ }
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ String timestamp = (String) application.getAttribute("deploymentTimestamp");
+%>
Hello World!
+Version: <%= version %>
+Deployed at: <%= timestamp %>
\ No newline at end of file