1111import com .fasterxml .jackson .databind .ObjectMapper ;
1212
1313import org .apache .commons .io .IOUtils ;
14- import org .elasticsearch .gradle .VersionProperties ;
14+ import org .elasticsearch .gradle .Version ;
1515import org .elasticsearch .gradle .internal .BwcVersions ;
1616import org .elasticsearch .gradle .internal .conventions .GitInfoPlugin ;
17+ import org .elasticsearch .gradle .internal .conventions .VersionPropertiesPlugin ;
1718import org .elasticsearch .gradle .internal .conventions .info .GitInfo ;
1819import org .elasticsearch .gradle .internal .conventions .info .ParallelDetector ;
1920import org .elasticsearch .gradle .internal .conventions .util .Util ;
5455import java .nio .file .Files ;
5556import java .util .List ;
5657import java .util .Locale ;
58+ import java .util .Properties ;
5759import java .util .Random ;
5860import java .util .concurrent .atomic .AtomicReference ;
61+ import java .util .regex .Matcher ;
62+ import java .util .regex .Pattern ;
5963import java .util .stream .Collectors ;
6064import java .util .stream .Stream ;
6165
6266import javax .inject .Inject ;
6367
6468import static org .elasticsearch .gradle .internal .conventions .GUtils .elvis ;
69+ import static org .elasticsearch .gradle .internal .conventions .VersionPropertiesPlugin .VERSIONS_EXT ;
6570
6671public class GlobalBuildInfoPlugin implements Plugin <Project > {
6772 private static final Logger LOGGER = Logging .getLogger (GlobalBuildInfoPlugin .class );
6873 private static final String DEFAULT_VERSION_JAVA_FILE_PATH = "server/src/main/java/org/elasticsearch/Version.java" ;
6974 private static final String DEFAULT_BRANCHES_FILE_URL = "https://raw.githubusercontent.com/elastic/elasticsearch/master/branches.json" ;
7075 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+ );
7179
7280 private ObjectFactory objectFactory ;
7381 private final JavaInstallationRegistry javaInstallationRegistry ;
@@ -106,21 +114,27 @@ public void apply(Project project) {
106114 throw new GradleException ("Gradle " + minimumGradleVersion .getVersion () + "+ is required" );
107115 }
108116
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" ));
111125
112126 Provider <File > explicitRuntimeJavaHome = findRuntimeJavaHome ();
113127 boolean isRuntimeJavaHomeExplicitlySet = explicitRuntimeJavaHome .isPresent ();
114128 Provider <File > actualRuntimeJavaHome = isRuntimeJavaHomeExplicitlySet
115129 ? explicitRuntimeJavaHome
116- : resolveJavaHomeFromToolChainService (VersionProperties . getBundledJdkMajorVersion () );
130+ : resolveJavaHomeFromToolChainService (bundledJdkMajorVersion );
117131
118132 Provider <JvmInstallationMetadata > runtimeJdkMetaData = actualRuntimeJavaHome .map (
119133 runtimeJavaHome -> metadataDetector .getMetadata (getJavaInstallation (runtimeJavaHome ))
120134 );
121135 AtomicReference <BwcVersions > cache = new AtomicReference <>();
122136 Provider <BwcVersions > bwcVersionsProvider = providers .provider (
123- () -> cache .updateAndGet (val -> val == null ? resolveBwcVersions () : val )
137+ () -> cache .updateAndGet (val -> val == null ? resolveBwcVersions (elasticsearchVersionProperty ) : val )
124138 );
125139 BuildParameterExtension buildParams = project .getExtensions ()
126140 .create (
@@ -134,9 +148,7 @@ public void apply(Project project) {
134148 javaHome -> determineJavaVersion (
135149 "runtime java.home" ,
136150 javaHome ,
137- isRuntimeJavaHomeExplicitlySet
138- ? minimumRuntimeVersion
139- : JavaVersion .toVersion (VersionProperties .getBundledJdkMajorVersion ())
151+ isRuntimeJavaHomeExplicitlySet ? minimumRuntimeVersion : JavaVersion .toVersion (bundledJdkMajorVersion )
140152 )
141153 ),
142154 isRuntimeJavaHomeExplicitlySet ,
@@ -190,19 +202,28 @@ private String formatJavaVendorDetails(JvmInstallationMetadata runtimeJdkMetaDat
190202 /* Introspect all versions of ES that may be tested against for backwards
191203 * compatibility. It is *super* important that this logic is the same as the
192204 * logic in VersionUtils.java. */
193- private BwcVersions resolveBwcVersions () {
205+ private BwcVersions resolveBwcVersions (Version currentElasticsearchVersion ) {
194206 String versionsFilePath = elvis (
195207 System .getProperty ("BWC_VERSION_SOURCE" ),
196208 new File (Util .locateElasticsearchWorkspace (project .getGradle ()), DEFAULT_VERSION_JAVA_FILE_PATH ).getPath ()
197209 );
198210 try (var is = new FileInputStream (versionsFilePath )) {
199211 List <String > versionLines = IOUtils .readLines (is , "UTF-8" );
200- return new BwcVersions (versionLines , getDevelopmentBranches ());
212+ return new BwcVersions (currentElasticsearchVersion , parseVersionLines ( versionLines ) , getDevelopmentBranches ());
201213 } catch (IOException e ) {
202214 throw new IllegalStateException ("Unable to resolve to resolve bwc versions from versionsFile." , e );
203215 }
204216 }
205217
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+
206227 private List <DevelopmentBranch > getDevelopmentBranches () {
207228 String branchesFileLocation = project .getProviders ()
208229 .gradleProperty (BRANCHES_FILE_LOCATION_PROPERTY )
0 commit comments