From 0c74b57f12264dfdf846c9d25807bf6833bb7d90 Mon Sep 17 00:00:00 2001 From: Eike Cochu <eike@cochu.com> Date: Thu, 18 Feb 2016 13:20:59 +0100 Subject: [PATCH] added versioning added maven buildnumber versioning added version properties file and reader updated info resource to return app information --- vipra-backend/pom.xml | 45 +++++++++++++++ .../de/vipra/rest/resource/InfoResource.java | 57 +++++++++++++++++++ .../src/main/resources/buildNumber.properties | 3 + vipra-cmd/.classpath | 6 ++ vipra-cmd/pom.xml | 33 +++++++++++ .../src/main/resources/buildNumber.properties | 3 + vipra-util/.classpath | 5 ++ .../org.eclipse.core.resources.prefs | 1 + .../org.eclipse.wst.common.component | 1 + vipra-util/pom.xml | 33 +++++++++++ .../main/java/de/vipra/util/BuildInfo.java | 56 ++++++++++++++++++ .../main/java/de/vipra/util/FileUtils.java | 19 +++++-- .../src/main/resources/buildNumber.properties | 3 + 13 files changed, 261 insertions(+), 4 deletions(-) create mode 100644 vipra-backend/src/main/resources/buildNumber.properties create mode 100644 vipra-cmd/src/main/resources/buildNumber.properties create mode 100644 vipra-util/src/main/java/de/vipra/util/BuildInfo.java create mode 100644 vipra-util/src/main/resources/buildNumber.properties diff --git a/vipra-backend/pom.xml b/vipra-backend/pom.xml index 0b93290d..6bc93255 100644 --- a/vipra-backend/pom.xml +++ b/vipra-backend/pom.xml @@ -14,6 +14,8 @@ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.source>1.8</maven.compiler.source> + <maven.build.timestamp.format>yyMMdd_HHmm</maven.build.timestamp.format> + <buildDate>${maven.build.timestamp}</buildDate> <jerseyVersion>2.22.1</jerseyVersion> <jettyVersion>9.3.6.v20151106</jettyVersion> <servletVersion>3.1.0</servletVersion> @@ -21,6 +23,12 @@ <jacksonVersion>2.7.0</jacksonVersion> </properties> + <scm> + <connection>scm:git:https://git.cochu.io/master-thesis/ma-impl.git</connection> + <developerConnection>scm:git:https://git.cochu.io/master-thesis/ma-impl.git</developerConnection> + <url>https://git.cochu.io/master-thesis/ma-impl</url> + </scm> + <dependencies> <!-- Jersey REST --> <dependency> @@ -113,6 +121,43 @@ <skipTests>true</skipTests> </configuration> </plugin> + + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-war-plugin</artifactId> + <version>2.6</version> + <configuration> + <archive> + <manifest> + <addDefaultImplementationEntries>true</addDefaultImplementationEntries> + </manifest> + <manifestEntries> + <git-SHA-1>${buildNumber}</git-SHA-1> + </manifestEntries> + </archive> + </configuration> + </plugin> + + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>buildnumber-maven-plugin</artifactId> + <version>1.4</version> + <executions> + <execution> + <phase>validate</phase> + <goals> + <goal>create</goal> + </goals> + </execution> + </executions> + </plugin> </plugins> + + <resources> + <resource> + <directory>${basedir}/src/main/resources</directory> + <filtering>true</filtering> + </resource> + </resources> </build> </project> diff --git a/vipra-backend/src/main/java/de/vipra/rest/resource/InfoResource.java b/vipra-backend/src/main/java/de/vipra/rest/resource/InfoResource.java index 18dde950..0b24151d 100644 --- a/vipra-backend/src/main/java/de/vipra/rest/resource/InfoResource.java +++ b/vipra-backend/src/main/java/de/vipra/rest/resource/InfoResource.java @@ -1,5 +1,62 @@ package de.vipra.rest.resource; +import java.lang.management.ManagementFactory; +import java.lang.management.RuntimeMXBean; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriInfo; + +import de.vipra.rest.model.Wrapper; +import de.vipra.util.BuildInfo; +import de.vipra.util.NestedMap; +import de.vipra.util.StringUtils; + +@Path("info") public class InfoResource { + @Context + UriInfo uri; + + private static final NestedMap info = new NestedMap(); + + static { + try { + RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean(); + Runtime rt = Runtime.getRuntime(); + BuildInfo buildInfo = new BuildInfo(); + + // vm info + info.put("vm.starttime", rb.getStartTime()); + info.put("vm.uptime", rb.getUptime()); + info.put("vm.args", StringUtils.join(rb.getInputArguments())); + info.put("vm.java.vendor", System.getProperty("java.vendor")); + info.put("vm.java.version", System.getProperty("java.version")); + + // host info + info.put("host.cores", rt.availableProcessors()); + info.put("host.memory", rt.maxMemory()); + info.put("host.os.name", System.getProperty("os.name")); + info.put("host.os.arch", System.getProperty("os.arch")); + info.put("host.os.version", System.getProperty("os.version")); + + // app info + info.put("app.gitsha1", buildInfo.getGitSHA1()); + info.put("app.version", buildInfo.getVersion()); + info.put("app.builddate", buildInfo.getBuildDate()); + } catch (Exception e) { + info.put("error", e.getMessage()); + } + } + + @GET + @Produces(MediaType.APPLICATION_JSON) + public Response getInfo() { + Wrapper<NestedMap> res = new Wrapper<>(); + return res.ok(info); + } } diff --git a/vipra-backend/src/main/resources/buildNumber.properties b/vipra-backend/src/main/resources/buildNumber.properties new file mode 100644 index 00000000..e340aab1 --- /dev/null +++ b/vipra-backend/src/main/resources/buildNumber.properties @@ -0,0 +1,3 @@ +git-sha-1=${buildNumber} +version=${project.version} +builddate=${buildDate} \ No newline at end of file diff --git a/vipra-cmd/.classpath b/vipra-cmd/.classpath index 24f51ca2..4b6b2766 100644 --- a/vipra-cmd/.classpath +++ b/vipra-cmd/.classpath @@ -22,5 +22,11 @@ <attribute name="maven.pomderived" value="true"/> </attributes> </classpathentry> + <classpathentry kind="src" output="target/test-classes" path="src/test/java"> + <attributes> + <attribute name="optional" value="true"/> + <attribute name="maven.pomderived" value="true"/> + </attributes> + </classpathentry> <classpathentry kind="output" path="target/classes"/> </classpath> diff --git a/vipra-cmd/pom.xml b/vipra-cmd/pom.xml index 64fc4cdf..ccacb04b 100644 --- a/vipra-cmd/pom.xml +++ b/vipra-cmd/pom.xml @@ -14,8 +14,16 @@ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.source>1.8</maven.compiler.source> + <maven.build.timestamp.format>yyMMdd_HHmm</maven.build.timestamp.format> + <buildDate>${maven.build.timestamp}</buildDate> </properties> + <scm> + <connection>scm:git:https://git.cochu.io/master-thesis/ma-impl.git</connection> + <developerConnection>scm:git:https://git.cochu.io/master-thesis/ma-impl.git</developerConnection> + <url>https://git.cochu.io/master-thesis/ma-impl</url> + </scm> + <dependencies> <!-- Apache Commons --> <dependency> @@ -116,10 +124,35 @@ <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>de.vipra.cmd.Main</mainClass> + <addDefaultImplementationEntries>true</addDefaultImplementationEntries> </manifest> + <manifestEntries> + <git-SHA-1>${buildNumber}</git-SHA-1> + </manifestEntries> </archive> </configuration> </plugin> + + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>buildnumber-maven-plugin</artifactId> + <version>1.4</version> + <executions> + <execution> + <phase>validate</phase> + <goals> + <goal>create</goal> + </goals> + </execution> + </executions> + </plugin> </plugins> + + <resources> + <resource> + <directory>${basedir}/src/main/resources</directory> + <filtering>true</filtering> + </resource> + </resources> </build> </project> diff --git a/vipra-cmd/src/main/resources/buildNumber.properties b/vipra-cmd/src/main/resources/buildNumber.properties new file mode 100644 index 00000000..e340aab1 --- /dev/null +++ b/vipra-cmd/src/main/resources/buildNumber.properties @@ -0,0 +1,3 @@ +git-sha-1=${buildNumber} +version=${project.version} +builddate=${buildDate} \ No newline at end of file diff --git a/vipra-util/.classpath b/vipra-util/.classpath index 332896b5..bc4ecd83 100644 --- a/vipra-util/.classpath +++ b/vipra-util/.classpath @@ -6,6 +6,11 @@ <attribute name="maven.pomderived" value="true"/> </attributes> </classpathentry> + <classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"> + <attributes> + <attribute name="maven.pomderived" value="true"/> + </attributes> + </classpathentry> <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> <attributes> <attribute name="maven.pomderived" value="true"/> diff --git a/vipra-util/.settings/org.eclipse.core.resources.prefs b/vipra-util/.settings/org.eclipse.core.resources.prefs index e9441bb1..abdea9ac 100644 --- a/vipra-util/.settings/org.eclipse.core.resources.prefs +++ b/vipra-util/.settings/org.eclipse.core.resources.prefs @@ -1,3 +1,4 @@ eclipse.preferences.version=1 encoding//src/main/java=UTF-8 +encoding//src/main/resources=UTF-8 encoding/<project>=UTF-8 diff --git a/vipra-util/.settings/org.eclipse.wst.common.component b/vipra-util/.settings/org.eclipse.wst.common.component index 98cb9759..217de769 100644 --- a/vipra-util/.settings/org.eclipse.wst.common.component +++ b/vipra-util/.settings/org.eclipse.wst.common.component @@ -1,5 +1,6 @@ <?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0"> <wb-module deploy-name="vipra-util"> <wb-resource deploy-path="/" source-path="/src/main/java"/> + <wb-resource deploy-path="/" source-path="/src/main/resources"/> </wb-module> </project-modules> diff --git a/vipra-util/pom.xml b/vipra-util/pom.xml index 5084145a..aa784ea6 100644 --- a/vipra-util/pom.xml +++ b/vipra-util/pom.xml @@ -10,8 +10,16 @@ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.source>1.8</maven.compiler.source> + <maven.build.timestamp.format>yyMMdd_HHmm</maven.build.timestamp.format> + <buildDate>${maven.build.timestamp}</buildDate> </properties> + <scm> + <connection>scm:git:https://git.cochu.io/master-thesis/ma-impl.git</connection> + <developerConnection>scm:git:https://git.cochu.io/master-thesis/ma-impl.git</developerConnection> + <url>https://git.cochu.io/master-thesis/ma-impl</url> + </scm> + <dependencies> <!-- Apache Commons --> <dependency> @@ -58,4 +66,29 @@ <version>2.1.0</version> </dependency> </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>buildnumber-maven-plugin</artifactId> + <version>1.4</version> + <executions> + <execution> + <phase>validate</phase> + <goals> + <goal>create</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + + <resources> + <resource> + <directory>${basedir}/src/main/resources</directory> + <filtering>true</filtering> + </resource> + </resources> + </build> </project> \ No newline at end of file diff --git a/vipra-util/src/main/java/de/vipra/util/BuildInfo.java b/vipra-util/src/main/java/de/vipra/util/BuildInfo.java new file mode 100644 index 00000000..befee312 --- /dev/null +++ b/vipra-util/src/main/java/de/vipra/util/BuildInfo.java @@ -0,0 +1,56 @@ +package de.vipra.util; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +public class BuildInfo { + + private static Properties properties; + + private static Properties getProperties() { + if (properties == null) { + InputStream inputStream = FileUtils.getResource("buildNumber.properties"); + properties = new Properties(); + try { + properties.load(inputStream); + } catch (IOException e) { + throw new RuntimeException("Failed to read properties file", e); + } finally { + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException e) { + // Ignore + } + } + } + } + + return properties; + } + + private final String gitSHA1; + private final String version; + private final String buildDate; + + public BuildInfo() { + Properties props = getProperties(); + this.gitSHA1 = props.getProperty("git-sha-1"); + this.version = props.getProperty("version"); + this.buildDate = props.getProperty("builddate"); + } + + public String getGitSHA1() { + return gitSHA1; + } + + public String getVersion() { + return version; + } + + public String getBuildDate() { + return buildDate; + } + +} \ No newline at end of file diff --git a/vipra-util/src/main/java/de/vipra/util/FileUtils.java b/vipra-util/src/main/java/de/vipra/util/FileUtils.java index 44aa7bce..1b26ea29 100644 --- a/vipra-util/src/main/java/de/vipra/util/FileUtils.java +++ b/vipra-util/src/main/java/de/vipra/util/FileUtils.java @@ -77,21 +77,32 @@ public class FileUtils extends org.apache.commons.io.FileUtils { * * @param name * name of resource + * @Param clazz class to use to retrieve resource * @return resource or null */ - public static InputStream getResource(String name) { + public static InputStream getResource(String name, Class<?> clazz) { while (name.startsWith("/")) name = name.substring(1); InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(name); if (is == null) { - is = Config.class.getResourceAsStream(name); + if (clazz != null) + is = clazz.getResourceAsStream(name); + else + is = Config.class.getResourceAsStream(name); } - if (isJAR && !name.startsWith("resources") && getResource("resources/") != null) - return getResource("resources/" + name); + if (isJAR && !name.startsWith("resources") && getResource("resources/", clazz) != null) + return getResource("resources/" + name, clazz); else return is; } + /** + * @see {@link FileUtils#getResource(String, Class)} + */ + public static InputStream getResource(String name) { + return getResource(name, null); + } + /** * Counts the lines in a file * diff --git a/vipra-util/src/main/resources/buildNumber.properties b/vipra-util/src/main/resources/buildNumber.properties new file mode 100644 index 00000000..e340aab1 --- /dev/null +++ b/vipra-util/src/main/resources/buildNumber.properties @@ -0,0 +1,3 @@ +git-sha-1=${buildNumber} +version=${project.version} +builddate=${buildDate} \ No newline at end of file -- GitLab