2
2
3
3
import java .io .IOException ;
4
4
import java .io .InputStream ;
5
+ import java .util .Arrays ;
6
+ import java .util .List ;
5
7
import java .util .Properties ;
6
8
import javax .annotation .Nullable ;
7
9
import org .slf4j .Logger ;
@@ -11,29 +13,55 @@ public class EmbeddedGitInfoBuilder implements GitInfoBuilder {
11
13
12
14
private static final Logger log = LoggerFactory .getLogger (EmbeddedGitInfoBuilder .class );
13
15
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" ;
15
31
16
- private final String resourceName ;
32
+ private final List < String > resourceNames ;
17
33
18
34
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 ));
20
43
}
21
44
22
- EmbeddedGitInfoBuilder (String resourceName ) {
23
- this .resourceName = resourceName ;
45
+ EmbeddedGitInfoBuilder (List < String > resourceNames ) {
46
+ this .resourceNames = resourceNames ;
24
47
}
25
48
26
49
@ Override
27
50
public GitInfo build (@ Nullable String repositoryPath ) {
28
51
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 );
34
64
}
35
- } catch (IOException e ) {
36
- log .error ("Error reading embedded Git properties from {}" , resourceName , e );
37
65
}
38
66
39
67
String commitSha = gitProperties .getProperty ("git.commit.id" );
0 commit comments