28
28
import org .gradle .api .Project ;
29
29
import org .gradle .api .logging .Logger ;
30
30
import org .gradle .api .logging .Logging ;
31
- import org .gradle .api .model .ObjectFactory ;
32
- import org .gradle .api .provider .Provider ;
33
31
import org .gradle .api .provider .ProviderFactory ;
34
32
import org .gradle .internal .jvm .Jvm ;
35
- import org .gradle .jvm .toolchain .JavaInstallation ;
36
- import org .gradle .jvm .toolchain .JavaInstallationRegistry ;
33
+ import org .gradle .internal .jvm .inspection .JvmInstallationMetadata ;
34
+ import org .gradle .internal .jvm .inspection .JvmMetadataDetector ;
35
+ import org .gradle .internal .jvm .inspection .JvmVendor ;
36
+ import org .gradle .jvm .toolchain .internal .InstallationLocation ;
37
+ import org .gradle .jvm .toolchain .internal .SharedJavaInstallationRegistry ;
37
38
import org .gradle .util .GradleVersion ;
38
39
39
40
import javax .inject .Inject ;
50
51
import java .nio .file .Paths ;
51
52
import java .time .ZoneOffset ;
52
53
import java .time .ZonedDateTime ;
53
- import java .util .ArrayList ;
54
54
import java .util .Arrays ;
55
55
import java .util .HashMap ;
56
56
import java .util .Iterator ;
@@ -69,14 +69,18 @@ public class GlobalBuildInfoPlugin implements Plugin<Project> {
69
69
private static Integer _defaultParallel = null ;
70
70
private static Boolean _isBundledJdkSupported = null ;
71
71
72
- private final JavaInstallationRegistry javaInstallationRegistry ;
73
- private final ObjectFactory objects ;
72
+ private final SharedJavaInstallationRegistry javaInstallationRegistry ;
73
+ private final JvmMetadataDetector metadataDetector ;
74
74
private final ProviderFactory providers ;
75
75
76
76
@ Inject
77
- public GlobalBuildInfoPlugin (JavaInstallationRegistry javaInstallationRegistry , ObjectFactory objects , ProviderFactory providers ) {
77
+ public GlobalBuildInfoPlugin (
78
+ SharedJavaInstallationRegistry javaInstallationRegistry ,
79
+ JvmMetadataDetector metadataDetector ,
80
+ ProviderFactory providers
81
+ ) {
78
82
this .javaInstallationRegistry = javaInstallationRegistry ;
79
- this .objects = objects ;
83
+ this .metadataDetector = metadataDetector ;
80
84
this .providers = providers ;
81
85
}
82
86
@@ -105,9 +109,10 @@ public void apply(Project project) {
105
109
params .reset ();
106
110
params .setRuntimeJavaHome (runtimeJavaHome );
107
111
params .setRuntimeJavaVersion (determineJavaVersion ("runtime java.home" , runtimeJavaHome , minimumRuntimeVersion ));
108
- params .setIsRutimeJavaHomeSet (Jvm .current ().getJavaHome ().equals (runtimeJavaHome ) == false );
109
- params .setRuntimeJavaDetails (getJavaInstallation (runtimeJavaHome ).getImplementationName ());
110
- params .setJavaVersions (getAvailableJavaVersions (minimumCompilerVersion ));
112
+ params .setIsRuntimeJavaHomeSet (Jvm .current ().getJavaHome ().equals (runtimeJavaHome ) == false );
113
+ JvmInstallationMetadata runtimeJdkMetaData = metadataDetector .getMetadata (getJavaInstallation (runtimeJavaHome ).getLocation ());
114
+ params .setRuntimeJavaDetails (formatJavaVendorDetails (runtimeJdkMetaData ));
115
+ params .setJavaVersions (getAvailableJavaVersions ());
111
116
params .setMinimumCompilerVersion (minimumCompilerVersion );
112
117
params .setMinimumRuntimeVersion (minimumRuntimeVersion );
113
118
params .setGradleJavaVersion (Jvm .current ().getJavaVersion ());
@@ -129,6 +134,11 @@ public void apply(Project project) {
129
134
project .getGradle ().getTaskGraph ().whenReady (graph -> logGlobalBuildInfo ());
130
135
}
131
136
137
+ private String formatJavaVendorDetails (JvmInstallationMetadata runtimeJdkMetaData ) {
138
+ JvmVendor vendor = runtimeJdkMetaData .getVendor ();
139
+ return runtimeJdkMetaData .getVendor ().getKnownVendor ().name () + "/" + vendor .getRawVendor ();
140
+ }
141
+
132
142
/* Introspect all versions of ES that may be tested against for backwards
133
143
* compatibility. It is *super* important that this logic is the same as the
134
144
* logic in VersionUtils.java. */
@@ -147,20 +157,22 @@ private void logGlobalBuildInfo() {
147
157
final String osVersion = System .getProperty ("os.version" );
148
158
final String osArch = System .getProperty ("os.arch" );
149
159
final Jvm gradleJvm = Jvm .current ();
150
- final String gradleJvmDetails = getJavaInstallation (gradleJvm .getJavaHome ()). getImplementationName ( );
151
-
160
+ JvmInstallationMetadata gradleJvmMetadata = metadataDetector . getMetadata (gradleJvm .getJavaHome ());
161
+ final String gradleJvmVendorDetails = gradleJvmMetadata . getVendor (). getDisplayName ();
152
162
LOGGER .quiet ("=======================================" );
153
163
LOGGER .quiet ("Elasticsearch Build Hamster says Hello!" );
154
164
LOGGER .quiet (" Gradle Version : " + GradleVersion .current ().getVersion ());
155
165
LOGGER .quiet (" OS Info : " + osName + " " + osVersion + " (" + osArch + ")" );
156
166
if (BuildParams .getIsRuntimeJavaHomeSet ()) {
157
- String runtimeJvmDetails = getJavaInstallation (BuildParams .getRuntimeJavaHome ()).getImplementationName ();
158
- LOGGER .quiet (" Runtime JDK Version : " + BuildParams .getRuntimeJavaVersion () + " (" + runtimeJvmDetails + ")" );
167
+ final String runtimeJvmVendorDetails = metadataDetector .getMetadata (BuildParams .getRuntimeJavaHome ())
168
+ .getVendor ()
169
+ .getDisplayName ();
170
+ LOGGER .quiet (" Runtime JDK Version : " + BuildParams .getRuntimeJavaVersion () + " (" + runtimeJvmVendorDetails + ")" );
159
171
LOGGER .quiet (" Runtime java.home : " + BuildParams .getRuntimeJavaHome ());
160
- LOGGER .quiet (" Gradle JDK Version : " + gradleJvm .getJavaVersion () + " (" + gradleJvmDetails + ")" );
172
+ LOGGER .quiet (" Gradle JDK Version : " + gradleJvm .getJavaVersion () + " (" + gradleJvmVendorDetails + ")" );
161
173
LOGGER .quiet (" Gradle java.home : " + gradleJvm .getJavaHome ());
162
174
} else {
163
- LOGGER .quiet (" JDK Version : " + gradleJvm .getJavaVersion () + " (" + gradleJvmDetails + ")" );
175
+ LOGGER .quiet (" JDK Version : " + gradleJvm .getJavaVersion () + " (" + gradleJvmVendorDetails + ")" );
164
176
LOGGER .quiet (" JAVA_HOME : " + gradleJvm .getJavaHome ());
165
177
}
166
178
LOGGER .quiet (" Random Testing Seed : " + BuildParams .getTestSeed ());
@@ -169,8 +181,8 @@ private void logGlobalBuildInfo() {
169
181
}
170
182
171
183
private JavaVersion determineJavaVersion (String description , File javaHome , JavaVersion requiredVersion ) {
172
- JavaInstallation installation = getJavaInstallation (javaHome );
173
- JavaVersion actualVersion = installation .getJavaVersion ();
184
+ InstallationLocation installation = getJavaInstallation (javaHome );
185
+ JavaVersion actualVersion = metadataDetector . getMetadata ( installation .getLocation ()). getLanguageVersion ();
174
186
if (actualVersion .isCompatibleWith (requiredVersion ) == false ) {
175
187
throwInvalidJavaHomeException (
176
188
description ,
@@ -183,46 +195,38 @@ private JavaVersion determineJavaVersion(String description, File javaHome, Java
183
195
return actualVersion ;
184
196
}
185
197
186
- private JavaInstallation getJavaInstallation (File javaHome ) {
187
- JavaInstallation installation ;
188
- if (isCurrentJavaHome (javaHome )) {
189
- installation = javaInstallationRegistry .getInstallationForCurrentVirtualMachine ().get ();
190
- } else {
191
- installation = javaInstallationRegistry .installationForDirectory (objects .directoryProperty ().fileValue (javaHome )).get ();
192
- }
193
-
194
- return installation ;
198
+ private InstallationLocation getJavaInstallation (File javaHome ) {
199
+ return getAvailableJavaInstallationLocationSteam ().filter (installationLocation -> isSameFile (javaHome , installationLocation ))
200
+ .findFirst ()
201
+ .get ();
195
202
}
196
203
197
- private List <JavaHome > getAvailableJavaVersions (JavaVersion minimumCompilerVersion ) {
198
- final List <JavaHome > javaVersions = new ArrayList <>();
199
- for (int v = 8 ; v <= Integer .parseInt (minimumCompilerVersion .getMajorVersion ()); v ++) {
200
- int version = v ;
201
- String javaHomeEnvVarName = getJavaHomeEnvVarName (Integer .toString (version ));
202
- if (System .getenv (javaHomeEnvVarName ) != null ) {
203
- File javaHomeDirectory = new File (findJavaHome (Integer .toString (version )));
204
- Provider <JavaInstallation > javaInstallationProvider = javaInstallationRegistry .installationForDirectory (
205
- objects .directoryProperty ().fileValue (javaHomeDirectory )
206
- );
207
- JavaHome javaHome = JavaHome .of (version , providers .provider (() -> {
208
- int actualVersion = Integer .parseInt (javaInstallationProvider .get ().getJavaVersion ().getMajorVersion ());
209
- if (actualVersion != version ) {
210
- throwInvalidJavaHomeException ("env variable " + javaHomeEnvVarName , javaHomeDirectory , version , actualVersion );
211
- }
212
- return javaHomeDirectory ;
213
- }));
214
- javaVersions .add (javaHome );
215
- }
204
+ private boolean isSameFile (File javaHome , InstallationLocation installationLocation ) {
205
+ try {
206
+ return Files .isSameFile (installationLocation .getLocation ().toPath (), javaHome .toPath ());
207
+ } catch (IOException ioException ) {
208
+ throw new UncheckedIOException (ioException );
216
209
}
217
- return javaVersions ;
218
210
}
219
211
220
- private static boolean isCurrentJavaHome (File javaHome ) {
221
- try {
222
- return Files .isSameFile (javaHome .toPath (), Jvm .current ().getJavaHome ().toPath ());
223
- } catch (IOException e ) {
224
- throw new UncheckedIOException (e );
225
- }
212
+ /**
213
+ * We resolve all available java versions using auto detected by gradles tool chain
214
+ * To make transition more reliable we only take env var provided installations into account for now
215
+ */
216
+ private List <JavaHome > getAvailableJavaVersions () {
217
+ return getAvailableJavaInstallationLocationSteam ().map (installationLocation -> {
218
+ File installationDir = installationLocation .getLocation ();
219
+ JvmInstallationMetadata metadata = metadataDetector .getMetadata (installationDir );
220
+ int actualVersion = Integer .parseInt (metadata .getLanguageVersion ().getMajorVersion ());
221
+ return JavaHome .of (actualVersion , providers .provider (() -> installationDir ));
222
+ }).collect (Collectors .toList ());
223
+ }
224
+
225
+ private Stream <InstallationLocation > getAvailableJavaInstallationLocationSteam () {
226
+ return Stream .concat (
227
+ javaInstallationRegistry .listInstallations ().stream (),
228
+ Stream .of (new InstallationLocation (Jvm .current ().getJavaHome (), "Current JVM" ))
229
+ );
226
230
}
227
231
228
232
private static String getTestSeed () {
0 commit comments