diff --git a/pom.xml b/pom.xml index 202ac02..8af6716 100644 --- a/pom.xml +++ b/pom.xml @@ -4,12 +4,15 @@ bcgov.example java-maven-pipeline-example war - 1.0.0-SNAPSHOT + 1.0.1-SNAPSHOT java-maven-pipeline-example 1.8 1.8 + UTF-8 + UTF-8 + central @@ -27,6 +30,7 @@ https://apps.nrs.gov.bc.ca/nexus/content/groups/public/ + junit @@ -34,7 +38,14 @@ 3.8.1 test + + javax.servlet + javax.servlet-api + 4.0.1 + provided + + @@ -47,6 +58,7 @@ 1.8 + org.apache.maven.plugins @@ -82,6 +94,7 @@ + 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