2
2
3
3
import static org .hibernate .search .develocity .util .Strings .isBlank ;
4
4
5
+ import java .io .IOException ;
6
+ import java .nio .file .Files ;
7
+ import java .nio .file .Path ;
8
+ import java .util .Objects ;
5
9
import java .util .function .Function ;
10
+ import java .util .regex .Pattern ;
6
11
7
12
import org .hibernate .search .develocity .Log ;
8
13
import org .hibernate .search .develocity .util .JavaVersions ;
14
+ import org .hibernate .search .develocity .util .Strings ;
9
15
10
16
import com .gradle .maven .extension .api .scan .BuildScanApi ;
11
17
import org .apache .maven .execution .MavenSession ;
18
+ import org .apache .maven .project .MavenProject ;
12
19
13
20
public final class BuildScanMetadata {
14
21
22
+ private static final Pattern DOCKERFILE_FROM_PATTERN = Pattern .compile ( "FROM (.+)" );
23
+ private static final Pattern CONTAINER_IMAGE_SHORT_PATTERN = Pattern .compile (
24
+ "^(?:.*/)?([^/]+:[^-.]+(?:[-.][^-.]+)?).*$" );
25
+
15
26
private BuildScanMetadata () {
16
27
}
17
28
@@ -25,15 +36,104 @@ public static void addMetadataToBuildScan(BuildScanApi buildScanApi, MavenSessio
25
36
26
37
buildScanApi .tag ( "hibernate-search" );
27
38
28
- recordExecutableVersion ( buildScanApi , mavenSession , "java-version.main.compiler" , JavaVersions ::forJavacExecutable );
29
- recordExecutableVersion ( buildScanApi , mavenSession , "java-version.test.compiler" , JavaVersions ::forJavacExecutable );
30
- recordExecutableVersion ( buildScanApi , mavenSession , "java-version.test.launcher" , JavaVersions ::forJavaExecutable );
39
+ for ( MavenProject project : mavenSession .getProjects () ) {
40
+ tagIntegrations ( buildScanApi , mavenSession , project );
41
+ }
42
+
43
+ recordExecutableVersion (
44
+ buildScanApi , mavenSession , "java-version.main.compiler" , JavaVersions ::forJavacExecutable );
45
+ recordExecutableVersion (
46
+ buildScanApi , mavenSession , "java-version.test.compiler" , JavaVersions ::forJavacExecutable );
47
+ recordExecutableVersion (
48
+ buildScanApi , mavenSession , "java-version.test.launcher" , JavaVersions ::forJavaExecutable );
49
+ }
50
+
51
+ private static void tagIntegrations (BuildScanApi buildScanApi , MavenSession mavenSession , MavenProject project ) {
52
+ var dbKind = getStringProperty ( project , "test.database.run.kind" );
53
+ if ( !Strings .isBlank ( dbKind ) ) {
54
+ if ( dbKind .equals ( "h2" ) ) {
55
+ // H2 doesn't use containers
56
+ buildScanApi .tag ( "h2" );
57
+ }
58
+ else {
59
+ tagDockerfileShortImageRef ( buildScanApi , mavenSession ,
60
+ "database/%s.Dockerfile" .formatted ( dbKind ), null
61
+ );
62
+ }
63
+ }
64
+ if ( !getBooleanProperty ( project , "test.lucene.skip" ) ) {
65
+ buildScanApi .tag ( "lucene" );
66
+ }
67
+ if ( !getBooleanProperty ( project , "test.elasticsearch.skip" )
68
+ && getBooleanProperty ( project , "test.elasticsearch.run.image.pull" ) ) {
69
+ var distribution = getStringProperty ( mavenSession , "test.elasticsearch.distribution" );
70
+ tagDockerfileShortImageRef ( buildScanApi , mavenSession ,
71
+ "search-backend/%s.Dockerfile" .formatted ( distribution ),
72
+ getStringProperty ( mavenSession , "test.elasticsearch.version" )
73
+ );
74
+ }
75
+ }
76
+
77
+ private static void tagDockerfileShortImageRef (BuildScanApi buildScanApi , MavenSession mavenSession ,
78
+ String dockerfileRelativePath , String versionOverride ) {
79
+ var path = Path .of ( mavenSession .getExecutionRootDirectory (), "build/container" , dockerfileRelativePath );
80
+ try {
81
+ String ref ;
82
+ try ( var stream = Files .lines ( path ) ) {
83
+ ref = stream .map ( line -> {
84
+ var matcher = DOCKERFILE_FROM_PATTERN .matcher ( line );
85
+ if ( matcher .matches () ) {
86
+ return matcher .group ( 1 ).trim ();
87
+ }
88
+ else {
89
+ return null ;
90
+ }
91
+ } )
92
+ .filter ( Objects ::nonNull )
93
+ .findFirst ()
94
+ .orElseThrow ();
95
+ }
96
+ if ( !Strings .isBlank ( versionOverride ) ) {
97
+ ref = ref .substring ( 0 , ref .lastIndexOf ( ':' ) + 1 ) + versionOverride ;
98
+ }
99
+ String shortImageRef = toShortImageRef ( ref );
100
+ buildScanApi .tag ( shortImageRef );
101
+ buildScanApi .value (
102
+ shortImageRef .substring ( 0 , shortImageRef .lastIndexOf ( ':' ) ),
103
+ ref .substring ( ref .lastIndexOf ( ':' ) + 1 )
104
+ );
105
+ }
106
+ catch (RuntimeException | IOException e ) {
107
+ Log .warn ( "Unable to add tag from Dockerfile at %s: %s" .formatted ( path , e .getMessage () ) );
108
+ }
109
+ }
110
+
111
+ static String toShortImageRef (String ref ) {
112
+ var matcher = CONTAINER_IMAGE_SHORT_PATTERN .matcher ( ref );
113
+ if ( matcher .matches () ) {
114
+ return matcher .group ( 1 );
115
+ }
116
+ else {
117
+ return ref ;
118
+ }
31
119
}
32
120
33
121
private static void recordExecutableVersion (BuildScanApi buildScanApi , MavenSession mavenSession ,
34
122
String propertyName , Function <String , String > executableToVersion ) {
35
- String javaExecutable = ( String ) mavenSession . getResult (). getProject (). getProperties (). get ( propertyName );
123
+ String javaExecutable = getStringProperty ( mavenSession , propertyName );
36
124
String javaVersion = executableToVersion .apply ( javaExecutable );
37
125
buildScanApi .value ( propertyName , "Path: %s\n Resolved version: %s" .formatted ( javaExecutable , javaVersion ) );
38
126
}
127
+
128
+ private static String getStringProperty (MavenSession mavenSession , String key ) {
129
+ return getStringProperty ( mavenSession .getResult ().getProject (), key );
130
+ }
131
+
132
+ private static String getStringProperty (MavenProject project , String key ) {
133
+ return (String ) project .getProperties ().get ( key );
134
+ }
135
+
136
+ private static boolean getBooleanProperty (MavenProject project , String key ) {
137
+ return Boolean .parseBoolean ( getStringProperty ( project , key ) );
138
+ }
39
139
}
0 commit comments