From 79cb66a428ffc1d17f5ec24d2a9b57b46b41e692 Mon Sep 17 00:00:00 2001
From: Daniel Gerhardt <code@dgerhardt.net>
Date: Tue, 12 Jan 2016 18:24:20 +0100
Subject: [PATCH] Add version info

Version information is now compiled as part of a build and written to
version.properties. It is exposed by the API.
---
 pom.xml                                       | 30 +++++++++++++++++++
 .../de/thm/arsnova/config/ExtraConfig.java    |  9 ++++++
 .../arsnova/controller/WelcomeController.java | 21 +++++++++++--
 src/main/resources/version.properties         |  4 +++
 4 files changed, 62 insertions(+), 2 deletions(-)
 create mode 100644 src/main/resources/version.properties

diff --git a/pom.xml b/pom.xml
index cca21dee5..94ee48bd5 100644
--- a/pom.xml
+++ b/pom.xml
@@ -9,6 +9,7 @@
 	<properties>
 		<io.spring.platform-version>2.0.1.RELEASE</io.spring.platform-version>
 		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+		<timestamp>${maven.build.timestamp}</timestamp>
 		<sonar.language>java</sonar.language>
 		<mobile.production.path>../arsnova-mobile/src/main/webapp/build/production/ARSnova</mobile.production.path>
 		<mobile.testing.path>../arsnova-mobile/src/main/webapp/build/testing/ARSnova</mobile.testing.path>
@@ -294,6 +295,23 @@
 	</dependencies>
 
 	<build>
+		<resources>
+			<resource>
+				<directory>src/main/resources</directory>
+				<filtering>false</filtering>
+				<excludes>
+					<exclude>version.properties</exclude>
+				</excludes>
+			</resource>
+			<resource>
+				<directory>src/main/resources</directory>
+				<filtering>true</filtering>
+				<includes>
+					<include>version.properties</include>
+				</includes>
+			</resource>
+		</resources>
+
 		<plugins>
 			<plugin>
 				<groupId>org.apache.maven.plugins</groupId>
@@ -424,6 +442,18 @@
 					</execution>
 				</executions>
 			</plugin>
+			<plugin>
+				<groupId>pl.project13.maven</groupId>
+				<artifactId>git-commit-id-plugin</artifactId>
+				<version>2.2.0</version>
+				<executions>
+					<execution>
+						<goals>
+							<goal>revision</goal>
+						</goals>
+					</execution>
+				</executions>
+			</plugin>
 		</plugins>
 	</build>
 
diff --git a/src/main/java/de/thm/arsnova/config/ExtraConfig.java b/src/main/java/de/thm/arsnova/config/ExtraConfig.java
index e1848c7c6..3adfec49d 100644
--- a/src/main/java/de/thm/arsnova/config/ExtraConfig.java
+++ b/src/main/java/de/thm/arsnova/config/ExtraConfig.java
@@ -19,6 +19,7 @@ package de.thm.arsnova.config;
 
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.beans.factory.config.PropertiesFactoryBean;
 import org.springframework.cache.CacheManager;
 import org.springframework.cache.annotation.EnableCaching;
 import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
@@ -76,6 +77,14 @@ public class ExtraConfig extends WebMvcConfigurerAdapter {
 		return configurer;
 	}
 
+	@Bean
+	public PropertiesFactoryBean versionInfoProperties() {
+		PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
+		propertiesFactoryBean.setLocation(new ClassPathResource("version.properties"));
+
+		return propertiesFactoryBean;
+	}
+
 	@Bean(name = "connectorClient")
 	public ConnectorClient connectorClient() {
 		if (!connectorEnable) {
diff --git a/src/main/java/de/thm/arsnova/controller/WelcomeController.java b/src/main/java/de/thm/arsnova/controller/WelcomeController.java
index 095d62056..cf9d3d204 100644
--- a/src/main/java/de/thm/arsnova/controller/WelcomeController.java
+++ b/src/main/java/de/thm/arsnova/controller/WelcomeController.java
@@ -22,7 +22,10 @@ import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.UnknownHostException;
 import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
 
+import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
 
 import org.springframework.beans.factory.annotation.Value;
@@ -52,6 +55,9 @@ public class WelcomeController extends AbstractController {
 	@Value("${mobile.path}")
 	private String mobileContextPath;
 
+	@Resource(name="versionInfoProperties")
+	private Properties versionInfoProperties;
+
 	@RequestMapping(value = "/", method = RequestMethod.GET)
 	public View home(final HttpServletRequest request) {
 		return new RedirectView(mobileContextPath + "/", false);
@@ -59,8 +65,19 @@ public class WelcomeController extends AbstractController {
 
 	@RequestMapping(value = "/", method = RequestMethod.GET, produces = "application/json")
 	@ResponseBody
-	public HashMap<String, Object> jsonHome(final HttpServletRequest request) {
-		return new HashMap<String, Object>();
+	public Map<String, Object> jsonHome(final HttpServletRequest request) {
+		Map<String, Object> response = new HashMap<>();
+		Map<String, Object> version = new HashMap<>();
+
+		version.put("string", versionInfoProperties.getProperty("version.string"));
+		version.put("buildTime", versionInfoProperties.getProperty("version.build-time"));
+		version.put("gitCommitId", versionInfoProperties.getProperty("version.git.commit-id"));
+		version.put("gitDirty", Boolean.parseBoolean(versionInfoProperties.getProperty("version.git.dirty")));
+
+		response.put("productName", "arsnova-backend");
+		response.put("version", version);
+
+		return response;
 	}
 
 	@RequestMapping(value = "/checkframeoptionsheader", method = RequestMethod.POST)
diff --git a/src/main/resources/version.properties b/src/main/resources/version.properties
new file mode 100644
index 000000000..412ab17e6
--- /dev/null
+++ b/src/main/resources/version.properties
@@ -0,0 +1,4 @@
+version.string=${project.version}
+version.build-time=${timestamp}
+version.git.commit-id=${git.commit.id}
+version.git.dirty=${git.dirty}
-- 
GitLab