Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add version and deployment timestamp #2

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
<groupId>bcgov.example</groupId>
<artifactId>java-maven-pipeline-example</artifactId>
<packaging>war</packaging>
<version>1.0.0-SNAPSHOT</version>
<version>1.0.1-SNAPSHOT</version>
<name>java-maven-pipeline-example</name>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<repositories>
<repository>
<id>central</id>
Expand All @@ -27,14 +30,22 @@
<url>https://apps.nrs.gov.bc.ca/nexus/content/groups/public/</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<!-- Maven Compiler Plugin -->
Expand All @@ -47,6 +58,7 @@
<target>1.8</target> <!-- Change as needed -->
</configuration>
</plugin>

<!-- Maven dependency Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down Expand Up @@ -82,6 +94,7 @@
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>github</id>
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/example/DeploymentTimestampListener.java
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
}
}
3 changes: 3 additions & 0 deletions src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@

<web-app>
<display-name>Web Application for testing</display-name>
<listener>
<listener-class>com.example.DeploymentTimestampListener</listener-class>
</listener>
</web-app>
26 changes: 26 additions & 0 deletions src/main/webapp/index.jsp
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>