Skip to content

Instantly share code, notes, and snippets.

@inancsevinc
Created August 31, 2012 06:09
Show Gist options
  • Save inancsevinc/3549551 to your computer and use it in GitHub Desktop.
Save inancsevinc/3549551 to your computer and use it in GitHub Desktop.
Expose war package build date as a rest service, using maven, spring and jax-rs
# build properties
# this file is supposed to be a maven filtered resource, variables are replaced by build process
build.name=${pom.name}
build.version=${pom.version}
build.date=${buildDateTime}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.1.xsd">
<context:property-placeholder />
<context:annotation-config />
<util:properties id="buildprop" location="classpath:build.properties" />
<!-- ...bean definitions etc... -->
</beans>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- ...project specific stuff... -->
<properties>
<!-- ...other properties... -->
<buildDateTime>${maven.build.timestamp}</buildDateTime>
</properties>
<!-- ...dependencies... -->
<build>
<!-- ...plugins... -->
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources/build</directory>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
</project>
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @author Inanc Sevinc
*
*/
@Path("/")
@Component
public class TestResource{
@Value("#{buildprop['build.date']}")
private String buildDate;
/**
* @return build date of the package which is defined in build.properties
*/
@GET
@Path("/version")
@Produces(MediaType.TEXT_PLAIN)
public Response version() {
try {
return Response.ok(buildDate).build();
} catch (Exception e) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity("version info couldn't be retrieved").build();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment