├── src ├── main │ ├── resources │ │ └── buildNumber.properties │ ├── java │ │ └── com │ │ │ └── jayway │ │ │ └── buildnumber │ │ │ ├── SpringJavaConfig.java │ │ │ ├── BuildNumberController.java │ │ │ └── PropertiesFileReader.java │ └── webapp │ │ ├── index.html │ │ └── WEB-INF │ │ └── web.xml └── test │ └── java │ └── com │ └── jayway │ └── buildnumber │ └── PropertiesFileReaderTest.java ├── .gitignore ├── README.md └── pom.xml /src/main/resources/buildNumber.properties: -------------------------------------------------------------------------------- 1 | git-sha-1=${buildNumber} -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Maven 2 | target/ 3 | 4 | # IntelliJ 5 | /.idea 6 | *.iml 7 | *.iws 8 | *.ipr 9 | 10 | # Mac OS X 11 | .DS_Store -------------------------------------------------------------------------------- /src/test/java/com/jayway/buildnumber/PropertiesFileReaderTest.java: -------------------------------------------------------------------------------- 1 | package com.jayway.buildnumber; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.assertFalse; 6 | 7 | 8 | public class PropertiesFileReaderTest { 9 | 10 | @Test 11 | public void shouldGetGitSha1() { 12 | String gitSha1 = PropertiesFileReader.getGitSha1(); 13 | 14 | assertFalse(gitSha1.isEmpty()); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/jayway/buildnumber/SpringJavaConfig.java: -------------------------------------------------------------------------------- 1 | package com.jayway.buildnumber; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.web.servlet.config.annotation.EnableWebMvc; 6 | 7 | /** 8 | * Spring Java configuration used for this application. 9 | * 10 | * @author Mattias Severson, Jayway 11 | */ 12 | @Configuration 13 | @EnableWebMvc 14 | public class SpringJavaConfig { 15 | 16 | /** 17 | * Creates the build number controller. 18 | * @return A {@link BuildNumberController}. 19 | */ 20 | @Bean 21 | public BuildNumberController buildNumberController() { 22 | return new BuildNumberController(); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Git SHA-1 Demo 2 | 3 | There are several way in which a Java project can publish its current Git SHA1: 4 | 5 | * As an entry in the manifest file 6 | * As a property in a properties file 7 | * As a Java method call 8 | * As a specific resource in a web service 9 | * As a part of the DOM (e.g. as a html comment in the index.html file) in a web project 10 | 11 | For more information, read the blog post at [http://blog.jayway.com/2012/04/07/continuous-deployment-versioning-and-git/] 12 | (http://blog.jayway.com/2012/04/07/continuous-deployment-versioning-and-git/) 13 | 14 | 15 | ## Getting started 16 | 17 | Open a terminal and execute the following steps: 18 | 19 | 1. Clone the project 20 | 21 | git clone git://github.com/matsev/git-build-number.git 22 | 23 | 2. Change directory 24 | 25 | cd git-build-number 26 | 27 | 3. Execute the project as a web app 28 | 29 | mvn jetty:run-war 30 | 31 | 4. Browse to [http://localhost:8080/](http://localhost:8080/) -------------------------------------------------------------------------------- /src/main/java/com/jayway/buildnumber/BuildNumberController.java: -------------------------------------------------------------------------------- 1 | package com.jayway.buildnumber; 2 | 3 | import org.springframework.http.MediaType; 4 | import org.springframework.stereotype.Controller; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | import org.springframework.web.bind.annotation.ResponseBody; 7 | 8 | import java.util.Collections; 9 | import java.util.Map; 10 | 11 | /** 12 | * A simple Spring controller that publishes the build number information. 13 | * 14 | * @author Mattias Severson, Jayway 15 | */ 16 | @Controller 17 | public class BuildNumberController { 18 | 19 | /** 20 | * Gets the Git SHA-1 and returns it as a JSON object. 21 | * @return A JSON object with the Git SHA-1 of the build. 22 | */ 23 | @RequestMapping(value = "/git-sha-1", produces = MediaType.APPLICATION_JSON_VALUE) 24 | @ResponseBody 25 | public Map getGitSha1() { 26 | String gitSha1 = PropertiesFileReader.getGitSha1(); 27 | return Collections.singletonMap("git-sha-1", gitSha1); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/webapp/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Git SHA-1 Demo 7 | 8 | 9 | 10 | 11 |

Git SHA-1 Demo

12 | 13 |

14 | A demo of how a Git SHA-1 can be embedded in a project: 15 | 16 |

24 | 25 | For more information, read the blog post at 26 | http://blog.jayway.com/2012/04/07/continuous-deployment-versioning-and-git 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | org.springframework.web.context.ContextLoaderListener 9 | 10 | 11 | 12 | contextClass 13 | org.springframework.web.context.support.AnnotationConfigWebApplicationContext 14 | 15 | 16 | 17 | contextConfigLocation 18 | com.jayway.buildnumber.SpringJavaConfig 19 | 20 | 21 | 22 | dispatcher 23 | org.springframework.web.servlet.DispatcherServlet 24 | 25 | contextClass 26 | 27 | org.springframework.web.context.support.AnnotationConfigWebApplicationContext 28 | 29 | 30 | 1 31 | 32 | 33 | 34 | dispatcher 35 | /build-number/* 36 | 37 | 38 | 39 | index.html 40 | 41 | 42 | -------------------------------------------------------------------------------- /src/main/java/com/jayway/buildnumber/PropertiesFileReader.java: -------------------------------------------------------------------------------- 1 | package com.jayway.buildnumber; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.util.Properties; 6 | 7 | /** 8 | * A property file reader that is implemented by using standard Java SE components. We could have used Spring's 9 | * {@link org.springframework.beans.factory.annotation.Value}, 10 | * {@link org.springframework.beans.factory.config.PlaceholderConfigurerSupport} and friends, but this approach let us 11 | * reuse it in any Java environment. 12 | * 13 | * @author Mattias Severson, Jayway 14 | */ 15 | public class PropertiesFileReader { 16 | 17 | private static final Properties properties; 18 | 19 | /** 20 | * Use a static initializer to read the properties file. 21 | */ 22 | static { 23 | InputStream inputStream = PropertiesFileReader.class.getResourceAsStream("/buildNumber.properties"); 24 | properties = new Properties(); 25 | try { 26 | properties.load(inputStream); 27 | } catch (IOException e) { 28 | throw new RuntimeException("Failed to read properties file", e); 29 | } finally { 30 | if (inputStream != null) { 31 | try { 32 | inputStream.close(); 33 | } catch (IOException e) { 34 | // Ignore 35 | } 36 | } 37 | } 38 | } 39 | 40 | /** 41 | * Hide default constructor. 42 | */ 43 | private PropertiesFileReader() {} 44 | 45 | /** 46 | * Gets the Git SHA-1. 47 | * @return A {@code String} with the Git SHA-1. 48 | */ 49 | public static String getGitSha1() { 50 | return properties.getProperty("git-sha-1"); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.jayway 6 | git-build-number 7 | 0.1-SNAPSHOT 8 | war 9 | 10 | Git Build Number 11 | 12 | 13 | 14 | Mattias Severson 15 | http://blog.jayway.com/2012/04/07/continuous-deployment-versioning-and-git 16 | Jayway 17 | http://www.jayway.com 18 | 19 | 20 | 21 | 22 | https://github.com/matsev/git-build-number 23 | scm:git:git://github.com/matsev/git-build-number.git 24 | scm:git:ssh://git@github.com:matsev/git-build-number.git 25 | 26 | 27 | 28 | UTF-8 29 | 30 | 31 | 32 | 33 | org.springframework 34 | spring-webmvc 35 | 3.1.2.RELEASE 36 | 37 | 38 | 39 | cglib 40 | cglib 41 | 2.2.2 42 | 43 | 44 | 45 | org.codehaus.jackson 46 | jackson-mapper-asl 47 | 1.9.10 48 | 49 | 50 | 51 | junit 52 | junit 53 | 4.10 54 | test 55 | 56 | 57 | 58 | 59 | 60 | 61 | org.apache.maven.plugins 62 | maven-compiler-plugin 63 | 2.5.1 64 | 65 | 1.6 66 | 1.6 67 | 68 | 69 | 70 | 71 | org.codehaus.mojo 72 | buildnumber-maven-plugin 73 | 1.1 74 | 75 | 76 | validate 77 | 78 | create 79 | 80 | 81 | 82 | 83 | 84 | 85 | org.apache.maven.plugins 86 | maven-war-plugin 87 | 2.3 88 | 89 | 90 | 91 | true 92 | 93 | 94 | 95 | ${buildNumber} 96 | 97 | 98 | 99 | 100 | 101 | ${basedir}/src/main/webapp 102 | true 103 | 104 | 105 | 106 | 107 | 108 | 109 | org.mortbay.jetty 110 | jetty-maven-plugin 111 | 8.1.4.v20120524 112 | 113 | foo 114 | 9999 115 | 5 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | ${basedir}/src/main/resources 124 | true 125 | 126 | 127 | 128 | 129 | --------------------------------------------------------------------------------