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,44 @@ 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" ;
15
-
16
- private final String resourceName ;
16
+ private final List <String > resourceNames ;
17
17
18
18
public EmbeddedGitInfoBuilder () {
19
- this (EMBEDDED_GIT_PROPERTIES_FILE_NAME );
19
+ // Order is important here, from the most reliable sources to the least reliable ones
20
+ this (
21
+ Arrays .asList (
22
+ // Spring boot fat jars and wars should have the git.properties file in the following
23
+ // specific paths, guaranteeing that it's not coming from a dependency
24
+ "BOOT-INF/classes/datadog_git.properties" ,
25
+ "BOOT-INF/classes/git.properties" ,
26
+ "WEB-INF/classes/datadog_git.properties" ,
27
+ "WEB-INF/classes/git.properties" ,
28
+ // If we can't find the files above, probably because we're not in a spring context, we
29
+ // can look at the root of the classpath. Since it could be tainted by dependencies,
30
+ // we're looking for a specific datadog_git.properties file.
31
+ "datadog_git.properties" ));
20
32
}
21
33
22
- EmbeddedGitInfoBuilder (String resourceName ) {
23
- this .resourceName = resourceName ;
34
+ EmbeddedGitInfoBuilder (List < String > resourceNames ) {
35
+ this .resourceNames = resourceNames ;
24
36
}
25
37
26
38
@ Override
27
39
public GitInfo build (@ Nullable String repositoryPath ) {
28
40
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 );
41
+
42
+ for (String resourceName : resourceNames ) {
43
+ try (InputStream is = ClassLoader .getSystemResourceAsStream (resourceName )) {
44
+ if (is != null ) {
45
+ gitProperties .load (is );
46
+ // stop at the first git properties file found
47
+ break ;
48
+ } else {
49
+ log .debug ("Could not find embedded Git properties resource: {}" , resourceName );
50
+ }
51
+ } catch (IOException e ) {
52
+ log .error ("Error reading embedded Git properties from {}" , resourceName , e );
34
53
}
35
- } catch (IOException e ) {
36
- log .error ("Error reading embedded Git properties from {}" , resourceName , e );
37
54
}
38
55
39
56
String commitSha = gitProperties .getProperty ("git.commit.id" );
0 commit comments