11
11
import com .fasterxml .jackson .databind .ObjectMapper ;
12
12
13
13
import org .apache .commons .io .IOUtils ;
14
- import org .elasticsearch .gradle .VersionProperties ;
14
+ import org .elasticsearch .gradle .Version ;
15
15
import org .elasticsearch .gradle .internal .BwcVersions ;
16
16
import org .elasticsearch .gradle .internal .conventions .GitInfoPlugin ;
17
+ import org .elasticsearch .gradle .internal .conventions .VersionPropertiesPlugin ;
17
18
import org .elasticsearch .gradle .internal .conventions .info .GitInfo ;
18
19
import org .elasticsearch .gradle .internal .conventions .info .ParallelDetector ;
19
20
import org .elasticsearch .gradle .internal .conventions .util .Util ;
54
55
import java .nio .file .Files ;
55
56
import java .util .List ;
56
57
import java .util .Locale ;
58
+ import java .util .Properties ;
57
59
import java .util .Random ;
58
60
import java .util .concurrent .atomic .AtomicReference ;
61
+ import java .util .regex .Matcher ;
62
+ import java .util .regex .Pattern ;
59
63
import java .util .stream .Collectors ;
60
64
import java .util .stream .Stream ;
61
65
62
66
import javax .inject .Inject ;
63
67
64
68
import static org .elasticsearch .gradle .internal .conventions .GUtils .elvis ;
69
+ import static org .elasticsearch .gradle .internal .conventions .VersionPropertiesPlugin .VERSIONS_EXT ;
65
70
66
71
public class GlobalBuildInfoPlugin implements Plugin <Project > {
67
72
private static final Logger LOGGER = Logging .getLogger (GlobalBuildInfoPlugin .class );
68
73
private static final String DEFAULT_VERSION_JAVA_FILE_PATH = "server/src/main/java/org/elasticsearch/Version.java" ;
69
74
private static final String DEFAULT_BRANCHES_FILE_URL = "https://raw.githubusercontent.com/elastic/elasticsearch/master/branches.json" ;
70
75
private static final String BRANCHES_FILE_LOCATION_PROPERTY = "org.elasticsearch.build.branches-file-location" ;
76
+ private static final Pattern LINE_PATTERN = Pattern .compile (
77
+ "\\ W+public static final Version V_(\\ d+)_(\\ d+)_(\\ d+)(_alpha\\ d+|_beta\\ d+|_rc\\ d+)?.*\\ );"
78
+ );
71
79
72
80
private ObjectFactory objectFactory ;
73
81
private final JavaInstallationRegistry javaInstallationRegistry ;
@@ -106,21 +114,27 @@ public void apply(Project project) {
106
114
throw new GradleException ("Gradle " + minimumGradleVersion .getVersion () + "+ is required" );
107
115
}
108
116
109
- JavaVersion minimumCompilerVersion = JavaVersion .toVersion (getResourceContents ("/minimumCompilerVersion" ));
110
- JavaVersion minimumRuntimeVersion = JavaVersion .toVersion (getResourceContents ("/minimumRuntimeVersion" ));
117
+ project .getPlugins ().apply (VersionPropertiesPlugin .class );
118
+ Properties versionProperties = (Properties ) project .getExtensions ().getByName (VERSIONS_EXT );
119
+ JavaVersion minimumCompilerVersion = JavaVersion .toVersion (versionProperties .get ("minimumCompilerJava" ));
120
+ JavaVersion minimumRuntimeVersion = JavaVersion .toVersion (versionProperties .get ("minimumRuntimeJava" ));
121
+
122
+ String bundledJdkVersion = versionProperties .getProperty ("bundled_jdk" );
123
+ String bundledJdkMajorVersion = bundledJdkVersion .split ("[.+]" )[0 ];
124
+ Version elasticsearchVersionProperty = Version .fromString (versionProperties .getProperty ("elasticsearch" ));
111
125
112
126
Provider <File > explicitRuntimeJavaHome = findRuntimeJavaHome ();
113
127
boolean isRuntimeJavaHomeExplicitlySet = explicitRuntimeJavaHome .isPresent ();
114
128
Provider <File > actualRuntimeJavaHome = isRuntimeJavaHomeExplicitlySet
115
129
? explicitRuntimeJavaHome
116
- : resolveJavaHomeFromToolChainService (VersionProperties . getBundledJdkMajorVersion () );
130
+ : resolveJavaHomeFromToolChainService (bundledJdkMajorVersion );
117
131
118
132
Provider <JvmInstallationMetadata > runtimeJdkMetaData = actualRuntimeJavaHome .map (
119
133
runtimeJavaHome -> metadataDetector .getMetadata (getJavaInstallation (runtimeJavaHome ))
120
134
);
121
135
AtomicReference <BwcVersions > cache = new AtomicReference <>();
122
136
Provider <BwcVersions > bwcVersionsProvider = providers .provider (
123
- () -> cache .updateAndGet (val -> val == null ? resolveBwcVersions () : val )
137
+ () -> cache .updateAndGet (val -> val == null ? resolveBwcVersions (elasticsearchVersionProperty ) : val )
124
138
);
125
139
BuildParameterExtension buildParams = project .getExtensions ()
126
140
.create (
@@ -134,9 +148,7 @@ public void apply(Project project) {
134
148
javaHome -> determineJavaVersion (
135
149
"runtime java.home" ,
136
150
javaHome ,
137
- isRuntimeJavaHomeExplicitlySet
138
- ? minimumRuntimeVersion
139
- : JavaVersion .toVersion (VersionProperties .getBundledJdkMajorVersion ())
151
+ isRuntimeJavaHomeExplicitlySet ? minimumRuntimeVersion : JavaVersion .toVersion (bundledJdkMajorVersion )
140
152
)
141
153
),
142
154
isRuntimeJavaHomeExplicitlySet ,
@@ -190,19 +202,28 @@ private String formatJavaVendorDetails(JvmInstallationMetadata runtimeJdkMetaDat
190
202
/* Introspect all versions of ES that may be tested against for backwards
191
203
* compatibility. It is *super* important that this logic is the same as the
192
204
* logic in VersionUtils.java. */
193
- private BwcVersions resolveBwcVersions () {
205
+ private BwcVersions resolveBwcVersions (Version currentElasticsearchVersion ) {
194
206
String versionsFilePath = elvis (
195
207
System .getProperty ("BWC_VERSION_SOURCE" ),
196
208
new File (Util .locateElasticsearchWorkspace (project .getGradle ()), DEFAULT_VERSION_JAVA_FILE_PATH ).getPath ()
197
209
);
198
210
try (var is = new FileInputStream (versionsFilePath )) {
199
211
List <String > versionLines = IOUtils .readLines (is , "UTF-8" );
200
- return new BwcVersions (versionLines , getDevelopmentBranches ());
212
+ return new BwcVersions (currentElasticsearchVersion , parseVersionLines ( versionLines ) , getDevelopmentBranches ());
201
213
} catch (IOException e ) {
202
214
throw new IllegalStateException ("Unable to resolve to resolve bwc versions from versionsFile." , e );
203
215
}
204
216
}
205
217
218
+ private List <Version > parseVersionLines (List <String > versionLines ) {
219
+ return versionLines .stream ()
220
+ .map (LINE_PATTERN ::matcher )
221
+ .filter (Matcher ::matches )
222
+ .map (match -> new Version (Integer .parseInt (match .group (1 )), Integer .parseInt (match .group (2 )), Integer .parseInt (match .group (3 ))))
223
+ .sorted ()
224
+ .toList ();
225
+ }
226
+
206
227
private List <DevelopmentBranch > getDevelopmentBranches () {
207
228
String branchesFileLocation = project .getProviders ()
208
229
.gradleProperty (BRANCHES_FILE_LOCATION_PROPERTY )
0 commit comments