22
33import java .io .IOException ;
44import java .io .InputStream ;
5+ import java .util .Arrays ;
6+ import java .util .List ;
57import java .util .Properties ;
68import javax .annotation .Nullable ;
79import org .slf4j .Logger ;
@@ -11,29 +13,55 @@ public class EmbeddedGitInfoBuilder implements GitInfoBuilder {
1113
1214 private static final Logger log = LoggerFactory .getLogger (EmbeddedGitInfoBuilder .class );
1315
14- private static final String EMBEDDED_GIT_PROPERTIES_FILE_NAME = "git.properties" ;
16+ // Spring boot fat jars and wars should have the git.properties file in a specific path,
17+ // guaranteeing that it's not coming from a dependency
18+ private static final String SPRING_BOOT_JAR_EMBEDDED_GIT_PROPERTIES_FILE_NAME =
19+ "BOOT-INF/classes/git.properties" ;
20+ private static final String SPRING_BOOT_JAR_EMBEDDED_DATADOG_GIT_PROPERTIES_FILE_NAME =
21+ "BOOT-INF/classes/datadog_git.properties" ;
22+ private static final String WAR_EMBEDDED_GIT_PROPERTIES_FILE_NAME =
23+ "WEB-INF/classes/git.properties" ;
24+ private static final String WAR_EMBEDDED_DATADOG_GIT_PROPERTIES_FILE_NAME =
25+ "WEB-INF/classes/datadog_git.properties" ;
26+ // If we can't find the files above, probably because we're not in a spring context, we can look
27+ // at the root of the classpath.
28+ // Since it could be tainted by dependencies, we're looking for a specific datadog_git.properties
29+ // file.
30+ private static final String EMBEDDED_DATADOOG_GIT_PROPERTIES_FILE_NAME = "datadog_git.properties" ;
1531
16- private final String resourceName ;
32+ private final List < String > resourceNames ;
1733
1834 public EmbeddedGitInfoBuilder () {
19- this (EMBEDDED_GIT_PROPERTIES_FILE_NAME );
35+ // Order is important here, from the most reliable sources to the least reliable ones
36+ this (
37+ Arrays .asList (
38+ SPRING_BOOT_JAR_EMBEDDED_GIT_PROPERTIES_FILE_NAME ,
39+ SPRING_BOOT_JAR_EMBEDDED_DATADOG_GIT_PROPERTIES_FILE_NAME ,
40+ WAR_EMBEDDED_GIT_PROPERTIES_FILE_NAME ,
41+ WAR_EMBEDDED_DATADOG_GIT_PROPERTIES_FILE_NAME ,
42+ EMBEDDED_DATADOOG_GIT_PROPERTIES_FILE_NAME ));
2043 }
2144
22- EmbeddedGitInfoBuilder (String resourceName ) {
23- this .resourceName = resourceName ;
45+ EmbeddedGitInfoBuilder (List < String > resourceNames ) {
46+ this .resourceNames = resourceNames ;
2447 }
2548
2649 @ Override
2750 public GitInfo build (@ Nullable String repositoryPath ) {
2851 Properties gitProperties = new Properties ();
29- try (InputStream is = ClassLoader .getSystemResourceAsStream (resourceName )) {
30- if (is != null ) {
31- gitProperties .load (is );
32- } else {
33- log .debug ("Could not find embedded Git properties resource: {}" , resourceName );
52+
53+ for (String resourceName : resourceNames ) {
54+ try (InputStream is = ClassLoader .getSystemResourceAsStream (resourceName )) {
55+ if (is != null ) {
56+ gitProperties .load (is );
57+ // stop at the first git properties file found
58+ break ;
59+ } else {
60+ log .debug ("Could not find embedded Git properties resource: {}" , resourceName );
61+ }
62+ } catch (IOException e ) {
63+ log .error ("Error reading embedded Git properties from {}" , resourceName , e );
3464 }
35- } catch (IOException e ) {
36- log .error ("Error reading embedded Git properties from {}" , resourceName , e );
3765 }
3866
3967 String commitSha = gitProperties .getProperty ("git.commit.id" );
0 commit comments