-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add version and deployment timestamp (#2)
- Loading branch information
Showing
4 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
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
22 changes: 22 additions & 0 deletions
22
src/main/java/com/example/DeploymentTimestampListener.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
%> | ||
<html> | ||
<body> | ||
<h2>Hello World!</h2> | ||
<p>Version: <%= version %></p> | ||
<p>Deployed at: <%= timestamp %></p> | ||
</body> | ||
</html> |